4

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.

2
  • See How can I build an AST using ANTLR4?. Commented Apr 15, 2014 at 13:28
  • 1
    If I want to generate just AST for more languages, than is it advisable to go with ANTLR3? Commented Apr 15, 2014 at 13:47

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Is there a good reference to build object model for a simple/mainstream language available online?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.