8

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

4 Answers 4

18

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.

3

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

4
  • why theoretically?what do you mean by that?
    – ehsan0x
    Apr 29, 2010 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, 2010 at 21:38
  • 9
    You could write a C++ compiler in Fortran, but you certainly wouldn't want to.
    – KeithB
    Apr 29, 2010 at 21:52
  • 4
    i think i'd rather write that C++ compiler in Fortran than in Javascript... Apr 29, 2010 at 21:59
2

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
1

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.

You must log in to answer this question.

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