Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you write a compiler for a language in that language? [duplicate]

Possible Duplicates:
How can a language's compiler be written in that language?
implementing a compiler in “itself”

I was looking at Rubinius, a Ruby implementation that compiles to bytecode using a compiler written in Ruby. I cannot get my head around this. How do you write a compiler for a language in the language itself? It seems like it would be just text without anything to compile it into an executable that could then compile the future code written in Ruby. I get confused just typing that sentence. Can anyone help explain this?

like image 850
jergason Avatar asked Nov 24 '10 10:11

jergason


People also ask

How can compiler of a language be written in the same 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?).

How is compiler code written?

A compiler is a special program that translates a programming language's source code into machine code, bytecode or another programming language. The source code is typically written in a high-level, human-readable language such as Java or C++.

Can you write a compiler in any language?

C is a “compiled” language - and the compiler could be written in any language at all. Most C compilers are written in C - but I'd bet a good few have a bunch of C++ code in them too.


2 Answers

To simplify: you first write a compiler for the compiler, in a different language. Then, you compile the compiler, and voila!

So, you need some sort of language which already has a compiler - but since there are many such, you can write the Ruby compiler compiler (!) e.g. in C, which will then compile the Ruby compiler, which can then compile Ruby programs, even further versions of itself.

Of course, the original compilers were written in machine code, compiled compilers for assembly, which in turn compiled compilers for e.g. C or Fortran, which compiled compilers for...pretty much everything. Iterative development in action.

The process is called bootstrapping - possibly named after Baron Munchhausen's story in which he pulled himself out of a swamp by his own bootstraps :)

like image 88
Piskvor left the building Avatar answered Sep 22 '22 13:09

Piskvor left the building


Regarding the bootstrapping of a compiler it's worth reading about this devilishly clever hack.

http://catb.org/jargon/html/B/back-door.html

like image 42
noodl Avatar answered Sep 18 '22 13:09

noodl