Monday, November 12, 2007

The First Compiler

When I first came to know about compilers there was this question that bothered me lot - How was the first compiler compiled, being that no compiler was available to compile it ? There is another related question if you consider an interesting point, that most compilers are written in the language that are built to compile. A good example is C. The first C compiler obviously couldn't have been written in C. This sounds like a chicken and egg problem right - How did the first compiler get generated ?

The first compiler was written in the traditional way. You write a assembly program and convert it to machine code and you have a executable that can be used to compile.

Now to the other question - how do you write a compiler for a new language if the compilers are written in the language that are built to compile. Well, there are many methods for this :

  1. Write the compiler for the new language in any of the existing language.  The first Pascal compiler was actually written in Fortran.
  2. Write the compiler in a language that is actually a subset of the new language so that we can have the compiler of the subset language to compile the new language. Unlike the first case here both languages are related. The new language is derived from another language.
  3. Write the compiler in the new language and hand compile it in an un-optimized way and use this compiler to compile the original source code, thereby you actually get the optimized compiler.
  4. And finally the classic way - write the compiler in assembly language and get the machine code from it.

So do you know which is the first compiler that was built and for which language it was for ? Well The "first" compiler was created by Rear Admiral Dr. Grace Murray Hopper in 1949. That was for A-O programming language.You can know more about that here. But this compiler was closer to what we would today call a linker, I.e., a tool for combining existing libraries into a program. The name came from the fact that they 'compiled' the libraries into a single binary image.The first successful compiler was the FORTRAN I compiler first began in 1954 and finished in 1957.Modern compiler theory was developed in the early and mid 1960s, primarily out of the early ALGOL-60 compilers.