Compiler Design

Practical Compiler Construction


Designing a compiler is not a simple task. Every compiler is a tight cooperation of data structures and algorithms. You need to understand the whole process of compilation in order to properly design symbol tables, scanner, parser, internal data representation, intermediate code structure, optimization passes, and other compiler building blocks.

There are many excellent books on compiler design and implementation. Some compiler books that I recommend are listed below. However, the best book on compiler construction is the compiler itself. There are many compiler construction tools around, but they don't provide the best approach to design a fast, standalone compiler. In order to learn about practical compiler algorithms, internal data representation and to test new approaches you need a working compiler to work with. To write such a program from scratch is inevitably a time consuming task which includes reinventing the wheel.

However, to have every aspect of a compiler under total control you need to write your own compiler. And before you start writing you should check the source code of some existing compiler. There are some open-source compilers around. An additional benefit in understanding the best compiler approaches would be to have a possibility to take a look into the internal architecture of some successful commercial compiler. The most popular Pascal compiler like Turbo Pascal, for example. Luckily, you are at the right place.

Turbo PascalI am a big fan of Pascal programming language. Therefore, I was using Turbo Pascal since it was available on the CP/M platform in early 1980s. Turbo Pascal was my primary developing platform for years, until Borland introduced Delphi. My passion for Pascal programming language brought me to the decision to write my own Pascal compiler. Mainly because I got some books about compiler construction (see below) and I wanted to have a good, Turbo Pascal compatible compiler for the 8051 family of microcontrollers. My first goal was to write a Pascal compiler that would be compatible with Borland Turbo Pascal command line compiler. Compatible compiler means compatible on the source level and on the binary level - compiling the Turbo Pascal 7 source files and generating the same units and executable code. The goal was accomplished with TPC16, Turbo Pascal compiler written in Borland Turbo Pascal.

TPC16 is a complete Turbo Pascal compiler written from scratch. It is consisted of many units like Scanner, Symbol Tables, Parser, Expressions, Statements, Inline Assembler, Code Generator, OMF import, Linker, and others. TPC16 source code reveals Turbo Pascal internals showing all the secrets of a successful compiler. With it you can easily get an overview of a practical compiler implementation. TPC16 was the first step in writing the Turbo51 compiler. While the source code of the Turbo51 compiler is not available, you can get the source code of TPC16. Unfortunately, I can not offer it for free. It took me some time to write and debug it so I would like to get some compensation for this. But if you would like to write your own compiler you will definitely need some handbook on compilers. And TPC16 is like a "compiler e-book" that can answer all the questions you will be asking yourself during the compiler design. TPC16 will reveal proven data structures and internal data representation including some clever algorithms to deal with symbol tables, expressions, intermediate code, optimizations and more.

The next step is TPC32. This is the same Turbo Pascal compiler as TPC16. It is only slightly modified to compile with Delphi 7. It still generates 16-bit x86 code but the compiler is a true 32-bit applicaton. TPC32 is a great starting point if you would like to write your own compiler.