18

I've seen it's very common for a compiler to be made in the language it's compiling. What is the benefit of this? Seems like it makes the process for outsiders (and the developers for a while) more difficult.

Take for instance:

It seems like it just makes things more tricky for the developers (at least in early stages) and for users of the compiler...

marked as duplicate by gnat, Telastyn, Scant Roger, Community Jan 4 '16 at 3:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • In most cases, the compiler is initially not written in itself. Only later versions are. GCC is an exception, because C exists almost everywhere. The Java compiler itself is a C program. etc – Ross Patterson Jan 4 '16 at 3:06
  • @RossPatterson Actually, "the" Java compiler (the canonical one shipped by Oracle) is written in Java and has been written in Java since times forgotten. – Erwin Bolwidt Jan 4 '16 at 6:21
  • @ErwinBolwidt Some of us haven't forgotten Sun, Gosling, and 1995 yet 😊 – Ross Patterson Jan 4 '16 at 12:33
  • @RossPatterson stackoverflow.com/questions/9221912/… The "bootstrap" Java compiler was never released and nobody knows what language it was written in. Perhaps in Oak. The first released Java compiler was distributed as .class files. – Erwin Bolwidt Jan 4 '16 at 13:32
21

It's true in one sense that bootstrapping a language with itself can lead to complications. On the other hand, the people writing a compiler for say C++ are likely to be the world's foremost experts at C++. To a degree, then, it's only natural that they'd choose that language in which to write their code.

It also gives the authors complete autonomy — they don't have to worry about Language X becoming unsupported, because when they're writing compilers for Language Y Version 2 in Language Y Version 1, they know that the compiler for Language Y Version 1 isn't going to disappear or cease to be supported: they are literally the people responsible for keeping it around and supporting it.

Besides, there aren't much better tests of whether a compiler is working properly than to pass its own source code through it. :)

  • 1
    How is getting a compiler to compile its own source a superior test? – MetaFight Jan 3 '16 at 22:15
  • 2
  • 1
    I know the term. I still don't understand how compiling the compiler's own source code is any kind of test. How do you guarantee it's done it correctly? – MetaFight Jan 3 '16 at 22:21
  • 1
    On the other hand, if most languages are written by people who like to write compilers, and they always eat their own dog food, the result will be that most languages will be optimized for writing compilers to the possible detriment of other use cases :) – rici Jan 3 '16 at 23:40
  • 1
    @EJP sure, that still doesn't prove much about the self-compiled compiler other than it can recreate itself. Recreating itself doesn't mean it isn't flawed. – MetaFight Jan 4 '16 at 2:43
4

It is an example of "Eating your own dog food". By writing the compiler in the target language, you verify if the language works and is sufficiently powerful for a complex task.

-1

I have personally found that writing a Python compiler and concrete set of classes that implement Python's grammar to be an extraordinary learning experience in the inner workings of Python. My goals are different than something like PyPy (also implements a self-hosting compiler) but it sure lets you turn the magic dial up to 11! Hahaha