3

I have a question regarding Antlr, I am building a simple parser with it but I can't traverse the tree. I have found many online tutorials and they use a getAst(); function of the Parser class. Does anyone have any experience with this? I have the feeling that the way of doing this differs per version.

grammar SimpleCalc;

options 
{
    output=AST; 
} 

tokens {
    PLUS    = '+' ;
    MINUS   = '-' ;
    MULT    = '*' ;
    DIV = '/' ;
    SEMICOLON = ';';
    EQUAL = '=';
    COMMA = ',';
    BRACKETL = '(';
    BRACKETR = ')';
}

Anyone have any idea, or suggestions on how to traverse the tree in an alternative way?

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
Fokko Driesprong
  • 2,075
  • 19
  • 31

1 Answers1

3

getAST() is a method from CommonAST which is used in ANTLR v2.x.

ANTLR v3.x uses CommonTree instead. When defining output=AST, all parser rules return an instance of RuleReturnScope which has a getTree() method you can use the get the tree.

Also see this previous Q&A that shows how the get hold of the AST after parsing some input: How to output the AST built using ANTLR?

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288