[antlr-interest] Tree Operators vs. Tree Rewrite Rules

Simon cocoa at gmx.ch
Sat Oct 25 14:36:49 PDT 2008



On Oct 25, 2008, at 12:40 PM, Gavin Lambert wrote:

> At 22:57 25/10/2008, Simon wrote:
> >Is it so obvious? Or is it simply not possible to achieve
> the same thing using rewrite rules?
>
> It is possible, and it's not quite so obvious (though it has been  
> mentioned a few times in the archives).  But messages sometimes slip  
> through the cracks :)
>

Yep, RTFM... http://www.antlr.org/wiki/display/ANTLR3/Tree 
+construction at the top...

> >> ^('.' ^('.' 'a' 'b') 'c')
> [...]
> >> expr
> >> 	:	primary ('.'^ ID )*
> >> 	;
> [...]
> >> However, I can't get the same tree using tree rewrite
> >> rules. Is it possible at all? If so, could somebody
> >> please show me how?
>
> expr
>  :  (primary -> primary)
>     ('.' ID -> ^('.' $expr ID))*
>  ;

Ok, thank you very much, that did it.

The answer leads to the next question. I've tried to split the rule  
into an additional selector subrule, but without success.

expr
     :    primary selector*
     ;

selector
     :    '.' ID
     |    '[' expr ']'
     ;

I went one step further and added array access to the selector (to  
make the example a bit more realistic).

My head is starting to spin from all those tree rewrite rules. If I'm  
again missing the obvious, please just send me a link to the correct  
information. This time, I've at least searched the list archive and  
read the manual... Or even better, if somebody has a tree grammar for  
Java expressions (that builds the tree as laid out in my original  
post), could you please send it to me?

Simon



More information about the antlr-interest mailing list