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...

  • 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 04 '16 at 03: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 04 '16 at 06:21
  • @ErwinBolwidt Some of us haven't forgotten Sun, Gosling, and 1995 yet – Ross Patterson Jan 04 '16 at 12:33
  • @RossPatterson http://stackoverflow.com/questions/9221912/is-the-compiler-of-java-bootstrapped 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 04 '16 at 13:32

3 Answers3

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. :)

Lightness Races in Orbit
  • 8,755
  • 3
  • 41
  • 45
  • 1
    How is getting a compiler to compile its own source a superior test? – MetaFight Jan 03 '16 at 22:15
  • 2
    @MetaFight: https://en.wikipedia.org/wiki/Eating_your_own_dog_food – Lightness Races in Orbit Jan 03 '16 at 22:19
  • 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 03 '16 at 22:21
  • @MetaFight: The term is the answer. Read the article. It explains. Of course there is no such thing as a "guarantee", in _anything_ we do.... but you must know that already.... – Lightness Races in Orbit Jan 03 '16 at 22:23
  • 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 03 '16 at 23:40
  • @rici do you mean "if most *compilers* were written ...". – muru Jan 04 '16 at 00:05
  • @muru: when I first made that offhand remark, I was thinking also of interpreters and other types of language processors. Could be "if most computer languages are designed by people who like to write programs which process computer languages...", but I think the point was made anyway. :) – rici Jan 04 '16 at 00:36
  • @MetaFight The self-compiled compiler should be bitwise identical to a compiler compiled by the self-compiled compiler. – user207421 Jan 04 '16 at 02:38
  • 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 04 '16 at 02:43
  • @MetaFight Ken Thompson agreed with you in 1984: https://www.ece.cmu.edu/~ganger/712.fall02/papers/p761-thompson.pdf – Ross Patterson Jan 04 '16 at 03:03
  • @MetaFight: But being unable to recreate itself would mean it is potentially flawed. So it serves the same purpose as unit tests: you can't prove there's no bug but failing would prove that there is a bug you need to fix. – slebetman Jan 04 '16 at 03:04
  • @slebetman agreed. But all that's saying is that it can compile a piece of code reliably. The fact that that code happens to be its own source adds nothing to the test. – MetaFight Jan 04 '16 at 03:05
  • @RossPatterson I'll have a read of that tomorrow. thanks :) – MetaFight Jan 04 '16 at 03:06
  • @MetaFight: I'm not sure if you've ever written a compiler or interpreter but if you can compile a compiler it proves that: recursion works (you'll need a recursive descent parser), looping works, dynamic/heap storage works (new, malloc etc.), functions work, variables work, stack storage works (sort of implied by recursion working but not necessarily), file I/O works. About the only thing you're not testing is networking. – slebetman Jan 04 '16 at 03:18
  • @slebetman my point is that you're only proving that those things work for *one* specific application's source. It doesn't prove that those features are bug free. It's no better than any other single-case test. The test being self-referential doesn't make it magical. – MetaFight Jan 04 '16 at 03:47
  • @MetaFight: It's still better than _not_ doing that. Why wait for someone else to find out that you fundamentally broke the implementation of loops in your most recent check in? You have some source code _right there_ that you can test it with right away! – Lightness Races in Orbit Feb 04 '16 at 18:30
5

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.

JacquesB
  • 57,310
  • 21
  • 127
  • 176
-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

Inversus
  • 99
  • 3