Question

i want to design a compiler for a 'new' language.The new language will have it's own syntax and outputs a valid C code.that is, generate a c code from pseudo code.I had designed a grammar.perform recursive descent parsing and got the abstract syntax tree.for example i got a tree for if structure .How can i map this to original c code? Can i perform in-order traversal or something?

i had read Aho Ullman book.What is the difference between the execution of interpreter and compilation? Can i apply visitor pattern?

Was it helpful?

Solution

The same way you would map it to machine code, but output C instead. That will make some things easier (you can leverage the C compiler's symbol tables rather than building your own) and some things harder (since you have to express things in a way that C will accept).

Note that in most cases you will need to convert the abstract syntax tree to a concrete semantic tree, unless the two are nearly identical in your language.

An interpreter runs only over the portions of the semantic tree which are actually executing, and may run over those repeatedly; it's also manipulating data in the interpreter's memory model. A compiler has to run over the entire semantic tree, once, and generate all code which could possibly execute; in general it also have to generate a full memory model but as I say outputting C will let you partly cheat on that.

The dragon book has more detailed answers for these questions. Basically, you'll be treating C as a "high-level assembler".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top