Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a compiler in its own language

Intuitively, it would seems that a compiler for language Foo cannot itself be written in Foo. More specifically, the first compiler for language Foo cannot be written in Foo, but any subsequent compiler could be written for Foo.

But is this actually true? I have some very vague recollection of reading about a language whose first compiler was written in "itself". Is this possible, and if so how?

like image 424
Dónal Avatar asked Oct 20 '22 11:10

Dónal


People also ask

Can you write a compiler in its own language?

At some point, you need a compiler (or interpreter) written in a different language. But it doesn't need to be efficient and can be done in a language that makes parsing and prototyping easy (LISP is popular). Once you have used this to compile the "self-compiler", you can discard it and use the result.

What language should I write a compiler in?

While C and C++ work perfectly well for writing compilers, quite a few other languages seem to work perfectly well for the task as well. A bit depends on the language you're compiling though. For small, simple languages, C and Pascal work quite nicely.

Is it possible to write a compiler for Python?

Compiling python isn't easy. You could look at pypy which has a just-in-time compiler. Another option is to start with the python bytecode that is saved in a . pyc file if a python program is run by the standard Cpython interpreter.

How do you write a compiler?

If languages each have a set of grammar rules, and those rules are all the legal expressions, then there are primarily two parts to building a compiler. Be able to read a file, parse it, then build an validate an Abstract Syntax Tree from that grammar.


2 Answers

This is called "bootstrapping". You must first build a compiler (or interpreter) for your language in some other language (usually Java or C). Once that is done, you can write a new version of the compiler in language Foo. You use the first bootstrap compiler to compile the compiler, and then use this compiled compiler to compile everything else (including future versions of itself).

Most languages are indeed created in this fashion, partially because language designers like to use the language they are creating, and also because a non-trivial compiler often serves as a useful benchmark for how "complete" the language may be.

An example of this would be Scala. Its first compiler was created in Pizza, an experimental language by Martin Odersky. As of version 2.0, the compiler was completely re-written in Scala. From that point on, the old Pizza compiler could be completely discarded, due to the fact that the new Scala compiler could be used to compile itself for future iterations.

like image 258
Daniel Spiewak Avatar answered Oct 22 '22 23:10

Daniel Spiewak


I recall listening to a Software Engineering Radio podcast wherein Dick Gabriel spoke about bootstrapping the original LISP interpreter by writing a bare-bones version in LISP on paper and hand assembling it into machine code. From then on, the rest of the LISP features were both written in and interpreted with LISP.

like image 79
Alan Avatar answered Oct 22 '22 23:10

Alan