views:

966

answers:

9

It is a university task in my group to write a compiler of C-like language. Of course I am going to implement a small part of our beloved C++.
The exact task is absolutely stupid, and the lecturer told us it need to be self-compilable (should be able to compile itself) - so, he meant not to use libraries such as Boost and STL.
He also does not want us to use templates because it is hard to implement.
The question is - is it real for me, as I`m going to write this project on my own, with the deadline at the end of May - the middle of June (this year), to implement not only templates, but also nested classes, namespaces, virtual functions tables at the level of syntax analysis?
PS I am not noobie in C++

+27  A: 

Stick to doing a C compiler.

Believe me, it's hard enough work building a decent C compiler, especially if its expected to compile itself. Trying to support all the C++ features like nested classes and templates will drive you insane. Perhaps a group could do it, but on your own, I think a C compiler is more than enough to do.

If you are dead set on this, at least implement a C-like language first (so you have something to hand in). Then focus on showing off.

rjh
When I took compilers, we added features as we could on our project. Of course, I was the only person in my group with any assembler experience, so we didn't get as far as we might have.
David Thornley
+1  A: 

Namespaces or nested clases, either virtual functions are at syntax level quite simple, its just one or two more rules to parser. It is much more complicated at higher levels, at deciding, which function / class choose (name shadowing, ambiguous names between namespaces, etc.), or when compiling to bytecode/running AST. So - you may be able to write these, but if isn't necessary, skip it, and write just bare functional model.

Yossarian
+18  A: 

"The exact task is absolutely stupid" - I don't think you're in a position to make that judgment fairly. Better to drop that view.

"I`m going to write this project on my own" - you said it's a group project. Are you saying that your group doesn't want to go along with your view that it should morph into C++, so you're taking off and working on your own? There's another bit I'd recommend changing.

It doesn't matter how knowledgable you are about C++. Your ability with grammars, parsers, lexers, ASTs, and code generation seems far more germane.

Without knowing more about you or the assignment, I'd say that you'd be doing well to have the original assignment done by the end of May. That's three months away. Stick to the assignment. It might surprise you with its difficulty.

If you finish early, and fulfill your obligation to your team, I'd say you should feel free to modify what's produced to add C++ features.

I'll bet it took Bjarne Stroustrup more than three months to add objects to C. Don't overestimate yourself or underestimate the original assignment.

duffymo
Part of the educational experience there is working in a group. Something that you will have to do in real life. I find that other people have good ideas too, when I have enough wit to listen.
EvilTeach
Exactly, EvilTeach. Well said.
duffymo
+1  A: 

If you are talking about a complete compiler, with code generation, then forget it. If you just intend to do the lexical & syntactic analysis side of things, then some form of templating may just about be doable in the time frame, depending on what compiler building tools you use.

anon
+4  A: 

Follow the assignment. Write a compiler for a C-like language! What I'd do is select a subset of C. Remove floating-point datatypes and every other feature that isn't necessary in building your compiler.

Writing a C compiler is a lot of work. You won't be able to do that in a couple of months. Writing a C++ compiler is downright insane. You wouldn't be able to do that in 5 years.

jalf
+6  A: 

No problem. And while you're at it, why not implement an operating system for it to run on too.

j_random_hacker
+2  A: 

You should be able to complete c-like language within the time frame. Assuming you are taking more than 1 course, that is exactly what you might be able to do in time. C++ is also doable but with a lot more extra hours to put it. Expecing to do c++ templates/virtual functions is overexpecting yourself and you might fail in the assignment all together. So it's better stick with a c subset compiler and finish it in time. You should also consider the time it takes for QA. If you want to be thorough QA itself will also take good time.

+2  A: 

For a class project, I think that requiring the compiler to be able to compile itself is a bit much to ask. I assume that this is what was meant by stupid in the question. It means that you need to figure out in advance exactly how much of C you are going to implement, and stick to that in building the compiler. So, building a symbol table using primitives rather than just using an STL map. This might be useful for a data structure course, but misses the point for a compiler course. It should be about understanding the issues involved with the compiler, and chosing which data structures to use, not coding the data structures.

Building a compiler is a wonderful way to really understand what happens to your code once the compiler get a hold of it. What is the target language? When I took compilers, it took 3 of us all semester to build a compiler to go from sorta-pascal to assembly. Its not a trivial task. Its one of those things that seems simple at first, but the more you get into it, the more complicated things get.

KeithB
+3  A: 

I will like to stress a few points already mentioned and give a few references.

1) STICK TO THE 1989 ANSI C STANDARD WITH NO OPTIMIZATION.

2) Don't worry, with proper guidance, good organization and a fair amount of hard work this is doable.

3) Read the The C Programming Language cover to cover.

4) Understand important concepts of compiler development from the Dragon Book.

5) Take a look at lcc both the code as well as the book.

6) Take a look at Lex and Yacc (or Flex and Bison)

7) Writing a C compiler (up to the point it can self compile) is a rite of passage ritual among programmers. Enjoy it.

Nikhil