What language are compilers written with?

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


Solution 1:

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.

Solution 2:

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

Solution 3:

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.