2
votes

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

1 Answers

2
votes

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.