Convert abstract syntax tree to bytecode

I am trying to learn how to build a simple compiler as a hobby. I am targeting a Java virtual machine.

I wrote a simple grammar using the ANTLR plugin for Eclipse.

Someone told me that there is something known as the node method in AST created by Antlr, and that should be called. I plan to use ASM to generate bytecode. So, what is the node method and how to call it from ASM and make it visit the method instructions?

As well as about the semantic analyzer of the compiler. Should it be written manually or are there any generators for it?

+7
source share
1 answer

Here you ask a lot of unrelated questions. Depending on the language you define, there may be a node method in your language or not, say, if your language is compiled using the main(String[]) method unconditionally.

There are several approaches to transforming AST into a target language. Basically, you will not generate the code directly, but generate the AST for your target platform and have a beautiful printer that generates code from it using a treewalker.

Semantic analysis is compiler programming. Reading and understanding input at the syntax level is parsing. You will need to write a semantic analyzer yourself, otherwise you would not write a compiler at all .; -)

I assume that you are using Jasmin to compile assembly code? A very good start would be to write grammars for your input language and target language (Jasmin) and think about which input structures would display which result. How to write a for i := 1 to 10 loop in Jasmin? Solve small problems and expand your compiler as needed, but slowly, testing recently implemented transformations early and thoroughly.

A very good reading: Let the compiler build, Jack Crenshaw .

+3
source

All Articles