1

As part of the programming assignments of our compilers related class. I've proposed to my teacher to use ANTLR instead of flex bison and here he ask me to ensure that it does all what we want, i.e lexical, syntactical and semantic analysis (thre first three steps in the image below) which I'm pretty sure that it's very easy to build such analyzer using ANTLR. but also the intermediate and object code generation phase of compilers (rest of the phases in the image below) and this is really confusing me. I've expected that such tool have to give users facilities to do such generations but I didn't find anything that explains how to do that even in the reference book written by the author.

enter image description here

Can anyone here explain to me how to do so or just point me to the write material that can help me to convince my teacher.

Haroun Mohammedi
  • 2,404
  • 12
  • 25
  • This is a very broad question. Basically you want to know how to build a compiler with ANTLR as a major component? Very ambitious. I built a Z80 assembler using ANTLR but would not want to undertake building a compiler with it. – TomServo Oct 27 '17 at 13:58
  • @TomServo I've just edited my question so that it would be somehow specific – Haroun Mohammedi Oct 27 '17 at 14:06

1 Answers1

1

The short answer is that ANTLR is well-suited for compiler implementation, and at least a functional equivalent of YACC/Bison for this purpose.

Specific to your question, ANTLR provides Lexer (lexical analysis), Parser (syntactic analysis), and tree-walker (semantic analysis) support, all with appropriate forms of error listener and recovery mechanisms. An example symbol table is in the github repo.

Multiple tree-walks are typically used in semantic analysis. A final walk can be used to output some intermediate representation (IR) of the code.

Look at the IR language, optimizers, and code generators provided by the LLVM project to understand what is involved in the remaining steps.

Prof. Parr's books Language Implementation Patterns and TDAR will help.

GRosenberg
  • 5,843
  • 2
  • 19
  • 23
  • are you saying that the developper have to generate the intermediate code on his own when walking the tree and this is not done by the parser generator tool ? – Haroun Mohammedi Oct 27 '17 at 22:11
  • Yes, this is true for ANTLR, YACC/Bison, and all other similar parsers. Semantic analysis of the parse tree (or AST for others), in combination with a symbol table, is the responsibility of the developer. For the actual rendering of the IR, use ANTLR's companion library [StringTemplate](http://www.stringtemplate.org/). – GRosenberg Oct 27 '17 at 23:38
  • can you please point me to a start guide to use `StringTemplate` to generate intermediate and object code – Haroun Mohammedi Nov 05 '17 at 21:59