2

I am converting an ANTLR3 grammar to an ANTLR4 grammar, this means that I had to remove all tree rewriting rules because they now have to be implemented in code and by visiting ParseTree with a custom Visitor it's possible to generate an AST.

The old grammar had rewrite rules that prevented ambiguities and automatically generated the wanted tree. I couldn't find any useful resource online showing how this can be done with the visitor/listener paradigm.

  1. A Visitor needs a type that is used as return type for all the visit* functions. If I need to generate an AST, what return type should I use? ANTLR3 used CommonTree objects.
  2. When entering a visit* node, I can create a node of the tree, but how should I track its parent node?
  3. Is there any example on how to convert a tree rewrite rule to a method generating a node of the AST? If not, would it be possible to have an example for a rule like the one below?

Here the rule for (3):

ctor_initializer: '::'? identifier '(' expr? ')' -> ^(CTOR_INITIALIZER^(INITIALIZER_ID '::'? identifier) ^(CTOR_EXPR expr?) ); 

1 Answer 1

2

Idiomatically, Antlr4 creates and supports use of parse-trees. There is no direct support for creating and modifying ASTs.

Some of the motivating factors for preferring parse-trees over ASTs are summarized here.

Antlr4 does not preclude constructing ASTs -- the structure can be defined in any manner desired. Some examples are here and here.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the links, however (3) is not covered in the other questions because they all assume to create a new grammar instead of converting antlr3 grammar rules.
Yes. The lack of examples anywhere on the 'net along the lines of (3) gives a telling indication of how frequently that path is taken.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.