8

Possible Duplicate:
How could the first C++ compiler be written in C++?

You probably heard that Microsoft released a new language called TypeScript which is a the typed superset of JavaScript.

The most interesting thing that makes me wonder is the fact that its compiler writen in TypeScript itself.

Call me ignorant but I really couldn't figure out in my head how that is possible. This is just like chicken and egg problem in my head because there is no compiler to compile TypeScript's compiler in the first place. How is it possible to write a compiler of the compiler of a programming language with that language?

marked as duplicate by user7043, Martin Beckett, Doc Brown, maple_shaft Oct 3 '12 at 19:12

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.

12

It's called Bootstrapping. You're right that the first version has to be written in another language. But once that's done, you can write successive versions with the previously finished compiler.

  • Not necessarily - sometimes the compiler is written in the same language on a different platform, and except for the first version of a language anywhere, that's the usual these days. – ddyer Oct 3 '12 at 16:57
  • 4
    @ddyer I took "the first version" as "the first version anywhere". Of course you can trivially use a cross compiler to port an existing interpreter to a platform the language hasn't been implemented for yet. – user7043 Oct 3 '12 at 17:22
  • 7
    It's not true that the very first version hast be written in another language. You have to somehow compile the very first compiler, but you don't have to do that by machine. You can do it by hand. Prof. Wirth, for example, simply chopped up the Oberon compiler into equal sized chunks and handed them out as homework assignments to be translated (into a variant of Fortran, I believe) by his students. So, in the end he did end up with a version written in another language, but it wasn't the first version, it was the second. And it wasn't "written" per se, it was translated. – Jörg W Mittag Oct 4 '12 at 1:06
  • That's an awesome story! – Noein Sep 12 '15 at 18:33
  • Glad he's not my teacher. Yuck! – Jack Giffin Dec 30 '17 at 21:45
1

A slight variation is where the language itself is interpreted, so the compiler is written in the language itself, runs in the interpreter and compiles itself. These languages (classically, Lisp) have very different runtime representations for compiled and interpreted code, but the compiled form is required to preserve the same semantics.

  • 3
    Of course, this requires having an interpreter in the first place. While this gets around the letter of the title (it's a pity so many people are so confused about the relationships of languages, compilers and interpreters), it's exactly equivalent once we shed the misconception that a compiler is somehow more of a language implementation than an interpreter. IOW bootstrapping is relevant regardless of whether the language implementations in question are interpreters or compilers. – user7043 Oct 3 '12 at 17:21
  • you could build that interpreter in hardware :) – jwenting Oct 4 '12 at 3:54
  • you could also build that interpreter in software if you're not a masochist :) – Jack Giffin Dec 30 '17 at 21:47

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