0
votes

I'm using ANTLRv3. I've defined a grammar. Now I want to display Parse Tree (like in ANTLRWorks Parse Tree or STACK). I've tried http://www.antlr.org/wiki/display/ANTLR3/Interfacing+AST+with+Java (walking throuht children) but it ommits clauses in grammar which does not appear in parsing string.

Eg. I have a SQL grammar. I'm parsing SELECT title,description from document . In ANTLRWorks I can see(in parse tree) root_statement->select_statement->select_expression->select_list->[displayed_column,displayed_colulmn] which is what I want.

But when I get the AST Tree from root_statement (through getChildren) I don't get select_statement, select_expression. The children are only for string from "SELECT title,description from document".

How can I get throught tree in the same way like in ANTLv4? (root_statement.select_statemet.select_expression)

1

1 Answers

0
votes

ANTLR 3 builds ASTs with a custom shape defined by special syntax in the grammar (operators ^, !, and ->). ANTLR 4 builds parse trees that automatically follow the shape of the grammar itself.

To make ANTLR 3 behave like ANTLR 4, you'd need to create rewrite rules for every parser rule in your grammar where the root node has the name of the rule itself. For example:

myParserRule
    :   x y* -> ^(MyParserRule x y*)
    |   z+   -> ^(MyParserRule z+)
    ;

As for the other direction, there isn't an "easy" way to make ANTLR 4 behave like ANTLR 3.