-1

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?

1 Answers1

2

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".

keshlam
  • 7,931
  • 2
  • 19
  • 33
  • my doubt is how to evaluate an ast? – user3217708 Mar 07 '14 at 05:48
  • Only you know what your intended semantics are for each component of the syntax, since you're inventing the language. You need to map the syntax into a form that represents the program's intended behavior, in a manner which is sufficiently direct that you can then go from that to either interpretation or compilation. – keshlam Mar 07 '14 at 05:52
  • In my old copy of Aho/Ullman, your questions start at Chapter 7, "Syntax-directed translation". Use the AST to produce an IL (intermediate-code) representation. Chapters 8-11 provide additional details on that process. You can initially ship chapters 12-14, which are about code optimization. Chapter 15 covers how to use the IL to generate code; that's the one you would adapt to output C instead of assembler or a binary object-code file. – keshlam Mar 07 '14 at 05:58
  • If you were writing an interpreter, it would run against the IL. – keshlam Mar 07 '14 at 06:00