如何为该语言的语言编写一个编译器? [英] How do you write a compiler for a language in that language?

查看:184
本文介绍了如何为该语言的语言编写一个编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能的重复项:

如何用某种语言编写语言的编译器?

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











$ b b

我在看一看 Rubinius ,一个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.

此过程称为 bootstrapping - 可能命名为男管家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 :)

这篇关于如何为该语言的语言编写一个编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆