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

Robert.Klaus at innovations.de Robert.Klaus at innovations.de
Thu Oct 25 01:02:04 PDT 2007


Hi Terence,

Thanks for the suggestion! However I have a different solution, as I
only need to replace the text for ID. My approach is ... ehm... Hacky
but I think I'll post it anyway, as it might help someone with a similar
problem.

I definend a method in the @members section:

private void replace(CommonTree tree, String text)
    {
      ((TokenRewriteStream)input.getTokenStream()).replace(
         input.getTreeAdaptor().getTokenStartIndex(tree),
         input.getTreeAdaptor().getTokenStopIndex(tree),
         text);
    }

It directly alters the TokenRewriteStream.

And then I was able to do a replace within an Action.

^(id=ID args+=arg*)
{
	replace($id, translateToNewName($id.text));
}

Bye,

Robert


> -----Original Message-----
> From: Terence Parr [mailto:parrt at cs.usfca.edu] 
> Sent: Thursday, October 25, 2007 1:51 AM
> To: Klaus Robert
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] How to use arg* when rewriting 
> with StringTemplate
> 
> 
> 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