Search code examples
ccompiler-constructionforth

Write a compiler from scratch in C


Possible Duplicate:
How to code a compiler in C?

How would I start writing a compiler from scratch (no Flex or Bison or Lex or Yacc) in C? I have a language that I wrote an interpreter for, and it's kind of like Forth. Sort of. It takes in symbols and interprets them one at a time, using a stack.

How would I make a compiler?

That wasn't a particularly spammy bit; just to show people the syntax and simplicity.

http://github.com/tekknolagi/StackBased


Solution

  • Simple!

    1. You tokenize the input.
    2. You build a proper representation of it, generally this is an Abstract Syntax Tree, but that is not required.
    3. You perform any tree transformations you may require (optional).
    4. You generate the code by walking the tree.
    5. You link any disparate portions together (optional)

    Flex and Bison help with stage 1 and 2, everything else is up to you. If you're still stuck, I suggest going through "Programming Language Pragmatics" or The Dragon Book.