8

I have an ANTLR3 grammar that builds an abstract syntax tree. I'm looking into upgrading to ANTLR4. However, it appears that ANTLR4 only builds parse trees and not abstract syntax trees. For example, the output=AST option is no longer recognized. Furthermore neither "AST" nor "abstract syntax" appears in the text of "The Definitive ANTLR4 reference".

I'm wondering if I'm missing something.

My application currently knows how to crawl over the AST produced by ANTLR3. Changing it to process a parse tree isn't impossible but it will be a bit of work. I want to be sure it's necessary before I start down that road.

Alex Shesterov
  • 26,085
  • 12
  • 82
  • 103
Peter Chapin
  • 330
  • 3
  • 9
  • This question was eventually answered at http://stackoverflow.com/questions/29971097/how-to-create-ast-with-antlr4 – James Jan 22 '17 at 12:21

1 Answers1

7

ANTLR 4 produces parse trees based on the grammar instead of ASTs based on arbitrary AST operators and/or rewrite rules. This allows ANTLR 4 to automatically produce listener and visitor interfaces that you can implement in the code using your grammar.

The change can be dramatic for users upgrading existing applications from version 3, but as a whole the new system is much easier to use and (especially) maintain.

Sam Harwell
  • 97,721
  • 20
  • 209
  • 280
  • Thanks. I can understand the advantages of the new system which is why I'd like to upgrade to it. I'm willing to do what is necessary to make that happen. I just wanted to be sure I understood the situation before embarking on that effort. It sounds like I do. – Peter Chapin Apr 05 '13 at 11:04
  • Can I take `ParserRuleContext` as AST's node? – jiamo Aug 18 '15 at 07:53