user2597000
user2597000

Reputation: 161

How to generate AST in ANTLR4?

I'm working on a project in which I have to generate Abstract Syntax Tree for a given program. Here program can be in any mainstream programming languages. What should be the standard way of generating AST in ANTLR4? I know only basics of ANTLR4 and I'm able to generate Parse tree for a given program.

Upvotes: 4

Views: 3279

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 100029

ANTLR 4 automatically generates parse trees instead of relying on manually-structured ASTs. This decision was made after observing years of development with prior approaches encountering extreme maintainability challenges, especially when multiple tree parsers were involved.

If you need an abstract representation of your source code, you should create an object model that accurately represents the constructs in your language, rather than rely on weakly typed and generally unstructured AST nodes. You then walk the parse trees instead of ASTs to create your object model.

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

Upvotes: 2

Related Questions