Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to code a compiler in C?

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?

like image 767
Hick Avatar asked Jan 24 '23 01:01

Hick


1 Answers

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.

like image 177
Ian G Avatar answered Jan 31 '23 12:01

Ian G