tags:

views:

139

answers:

4

I am coding a compiler in C, and I have read all about compilers in the Dragon book. But I am finding it really hard to implement, and I have no clue as to where to start. Even when it comes to lexer part, so would love to know a step by step method on the basis of code writing to write a compiler in C!!

What would you suggest I do next?

+5  A: 

This post might help:

http://stackoverflow.com/questions/1669/learning-to-write-a-compiler

o.k.w
+3  A: 

Most people user specialized parser- and lexer-generating tools like ANTLR or Yacc/Bison with Lex.

Chuck
i want to code my own lexer and parser ..!! and not use any sort of tool !!
mekasperasky
At least ANTLR produces more or less readable code. Looking at the generated lexer might give you good ideas about how to build your own one.
nikie
No tools? Guess you're going to be hand-entering bytes directly into memory then vs. using any kind of "compiler" to build this compiler.
Joe
A: 

http://compilers.iecc.com/crenshaw/

Jherico
+1  A: 

You could look at Appel's Modern Compiler Implementation in C.

By the sounds of it you need to work out what language you want to compile: do you want a subset of C say or a easy to parse language like Scheme, or just an arithmetic expression language?

Pick/design a language, write a couple of really small programs in it, write a lexer/parser for part of it, then the back to get parts working (possibly interpreted to start - just so you can see it running) and then iterate chunks that seem interesting, building up to the full language.

Edit based on extra details supplied

"i want to make a super set of c , like implementing various advantages of python , but keeping it as simple as c"

I'm not sure I'd do that by writing everything by hand but if I did ...

I'd write out some programs in the hybrid language that I want to end up with: so if you want C with Python like list comprehensions then maybe

void main()
{
    int[] x = {1,2,3,4,5};
    int[] y = {i*i for i in x where i % 2 == 0};
    for (int i in y) { printf("%d", i); }
}

[C style arrays that include their count as implied above left as exercise for the reader :-) !]

Then get an absolutely minimal C program working, hello world or even just adding some numbers statically (if it was hello world I might even start by special casing printf so I didn't have to parse stdio.h - if you're heading towards a C-Python hybrid you may end up keeping that). Once you could do

void main() 
{
    int x = 0; 
    int y; 
    y = 5; 
    x + y;
}

You can start adding complexity: arbitrary function definitions and calls, more operators, return values, arrays, data structures, const, pointers, ... building towards the simplest of the example programs step by step.

The advantage of starting with the C subset is you have lots of C compilers you can look at for ideas so you get going e.g. TinyCC so by the time you get to the difficulties of adding python-esque pieces you've got a solid base.

This is skating over a lot of details on a long road. Good luck.

Ian G
i want to make a super set of c , like implementing various advantages of python , but keeping it as simple as c . I really liked your last para , and would like to develop the compiler in that way , can you describe little more about that method ?
mekasperasky
So, you want to build a Jumbo Jet, only bigger and better, using only the pieces in the basic Meccano set?
Peter Wone
yup !! so you can understand my problem ,so please help me out
mekasperasky