[antlr-interest] Parser - > Tree walker

Lucio Biondi lbiondi at olijo.com
Tue May 22 10:22:58 PDT 2007


>   So far so good, I'm able to create a pretty good AST, the only problem that I'm facing with this grammar is that I build the following tree node:
>  ^('-' expr expr)     // Arithmetic expression 
>  ^('-' expr)            // Unary expression.
> 
>    My problem is that I don't know how to walk this tree, because of the ambiguity in the rule abode created with '-'. 
> 
>    How I can solve the problem with unary expressions?. 
>    Thanks in advance.
> 
> 

I guess It would be better if you try to resolve the ambiguity in the parsing phase
with an imaginary token (UNARY).

additiveExpression
    :	multiplicativeExpression (('+'^ | '-'^) multiplicativeExpression)*
    ;

multiplicativeExpression
   :	unaryExpression (('*'^ | '/'^ | '%'^ | '**'^) unaryExpression)*
    ;

unaryExpression
    :	'+' unaryExpression
    |	'-' unaryExpression -> ^(UNARY unaryExpression)
    |	unaryNotExpression
    ;

Lucio


More information about the antlr-interest mailing list