Fokko Driesprong
Fokko Driesprong

Reputation: 2250

Antlr Tree traversing using Java

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?

Upvotes: 3

Views: 2113

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170128

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?

Upvotes: 3

Related Questions