2

There is a question on stackoverflow about Learning to write a compiler. I have looked at it and I think it's an undertaking I want to tackle. I think (like many others) the knowledge will be invaluable to a programmer. However, my skills are primarily in C++ and I would say I am very comfortable with the syntax of the language and some basic algorithms and design concepts, but I am by no means a seasoned programmer. My only programming experience comes from academic textbooks at the college level and the completion of introductory/intermediate courses (300 level classes). Hence, the rise of my question.

With only a general knowledge of the C++ language and no Assembly knowledge, would a book aimed at the theories and workings of a compiler and the implementation of those theories, such as the book Compilers: Principles, Techniques, and Tools (2nd Edition), be difficult for me to understand?

Community
  • 1
  • 1
  • Those that are difficult to understand are ones you should read :-) – home Oct 05 '11 at 20:31
  • 1
    That is an excellent book. Worth reading.But generating a compiler end to end is more than just the compiler.You need a lot of other pieces (linker standard library stand out as big ones).Writing a compiler from scratch for a real language is not something you can expect to complete in less than a couple of years working by yourself.If you make the language simple and bolt on tools from existing projects (like C standard lib/ standard linker/ Code generator from an existing compiler (gcc/LLVM)) you may be able to build something by yourself in 6 months. An easy first step is to compile in to C – Martin York Oct 05 '11 at 23:23

3 Answers3

3

I would recommend you start with an interpreter first as you don't need proprietary hardware knowledge to implement it. The key concepts are usually how is the language defined, what makes a statement, building parse trees, etc... The hardware knowledge to me is secondary than actually understanding how to read and evaluate the statements.

What I did when learning was write a small interpreter for a Pascal like language and started small with simple statements and variable storage and slowly added different things to it as I got better.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
  • 1
    You don't necessarily need hardware-specific knowledge to write a compiler: a compiler targeting LLVM or C—or any other language really—is still a compiler. – Jon Purdy Oct 05 '11 at 22:47
  • @JonPurdy Yeah I know, I just sort of wanted to get the point across that all the low level or even translating one language to another to then compile isn't necessary to learn about how compilers work – Jesus Ramos Oct 06 '11 at 00:26
1

Go ahead - it's a great way to learn. Often, you will need to try out techniques given in the book before you really understand them, but after you go through the exercise you should be a better programmer overall.

Bryan
  • 11,398
  • 3
  • 53
  • 78
0

You will need to learn some kind of assembly language (or byte code) in order to translate a compiled program into something that can actually be executed.

For C(++) in particular you'll also need a copy of the C(++) standard(s).

You can make a few short cuts like interpreting instead of compiling fully or translating into a different language and compiling that with a different compiler.

Also, you should consider first making a compiler for a small or dumbed down language like Forth, Basic or so-called small C (not supporting all types, constructs and keywords and lacking most of the standard library).
I can estimate that it would take me, a fairly versed C programmer, who knows x86 well, a few months (~3) to make a working small C-ish compiler. Just that, minimal standard library, no optimizations, minimal error checking (that is, not generating all the useful warnings). This is to give you an idea of how long things can take even in not so complex cases. Making quickly something small can be more rewarding.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • 2
    No, you don't need assembly to do anything. Compilation is a process of translating from one language to another, not from a language to a specific assembly language. Also, you won't be able to write C++ compiler as your first one. – Cat Plus Plus Oct 05 '11 at 21:47
  • A good first step in language development is to build a proof of concept that compiles your language into C then invokes the local C compiler to generate the executable. – Martin York Oct 05 '11 at 23:31