[antlr-interest] Parser - > Tree walker

CARLOS MELGAREJO carlos.melgarejo at rogers.com
Tue May 22 10:10:26 PDT 2007


  Hi all,

  I'm trying to create a grammar that recognize simple expressions and other goodies and generate an evaluation at the end.

  First I read "The definitive ANTLR Reference" book(for the version 3) and I got all exited, and then after a lot of testing examples I was encourage to start working in my project.

  So, making the long story short this is my Lexer-Parser grammar look like:

grammar Expert;
options {
    output=AST;
    ASTLabelType=CommonTree;
}

// Starting point
statement : expression  ;  

expression  : conditionalAndExpression ( 'or'^ conditionalAndExpression )*  ;

conditionalAndExpression : exclusiveOrExpression ( 'and'^ exclusiveOrExpression )* ;

exclusiveOrExpression : equalityExpression ( 'xor'^ equalityExpression )* ;

equalityExpression : relationalExpression (('='^ | '!='^) relationalExpression)*  ;

relationalExpression : additiveExpression ( ('<='^|'>='^|'<'^|'>'^) additiveExpression )* ;        

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

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

unaryExpression  :  '+'^ unaryExpression 
                          |  '-'^ unaryExpression
                          |  unaryExpressionNotPlusMinus
                          ;
  
unaryExpressionNotPlusMinus
    : 'not'^ unaryExpression
    | atom
    ;     
    
atom
    : literal  
    | log 
    | Date 
    | '('! expression  ')'!
    ;

literal    
    :   FloatingPointLiteral  
    |   StringLiteral         
    ; 
......................

  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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070522/917d4c42/attachment-0001.html 


More information about the antlr-interest mailing list