[antlr-interest] Somewhere between actions and string templates

Ted Villalba tedv at esrconsulting.com
Fri May 18 13:59:17 PDT 2007


Hi ,
 
I'm trying to move from print statements to StringTemplates in my tree
grammars, and in the process got stuck on a couple of things:
return values and 
how to reuse my previous actions.
 
I think the following lines may be enough to express where I am confused. I
am using a template here because I'm unable to grab the list of values
otherwise:
 
 
expression returns [String subexpr]:         ^('=' tag (v+=value)+) ->
template(values={$v}) "<values>" ;
 
//         {
//Commented out this code to add the template
//           $subexpr=$tag.text+":"+$value.text;      //added the template
because this $value.text would only grab the first value
//       }                                                  
//      ;
 
I am not so interested in printing something out, but rather in passing back
subexpr once I can get the list of 'values' into that expression(
$subexpr=.). 
 
Somewhat related, in the book when StringTemplates are introduced, the
embedded actions go away, yet the return statements remain, so I don't see
how the [int value] is populated here. Maybe if I understand that, it will
help me with the above.
 
expr returns [int value]
    :   ^('+' a=expr b=expr) -> add(a={$a.st},b={$b.st})
    |   ^('-' a=expr b=expr) -> sub(a={$a.st},b={$b.st})
    |   ^('*' a=expr b=expr) -> mult(a={$a.st},b={$b.st})
    |   INT -> int(v={$INT.text})
    |   ID  -> var(id={$ID.text}, varNum={locals.get($ID.text)})
;
 
Apologies for the barrage of misunderstandings, but I'm a little adrift here
I think.
Thanks very much for reading this far!
 
Ted
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070518/d662c237/attachment.html 


More information about the antlr-interest mailing list