How to code a compiler in C?

13,609

Solution 1

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.

Solution 2

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

Share:
13,609
Hick
Author by

Hick

Updated on June 04, 2022

Comments

  • Hick
    Hick about 2 years

    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?

  • Hick
    Hick over 14 years
    i want to code my own lexer and parser ..!! and not use any sort of tool !!
  • Hick
    Hick over 14 years
    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 ?
  • Peter Wone
    Peter Wone over 14 years
    So, you want to build a Jumbo Jet, only bigger and better, using only the pieces in the basic Meccano set?
  • Hick
    Hick over 14 years
    yup !! so you can understand my problem ,so please help me out
  • Niki
    Niki over 14 years
    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.
  • Joe
    Joe over 14 years
    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.