1

I am developing a compiler and have already implemented lexer, parser and semantic analyzer(using listener and visitor) using ANTLR4. For code generation I am planning to generate LLVM IR using StringTemplate(ST). To do so I am thinking of first constructing an AST and then generating the code.

My question here is do I need to construct AST?? or can I use Parse Tree? If I need to use AST, I am not able to find any examples of manually constructing AST using visitors or listeners. Even a small grammar example will be very helpful.

Thank you.

NewGirl
  • 101
  • 1
  • 4

1 Answers1

2

No, there is no fundamental need to construct an AST. In the simplest case, you can walk the parse-tree and output the IR, directly or using ST.

Where transformations are required for output as IR, the two basic approaches are to (1) analyze and annotate the parse-tree describing the necessary changes; or (2) walk the parse-tree, constructing a separate AST, and then walk and transform the AST.

For the annotation strategy, extend ParseTreeProperty to create context node type specific property classes. See the comment in that class for how to use.

The AST strategy is not discouraged -- it was the primary strategy used in Antlr3 -- but is essentially unsupported in Antlr4. As for why Antlr4 favors the annotation strategy, see the last few paragraphs of this answer.

Community
  • 1
  • 1
GRosenberg
  • 5,843
  • 2
  • 19
  • 23
  • Thanks a lot for your reply. I could output the IR by walking parse tree for simple constructs like binary operations. I am not able to handle Inheritance that well(base class function calls from derived class / function overriding). Have you seen any such projects online? An example would be of great help. Thank you very much. – NewGirl Oct 19 '15 at 15:03
  • I know of no such projects. Perhaps best to post a new question, with illustrative details, for how to analyze and resolve the parse-tree structure suitable for output. – GRosenberg Oct 19 '15 at 20:44