How can a C compiler be written in C? [duplicate] How can a C compiler be written in C? [duplicate] c c

How can a C compiler be written in C? [duplicate]


It's called Bootstrapping, quoting from Wikipedia:

If one needs a compiler for language X to obtain a compiler for language X (which is written in language X), how did the first compiler get written? Possible methods to solving this chicken or the egg problem include:

  1. Implementing an interpreter or compiler for language X in languageY. Niklaus Wirth reported that he wrote the first Pascal compiler inFortran.
  2. Another interpreter or compiler for X has already been written inanother language Y; this is how Scheme is often bootstrapped.
  3. Earlier versions of the compiler were written in a subset of X forwhich there existed some other compiler; this is how some supersetsof Java, Haskell, and the initial Free Pascal compiler arebootstrapped.
  4. The compiler for X is cross compiled from another architecture wherethere exists a compiler for X; this is how compilers for C areusually ported to other platforms. Also this is the method used forFree Pascal after the initial bootstrap.
  5. Writing the compiler in X; then hand-compiling it from source (mostlikely in a non-optimized way) and running that on the code to getan optimized compiler. Donald Knuth used this for his WEB literateprogramming system.

And if you are interested, here is Dennis Richie's first C compiler source.


See the Chicken and Egg section of the Wikipedia page:

If one needs a compiler for language X to obtain a compiler for language X (which is written in language X), how did the first compiler get written? Possible methods to solving this chicken or the egg problem include:

  • Implementing an interpreter or compiler for language X in language Y. Niklaus Wirth reported that he wrote the first Pascal compiler in Fortran.
  • Another interpreter or compiler for X has already been written in another language Y; this is how Scheme is often bootstrapped.
  • Earlier versions of the compiler were written in a subset of X for which there existed some other compiler; this is how some supersets of Java, Haskell, and the initial Free Pascal compiler are bootstrapped.
  • The compiler for X is cross compiled from another architecture where there exists a compiler for X; this is how compilers for C are usually ported to other platforms. Also this is the method used for Free Pascal after the initial bootstrap.
  • Writing the compiler in X; then hand-compiling it from source (most likely in a non-optimized way) and running that on the code to get an optimized compiler. Donald Knuth used this for his WEB literate programming system.


Usually, a first compiler is written in another language (directly in PDP11 assembler in this case, or in C for most of the "modern" languages). Then, this first compiler is used to program a compiler written in the language itself.

You can read this page about the history of the C language. You will see that it is also strongly linked to the UNIX system.