Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question
add comment

4 Answers 4

up vote 13 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
add comment

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
1  
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
9  
You could write a C++ compiler in Fortran, but you certainly wouldn't want to. –  KeithB Apr 29 '10 at 21:52
3  
i think i'd rather write that C++ compiler in Fortran than in Javascript... –  quack quixote Apr 29 '10 at 21:59
1  
@quack: or brainfuck for that matter :) –  akira Apr 30 '10 at 16:09
show 3 more comments

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
add comment

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:
    SmallTalk
  • Lisp:
    Lisp
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.