How was the first C compiler written?

CAssemblyCompiler Construction

C Problem Overview


Is it true that the first C compiler was written in C itself? Then, how was it executed and compiled? Or, was this compiler written in assembly language?

C Solutions


Solution 1 - C

It is described pretty well in Dennis Ritchie's writeup of the C language history.

Giving just a summary of what he wrote there, use his article for the finer details. C started with the BCPL language, Ken Thomson had access to a compiler for it that ran on their General Electrics 635 main frame. Unhappy with the language, Thomson used BCPL to write a compiler for the B language, an evolutionary step beyond BCPL that removed some of the technical problems in BCPL.

They used B to create programs on their PDP-7 minicomputer, although most of them were little more than toy programs, the machine had very restricted hardware capabilities. One important step taken by Thomson was to rewrite the B compiler in B itself. A common bootstrapping step.

That compiler was then gradually tweaked, through a short-lived intermediary step called NB (New B) to start resembling C. The PDP-11 minicomputer was instrumental in that step, giving them enough room to improve the language and compiler.

Solution 2 - C

The first C compiler wasn't written in C, usually when writing a compiler we use either assembly language, or another programming language, and it's common that after first compilation, the compiler is rewritten in it's native language.

There's a lot of programming languages that have been written in C then rewritten in their native language : Java for example, Ada ...

Solution 3 - C

Reading Dennis Ritchie's note on primevalC indicates that, rather like the chicken and the egg, C evolved from a precursor language along with its own compiler via bootstrapping. The speciation timescales were quicker than for Gallus gallus domesticus.

One can even see the compiler source code caught in the midst of the evolutionary turmoil. The note links to two snapshots of it restored from tape backups. This code has also been placed on github. These snapshots constitute intermediate fossils, years before K&R syntax. Dennis' description of one of the tapes shows it is a freeze-frame of structs being bootstrapped:

> "prestruct-c" is a copy of the compiler just before I started changing > it to use structures itself.

In other words the compiler had been augmented to support structs but didn't yet use them. A good time to make a tape backup...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJaskaran S.PView Question on Stackoverflow
Solution 1 - CHans PassantView Answer on Stackoverflow
Solution 2 - CBelkacem REBBOUHView Answer on Stackoverflow
Solution 3 - CegyikView Answer on Stackoverflow