Getting tree construction with ANTLR

Posted by prosseek on Stack Overflow See other posts from Stack Overflow or by prosseek
Published on 2010-06-11T18:15:18Z Indexed on 2010/06/11 18:33 UTC
Read the original article Hit count: 218

Filed under:
|
|

As asked and answered in http://stackoverflow.com/questions/2999755/removing-left-recursion-in-antlr , I could remove the left recursion

E -> E + T|T
T -> T * F|F
F -> INT | ( E )

After left recursion removal, I get the following one

E -> TE'
E' -> null | + TE'
T -> FT'
T' -> null | * FT'

Then, how to make the tree construction with the modified grammar? With the input 1+2, I want to have a tree

^('+' ^(INT 1) ^(INT 2))
. Or similar.

grammar T;

options {
    output=AST;
    language=Python;
    ASTLabelType=CommonTree;
}

start : e -> e
   ;
e  : t ep -> ???
   ;
ep : 
   | '+' t ep -> ???
   ;
t : f tp -> ???
  ;
tp : 
  | '*' f tp -> ???
  ;
f : INT 
  | '(' e ')' -> e
  ;

INT :   '0'..'9'+ ;
WS: (' '|'\n'|'\r')+ {$channel=HIDDEN;} ;

© Stack Overflow or respective owner

Related posts about tree

Related posts about antlr