How can I build an AST using ANTLR4? [duplicate]
Asked Answered
R

1

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.

Riyal answered 4/4, 2013 at 23:15 Comment(1)
This question was eventually answered at #29971597Nodal
T
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.

Trujillo answered 4/4, 2013 at 23:48 Comment(2)
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.Riyal
Can I take ParserRuleContext as AST's node?Thaothapa

© 2022 - 2024 — McMap. All rights reserved.