问题描述
$ b b
我在看一看,一个Ruby实现,使用Ruby编写的编译器编译为字节码。我不能让我的头围绕这个。你如何在语言中为语言编写一个编译器?它似乎只是文本没有任何东西将它编译成可执行文件,然后可以编译用Ruby编写的未来代码。我很困惑只是输入那句话。任何人都可以帮助解释这个问题?
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?
推荐答案
简化:你首先用不同的语言编写一个编译器。然后,你编译编译器,并且瞧!
To simplify: you first write a compiler for the compiler, in a different language. Then, you compile the compiler, and voila!
所以,你需要一些已经有编译器的语言 - 但是因为有很多这样,你可以写Ruby编译器(!)例如在C中,然后它将编译Ruby编译器,然后可以编译Ruby程序,甚至自己的更多版本。
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.
当然,原始编译器是用机器代码编写的,汇编的汇编编译器,其反过来编译例如的编译器C或Fortran,它编译编译器几乎一切。 。
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.
此过程称为 - 可能命名为男管家Munchhausen的故事,他通过自己的引导自己脱离沼泽):
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 :)
这篇关于如何为该语言的语言编写一个编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!