[antlr-interest] ast for pre/postfix expressions

Felix Dorner felix_do at web.de
Thu Mar 27 02:07:21 PDT 2008


Hey,

I am currently creating AST's for pre and postfix expressions, like in Java:

a++
++a

my current grammar looks like that:

prefixExpression
    :    prefixOp^?  postfixExpression;

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

The problem is that this creates identical trees for the two examples 
above. Since this is the first time I really work with AST construction 
I am not sure how to solve this problem. Should I just introduce two 
imaginary tokens PRE and POST, and change the grammar to this:

prefixExpression
    :    prefixOp postfixExpression -> ^(PRE prefixOp postfixExpression)
    |    postfixExpression
    ;

postfixExpression
    :    primaryExpression ('++') -> ^(POST primaryExpression '++')
    |    ...
    |    primaryExpression


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

Thanks for your suggestions,

Felix











More information about the antlr-interest mailing list