47

Possible Duplicate:
How could the first C++ compiler be written in C++?

I know my question goes to the underground galaxy cave where languages are born and involves some lambda math and light-years of google-studying. But what kind of knowledge is necessary to create a language?

H_7
  • 559

4 Answers4

85

Look up "bootstrapping".

Basically you start with a very minimal process/set of functions that can be used to compile the code that defines a slightly more functional compiler. This creates your next compiler which then can then be used to build code that can do even more. You repeat this process until you have a full blown compiler that can compile all the language features.

The other alternative is to write the first version of the compiler in a different language and then write the next version in your target language.

ChrisF
  • 38,948
  • 11
  • 127
  • 168
25

ChrisF's answer is excellent, but I wanted to add this example that always stuck by me after my computer science course on bootstrapping.

Suppose you have a basic C compiler that does not support escape codes for strings yet, and you wanted to add that. You could add a snippet of code similar to this:

if( str[i] == 0x5c ) {       // ASCII code for backslash
   switch( str[i+1] ) {
      case 'n': return 0x0a; // ASCII code for new line
      case 't': return 0x09; // ASCII code for tab
      // ...                 // more ASCII code for other escapes
      default: return str[i+1];
   }
}

After you have added this to the compiler, and generated a new compiler binary, you could then rewrite this into:

if( str[i] == '\\' ) { 
   switch( str[i+1] ) {
      case 'n': return '\n';
      case 't': return '\t';
      // ...
      default: return str[i+1];
   }
}

That would remove any knowledge about ASCII codes from the compiler source code, but the compiler would still magically generate the correct codes.

fishinear
  • 413
19

Bootstrapping is definitely the standard way to build a compiler today. But remember that you don't need a compiler or interpreter to write a program in a language. For instance, Christopher Strachey wrote a famous AI program that was able to play Checkers in CPL before there was a compiler for CPL. He had to translate the program to machine code "manually", which is tedious and error-prone, but not really difficult (that's why computers can do it so well).

nikie
  • 6,333
10

I hope this is not out of topic, but I wanted to point out that, once you have one C compiler for one platform X, bootstrapping for other platforms can be done by using cross-compilation:

  • You have a C compiler c1 for architecture X that runs on architecture X.
  • You write a C compiler c2 for architecture Y, written in C.
  • You compile the compiler c2 on X using c1 and obtain the binary for compiler c2 that runs on X.
  • You use the binary for c2 that runs on X to compile itself and obtain a binary of c2 that will run on Y.

In other words, when you have the first egg, it is easy to make more eggs.

Giorgio
  • 19,764