Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Are compilers written in different languages than the language they compile?

share|improve this question
up vote 16 down vote accepted

There are three languages involved in a compiler: the language being compiled (source language), the language being compiled into (target language), and the language that the compiler is written in (implementation language). In general, the implementation language is either a general purpose language like C or C++, or the source language. The target language can be some language suitable close to the source language that already exists, or assembly. Sometimes there is a custom intermediate language, so that there can be a common backend for a variety of languages. This is the way that gcc works. There is a front end for C, C++, Fortran, Ada, and probably others. Each one is compiled into the same intermediate language, which is then compiled into architecture specific assembly language.

If the compiler is written in the same language as the one it's compiling, the compiler is called self-hosting. If a language is new, then no compiler exists for it already, which means that its first compiler cannot be self-hosting (otherwise, how would that first compiler be compiled?). As a result, the first iteration of any compiler for a new language is always written in a different language. That having been said, a self-hosting is more convenient; the author(s) only need to be proficient in one language.

share|improve this answer

You can write a compiler in, theoretically, any language. Even the one you're compiling.

share|improve this answer
why theoretically?what do you mean by that? – Nobody Apr 29 '10 at 21:36
I say that because I've never seen, for example, a compiler in Javascript. It may or may not be possible, as I've never seen everything Javascript has to offer. – Corey Apr 29 '10 at 21:38
You could write a C++ compiler in Fortran, but you certainly wouldn't want to. – KeithB Apr 29 '10 at 21:52
i think i'd rather write that C++ compiler in Fortran than in Javascript... – quack quixote Apr 29 '10 at 21:59

There's a lot more information about this over at Stack Overflow, like What is the best language to write a compiler in?.

More related at SO

I know questions get moved here from Stack Overflow, but I don't know if it works the other way around too.

share|improve this answer

From a little study I made few years ago (2010):

  • Ocaml ocaml:
    parser: (o)caml
    core: C
  • C++ g++:
    parser: C
    core: C (now c++) -> early generated with bison then hand written
  • D dmd:
    parser: C
    core: C
  • Haskell GHC:
    parser: haskell (+ C)
    core: Haskell
    -> formerly in LML
  • C gcc:
    parser: C
    core: C
    -> formerly BCPL / asm
  • SmallTalk:
  • Lisp:
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .