Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Abstract Syntax Tree to Byte code

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

I have written a simple grammar using ANTLR plugin for Eclipse .

Someone told me that there is something known as a method node on the AST generated by Antlr, and that has to be called. I am planning to use ASM to generate the bytecode. So what is the method node and How do I call it from ASM and make it visit method instructions?

Also what about the semantic analyzer of a compiler. Should that be manually written or are there any generators for it?

like image 660
Gautam Avatar asked Jul 17 '11 06:07

Gautam


1 Answers

You ask many unrelated questions in here. Depending on the language you define, there may be a method node in your language or there won't be any, say, if your language compiles to a main(String[]) method unconditionally.

There are multiple approaches to transform an AST to a target language. Mostly you would not generate code directly, but generate an AST for your target platform and have a pretty printer generate code out of it, using a treewalker.

The semantic analysis is the programming of a compiler. Reading and understanding the input on a syntactically level is the parsing. You will need to write the semantic analyzer on your own or you would not have written a compiler at all. ;-)

I presume you use Jasmin to compile the assembly code? A very good start would be writing grammars for your input language and the target language (Jasmin) and think about, which input structures would render what output. How would one write a for i := 1 to 10 loop in Jasmin? Tackle small problems and expand your compiler as needed, but slowly, testing newly implemented transformations early and thoroughly.

A very good reading: Let's Build a Compiler, by Jack Crenshaw.

like image 179
kay Avatar answered Sep 23 '22 06:09

kay