views:

178

answers:

2

Possible Duplicate:
Learning to write a compiler

I want to write a Compiler for C. This is a Project for my College i am doing as per my University. I am an intermediate programmer in C, with understanding of Data Structures.

Now i know a Compiler has the following parts: 1. Lexer 2. Parser 3. Intermediate Code Generator 4. Optimizer 5. Code Generator

I want to begin with the Lexer part and move on to Parser. I am consulting the following book: Compilers: Principles, Techniques, and Tools by Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman. The thing is that this book is highly theoretical and perplexing to me. I really appreciate the authors. But the point is i am not able to begin my project, as if i am blinded where to go. Need guidance please help.

A: 

The first stages to get working are your lexer and parser. Unless you are required to build a recursive descent parser, the most straight-forward way to get your teeth into this practically is to use a lexer-generator and parser-generator (aka compiler-compiler).

Both parser-generators and lexer-generators take a DSL in, and produce compilable C code. Lexer-generators such as flex take can tokenise regular languages, taking as input specially formatted regular expressions, and converting them into a finite-state automota in C that is capable of accepting the specified language. Parser-generators such as Bison take a LALR(1) grammar and associated actions perform on matching them as input, and produce a C program as output.

I am assuming that if you are taking a heavily theoretical course you will be able to understand the technical terminology. A detailed tutorial can help through the practicalities of using flex and bison.

fmark
I personally found writing the recursive descent parser a lot easier than using bison. It's really straight forward (if a lot of work) to write it once you have a compatible grammar, and it maps really well to actual code without the need for AST's.
Blindy
A: 

If you find the dragon book hard going I suggest you try a different book. Andew Appel's "Modern Compiler Implementation in C" is a very good alternative. I've found it very accessible and hands on. It has a homepage: http://www.cs.princeton.edu/~appel/modern/c/

What will be the target of your compiler? x86 assembler? Something else? Learning about the target architecture can be a significant undertaking when writing a compiler. Often it's not enough to just have a general compiler book, they may not describe the target you want to generate code for. Make sure you dig up some relevant material for the code generation.

Good luck!

svenningsson
Well to be very frank and absolutely honest, i want to make a very simple Compiler, nothing fancy, so i will go with x86 architecture, as my Computer itself is x86-based. I want a very basic Compiler which would just be able to Compile simple and very basic C programs which people write as students.I am not a Professional, this project is just a Dream i want to realise. No friends are supporting, all my teachers have unanimously said they can't help in this because most of them are not so knowledgeable about this, in fact my Best Friend isn't in this with me :(
strut