views:

428

answers:

6

I was just listening to episode 57 of Software Engineering Radio (TRANSCRIPT: http://www.se-radio.net/transcript-57-compiletime-metaprogramming ) I'm only 40 minutes in, but I'm wondering why C is the language of compilers- when a Scheme subset would seem to be a better fit? (or some other HLL) (excluding the obvious reason of not wanting to rewrite gcc)

PS originally posted this at LtU http://lambda-the-ultimate.org/node/3754

+2  A: 

Well, one reason will be the issue of bootstrapping the compiler on unsupported architectures. That will usually require the existence of a working compiler for that architecture, which generally means C. I remember trying to compile MIT-scheme from source, and getting really pissed off that it required MIT-scheme to be installed before I could build MIT-scheme.

Incidentally, I'm not sure I agree with your premise... C certainly seems to be the most widely deployed language, but other language compilers (e.g. MIT-scheme) are often implemented in those languages.

Aidan Cully
+6  A: 

I won't bother to listen to 40 minutes of radio to perhaps understand your question more thoroughly, but I would claim the exact opposite. Only a minority of compilers are written in C. I rather have the impression that (at least where appropriate), most compilers are implemented in the language they are meant to compile.

jarnbjo
I don't have stats to refute you, but it seems unlikely because of the chicken-and-egg problem: With no existing compiler in that language available, how do those compilers ever get built on new architecture?
Carl Smotricz
@Carl Smotricz: Well, the ubiquitous C compiler gcc is written in C. Similarly the Erlang compiler is written in Erlang. And the Lisp eval function, which IS the Lisp compiler was written in Lisp when Lisp was merely a theory until a grad student looked at the code and realised he could transliterate most of it directly in assembly. So I guess the first practical Lisp compiler was compiled by a human.
slebetman
It's called "bootstrapping" and it's mentioned in the most fundamental of compiler textbooks, the dragon book. Basically you implement a small part of a language compiler in another language first, then use the new language to build the next compiler for that langauge (with more language features) and iterate.How do you think the first compilers (or even assemblers) were written? In machine code of course. Same goes for the first editors and other utilities.
Daemin
@Carl - see http://en.wikipedia.org/wiki/Bootstrapping_%28compilers%29
Daniel Earwicker
As far as I know, first versions are normally written in other languages (such as plain assembler)
Álvaro G. Vicario
"a minority of compilers are written in C". Counting compilers might be misleading anyway. The vast majority of compilers are probably Scheme or ML compilers, written by CS undergraduates learning Scheme or ML ;-)
Steve Jessop
Any language who's origins I've ever heard of was first implemented as a <language>-to-c translator. The real compiler for <language>-to-machine-code came as a second step using the c-translator.
Alan
@Alan: Lisp, Pascal, FORTRAN and COBOL were all compiled languages whose original compiled implementations did not use C. Of course, in 3 of 4 cases C didn't exist at the time. For example the first Lisp compiler was written in Lisp, and bootstrapped using a Lisp interpreter written in machine code. I remember when all this was fields, you know (well, I don't, but I've read about what it was like when all this was fields...)
Steve Jessop
More recently, I don't believe that the first Java JIT was initially written as a Java-to-C translator. I could be wrong, though, and I guess you could argue that a JIT isn't a "proper" compiler.
Steve Jessop
@Steve: A JIT is a dynamic compiler, as opposed to traditional static compilers. For dynamic fans, dynamic compilers are even more of a "real compiler" than static compilers, because of the greater opportunities for optimisation.
Chris Jester-Young
Sure, I'm just trying to speculate what Alan actually means by "any language whose origins I've ever heard of". Obviously he's deliberately excluding languages all of whose implementations are interpreted, since of course they don't translate to C. So I don't know whether it's reasonable or not to raise Java as a counter-example alongside Lisp etc. He may be aware of its origins, but discounted it deliberately, rather than just having forgotten it or been unaware of its origins (like Lisp etc). In other contexts I'm entirely with you, a JIT is a proper compiler.
Steve Jessop
+5  A: 

C need not be the language for compilers, but it does have some advantages. C is available on almost all platforms and that makes it easy to port and bootstrap the compiler. C is closer to the hardware and makes possible many optimizations that will be difficult to achieve in other languages. It is easy for a compiler written in C to co-exist with other languages, libraries and systems as most of them provide a C interface. It is also easy for others to extend the compiler as C is the Esperanto of system programmers.

Vijay Mathew
+3  A: 

It's probably a combination of factors:

  1. C compilers are available for almost every platform, making it easier to build a new compiler for a new language.
  2. History: C is a very popular language, so it makes sense that a lot of projects are in C (no matter the project).
  3. Scheme, specifically, is very unpopular (compared to C).
Edan Maor
If we accept (2), why would anyone write a compiler for a different language in the first place? Also if they write a compiler, we can assume they know more than just one language, so (3) doesn't sound like a convincing reason either.
nikie
we can accept 2, and not remove all other compilers. I don't undersand your logic. The Beatles are popular, that doesn't negate the need and desire for other musicians.
Matt Ellen
@nikie: Just because a language is popular doesn't mean others aren't used at all. If 1 million programmers use C, and 1 thousand like Scheme, and some random programmer decided to implement a new compiler for Scheme, it's more likely to be a C programmer by shear numbers. Besides which, a lot of compilers aren't written for use by the people themselves, but for other reasons (commercial use, learning a new language, fun, etc).
Edan Maor
+1  A: 

C has Flex and Yacc which help with implementing the Frontend (parser and lexer) of a compiler, if I remember right their output is limited to c code

josefx
There are many variants and successors of YACC that can output to a wide variety of languages.
nikie
I've seen similar functionality in a lot of different languages, but not Scheme. I'm not saying there isn't a Scheme parser generator out there, but the only Lispy one I've seen was for Common Lisp.
David Thornley
+1  A: 

Many compilers today are written in languages other than C (such as Scheme). To make them portable they initially generate C code as a target language.

MarkTxx