How to generate AST in ANTLR4?

I am working on a project in which I have to generate an abstract syntax tree for this program. Here the program can be in any major programming language. What should be the standard way to generate AST in ANTLR4? I know only the basics of ANTLR4, and I can generate a parse tree for this program.

+4
source share
1 answer

ANTLR 4 automatically generates parsing trees instead of relying on hand-crafted ASTs. This decision was made after we observed years of development using previous approaches that encountered extreme maintainability problems, especially when several tree parsers were involved.

If you need an abstract representation of the source code, you should create an object model that accurately represents the constructs in your language, and not rely on weakly typed and generally unstructured AST nodes. Then you produce parsing trees instead of AST to create your object model.

I would not recommend going with ANTLR 3 for any new project.

+2
source

Source: https://habr.com/ru/post/1536598/


All Articles