views:

237

answers:

9

Hi,

I just want to know the language in which the C compiler was written. Please say something other than C.

A: 

You have to specify which compiler.

Luca Martini
A: 

Depending on which C compiler, it was likely written in assembly, then it eventually probably became self-compiling so then parts were written in C.

You may browse the source for GCC for yourself at http://gcc.gnu.org/viewcvs/branches/

Daniel A. White
Which begs the question, what was used to write assembly?
JackN
@JackN Perhaps punch cards, etc.
ChrisW
At some point you get down to flipping switches to compose the bits of each machine instruction sent to the processor. Any lower than that and it's hardware engineering rather than programming.
Tyler McHenry
@JackN - assembly was used to write assembly. Before that, you could argue machine code - but probably even that was hand-assembled assembly. BTW - hand assembly isn't so difficult (for a simple processor) as some might think. In the 8-bit days, it was common for people who didn't have assemblers to hand-assemble little fragments to call from Basic programs. I did some of this, though that probably means I chose the wrong kind of "misspent youth". Once upon a time, I wrote simple 6502 assemblers as a kind of next step up from "hello world" for a new language.
Steve314
@Steve314 I wrote an assembler by writing a preprocessor in Basic (because a Basic interpreter was included with the O/S) and then piping it into the `a` (assemble) command of `debug.com`; I then wrote another in assembly.
ChrisW
@ChrisW - I wrote a pipe-it-through-debug assembler once too! It had to do the whole thing several times over, hoping that the addresses for labels would stabilize - took forever for anything substantial. I honestly don't remember what I wrote it in - maybe GW-Basic, not sure. I never wrote a real i86 assembler because there's too many mnemonics and addressing modes to worry about - it distracts from the point of the next-step-up-from-hello-world which is arithmetic and logic, file handling, string processing and some basic data structures.
Steve314
+2  A: 

GCC is written in C. The majority of C compilers are written in C.

There is a boot-strapping phase when first producing a compiler for a language (any language that has pretensions to be able to compile its own compiler - COBOL is one plausible exception, but there are many others) on a given platform, but once you have a compiler, then you write the compiler in that language.

All else apart, doing it in assembler is too expensive.

Jonathan Leffler
On "but once you have a compiler, then you write the compiler in that language" - it ain't necessarily so. I lot of languages *always* have their compilers/interpreters written in C, and a lot of compilers generate C rather than machine code - it's one of the reasons C gained a reputation as a kind of high level assembly language.
Steve314
@Steve: There are languages that don't pretend to be able to compile their own compiler - those languages are not covered by my statement.
Jonathan Leffler
GCC is being moved to C++ and Python has a pure python interpreter (pypy) but it's not used in actual work. Many languages can be used to implement themselves yet are not. clisp is largely written in C.
Nathon
@Jonathon - just because you write a language that's well suited to writing compilers, doesn't automatically mean you want to self-host. The whole boostrap thing is a hassle for a start. Of course it's common to do it anyway, but that doesn't prove anything. There's no rule that says you have to - only a certain amount of misplaced pride.
Steve314
+4  A: 

Nearly all major C compilers are written in C. You might think there's a chicken-and-egg problem with this, but there's not. The process is called bootstrapping.

Tyler McHenry
+6  A: 

Here's an excellent read: Reflections on Trusting Trust by Ken Thompson. Starts off with an overview of how the first C compilers were written. The boot-strapping technique to be precise. May not answer your question directly but gives you some insight.

dirkgently
+1: The ACM Turing Award paper you quote is an excellent read!
Jonathan Leffler
It's one paper I keep going back to from time to time. A personal favorite.
dirkgently
A: 

In the old days, people would write a small subset of the C language in assembler, and then use that to "bootstrap" compile a better C compiler written in C. These days it's more common to make a C compiler for a new architecture by cross compiling from an architecture that already works. I believe there are very few bits of, for instance, the gcc compiler, that aren't written in C or C++.

Paul Tomblin
+2  A: 

The very original C compiler was written (by K&R) in a predecessor language called B, or maybe BCPL. But once the C compiler was working well enough, they converted over to C and began using each successive version to compile the next.

Many of the bizarre features of C such as pre- and post-increment operators exist because (a) they represented special addressing modes on the PDP-11 on which the first C was developed, or (b) they helped the compiler fit in memory while compiling its own next version.

So that's the rest of the story.

Ian
A: 

Seems to me it would be easiest to write a compiler in perl

JackN
In person, in perspiration, in perhaps? And god forbid, in perl!
leppie
@leppie - just checking - you did intend "perl!" as opposed to "peril"? I understand they're much the same thing, but...
Steve314
per is not a good abbreviation for perl?
JackN
+1  A: 

gcc is written in C

Clang is written in C++.

Those are the two I know.

JeremyP