[antlr-interest] ast for pre/postfix expressions

Gavin Lambert antlr at mirality.co.nz
Thu Mar 27 02:21:45 PDT 2008


At 22:07 27/03/2008, Felix Dorner wrote:
 >prefixExpression
 >    :    prefixOp postfixExpression -> ^(PRE prefixOp
 >postfixExpression)
 >    |    postfixExpression
 >    ;
 >
 >postfixExpression
 >    :    primaryExpression ('++') -> ^(POST primaryExpression
 >'++')
 >    |    ...
 >    |    primaryExpression

That's the way I'd do it.

 >A secondary question is: Is it possible to singularize
 >alternatives using '?'  for each of the above rules? How
 >would the rewrites look?

Changing prefixExpression that way is possible, but it would make 
it an unreadable mess (in my opinion) so you'd be better off not 
doing it.

postfixExpression, on the other hand:

postfixExpression
   :  primaryExpression -> primaryExpression
      ( postfixOp -> ^(POST postfixExpression postfixOp) )?
   ;
postfixOp: '++' | '--';

(I'm actually not 100% sure of the syntax.  You might need to 
sprinkle in some $s at strategic points.  But the above should be 
close.)



More information about the antlr-interest mailing list