1.9. Compilers

A compiler translates a program from one language to another or from one form to another.

Some compilers generate machine language, some generate assembly language, some generate more portable code such as C code, some create abstract machine code.

Some just generate data structures that are used by other parts of a program. That is the type of compiler that you will develop.

Early compilers

The first compilers were written in the 1950s. Compiler developers had a poor understanding of what they were doing, and did everything in an ad-hoc way. The popular wisdom was that it took 30 person-years to write a compiler, even for a very simple programming language such as early versions of Fortran.

Considering the state of the art at the time, the compiler developers did a remarkably good job. Their code optimizers produced excellent machine code. That was really important because if the compilers produced poor machine code, programmers would have been much slower to adopt Fortran.

There is probably no aspect of software development that has benefited from developments of the last 50 years more than the construction of compilers.

Developments in the theory of computing have led directly to very powerful tools that not only make compiler writing much easier than it once was, but are also useful for other kinds of software.

It is now feasible to write a compiler for a small programming language as a course project.

Why study compilers?

It is useful for a computer scientist to study compiler design for several reasons.

  1. Anyone who does any software development needs to use a compiler. It is a good idea to understand what is going on inside the tools that you use.

  2. Compilers are sophisticated text processors. Most programs need to do some text processing, even if only to read in the contents of a configuration file.

  3. A useful software design technique for large projects is to develop a special-purpose language that makes the project easy to implement.

    It can take less time and effort, and lead to a higher quality product, to spend the time to develop and implement a small special purpose language and to write the software in that language than to write the software in a general purpose language.

    One example is the language Erlang, which was designed for writing software to control data switches. Erikson created a data switch all of whose software was written in Erlang. They claim downtime due to software bugs per switch in the range of milliseconds per year.

    There has been growth in the development of domain-specific languages. Studying compilers enables you to design and implement your own domain-specific language.

  4. Compilers benefit tremendously from careful analysis of a problem, and from tools for performing that analysis. A study of compiler design gives a good feeling for how a large problem can be broken down and solved in a manner that is not ad-hoc.

  5. Compiler design makes use of formal methods that are rarely seen elsewhere except when quite difficult formal methods are used for general purpose software design. The study of compilers provides a gentle introduction to formal methods.

  6. A course in compilers offers a good opportunity to get experience with development of a larger piece of software.