[antlr-interest] AST for repeated occurrences of binary operators

Gavin Lambert antlr at mirality.co.nz
Wed Nov 25 02:01:37 PST 2009


At 17:24 25/11/2009, David-Sarah Hopwood wrote:
 >Jim Idle wrote:
 >> expr : ae+=andExpr (OR ae+=andExpr)* -> ^(OR $ae+) ;
 >
 >That isn't what the poster asked for: it will produce '(OR a)'
 >when the input is an andExpr, whereas what I posted above
 >should produce just 'a', if I haven't made a mistake.

I think the += based equivalent is this:

expr
   : (andExpr -> andExpr)
     ((OR ae+=andExpr)+ -> ^(OR $expr $ae+))?
   ;

A fair bit harder to read, although it can be expanded to do 
weirder things a bit easier. :)



More information about the antlr-interest mailing list