[antlr-interest] How to use arg* when rewriting with StringTemplate

Terence Parr parrt at cs.usfca.edu
Wed Oct 24 16:50:55 PDT 2007


On Oct 24, 2007, at 11:13 PM, <Robert.Klaus at innovations.de>  
<Robert.Klaus at innovations.de> wrote:

> Hi,
>
> I would like to rewrite a language using a TreeGrammar. For this  
> purpose
> I have defined a matching rule and rewrite with a template like this:
>
>
> ^(ID args+=arg*) -> template(id={$ID.text}, arg={$args}) "<id> ( <arg;
> separator=\", \"> )"
>
> So what happens is that the rule "arg"'s return template is added  
> to an
> ArrayList which then is passed to the template. However "arg" does not
> return a template as it does not need to rewrite something.

Hi Robert.  Unfortunately, ANTLR (currently) doesn't automatically  
return anything in output=template mode. I wasn't sure of correct  
answer.

> arg:
> INTEGER
> | FLOAT
> | BOOLEAN
> ;
>
> So what I need is the original text from each arg.

Just add

arg
@after { $st = {%{$text}}; }  // return template created from text of  
rule.
	:	...
	;

Ter


More information about the antlr-interest mailing list