如何用一种语言编写一个编译器?

发布于 2024-10-04 07:52:39 字数 536 浏览 0 评论 0原文

可能的重复:
一种语言的编译器如何用该语言编写?< /a>
在“自身”中实现编译器

我正在查看Rubinius,一个 Ruby 实现,使用 Ruby 编写的编译器编译为字节码。我无法理解这个问题。如何用该语言本身编写该语言的编译器?看起来它只是文本,没有任何东西可以将其编译成可执行文件,然后可以编译将来用 Ruby 编写的代码。只是输入这句话我就感到困惑。谁能帮忙解释一下吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

北方的巷 2024-10-11 07:52:39

为了简化:您首先用不同的语言为编译器编写一个编译器。然后,编译编译器,瞧!

所以,你需要某种已经有编译器的语言 - 但由于有很多这样的语言,你可以用 C 编写 Ruby 编译器(!),然后编译 Ruby 编译器,然后编译 Ruby 程序,甚至是其自身的进一步版本。

当然,最初的编译器是用机器代码编写的,编译用于汇编的编译器,汇编编译器又编译用于例如 C 或 Fortran 的编译器,后者编译编译器......几乎所有东西。 迭代开发实践。

该过程称为bootstrapping - 可能以 Baron Munchhausen 的故事命名,在这个故事中,他通过自己的引导从沼泽中拉出了自己:)

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 :)

你另情深 2024-10-11 07:52:39

关于编译器的引导,值得一读的是这个极其聪明的技巧。

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

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

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

秋日私语 2024-10-11 07:52:39

光是读这句话我就感到困惑。

将编译器视为翻译器(通常称为编译器)可能会有所帮助。其目的是获取人类可以读取的源代码并将其翻译为计算机可以读取的二进制代码。就 Rubinius 而言,它读取的代码恰好是 Ruby 代码,而它转换成的代码是机器代码(实际上是 LLVM 机器代码,其本身进一步编译为 Intel 机器代码,但这只是背景细节) 。 Rubinius 本身可以用任何编程语言编写。它只是碰巧是用它编译的相同语言编写的。

当然,您首先需要一些东西来运行 Rubinius,这很可能是一个常规的 Ruby 解释器。但请注意,一旦您能够在解释器上运行 Rubinius,您就可以将其自己的源代码传递给它,它将创建并运行其自身的编译版本。这就是所谓的“引导”(bootstrapping),源于一句古老的短语,“通过引导来拉动自己”。

最后一点:Ruby 程序无法调用任意机器代码。 Rubinius 的那部分实际上是用 C++ 编写的。

I get confused just reading that sentence.

It may help to think of the compiler as a translator, which compilers are often called. Its purpose is to take source code that humans can read and translate it into binary code that computers can read. In the case of Rubinius, the code that it reads happens to be Ruby code, and the code that it converts it into is machine code (actually LLVM machine code which is itself further compiled into Intel machine code, but that's just a background detail). Rubinius itself could have been written in just about any programming language. It just happened to have been written in the same language that it compiles.

Of course, you need something to run Rubinius in the first place, and this most likely a regular Ruby interpreter. Note, however, that once you are able to run Rubinius on an interpreter, you can pass it its own source code, and it will create and run a compiled version of itself. This is called bootstrapping, from the old phrase, "pulling yourself up by the bootstraps".

One final note: Ruby programs can't invoke arbitrary machine code. That part of Rubinius is actually written in C++.

忆依然 2024-10-11 07:52:39

可以按照以下顺序进行:

  1. 用任何语言编写一个编译器,比如用 C 语言编写 Ruby 代码。
  2. 现在您可以编译 Ruby 代码了,您可以编写一个编译 Ruby 代码的编译器,并使用您在步骤 1 中编写的 C 编译器来编译此编译器。wahh 这句话很奇怪!
  3. 从现在开始您可以编译你所有的 ruby​​ 代码都用 2 编写的编译器。:)

玩得开心! :)

Well it is possible to do it in the following order:

  1. Write a compiler in any language, say C for your Ruby code.
  2. Now that you can compile Ruby code, you can write a compiler that compiles ruby code and compile this compiler with the C compiler you wrote in step 1. wahh this sentence is strange!
  3. From now on you can compile all your ruby code with the compiler written in 2. :)

Have fun! :)

骑趴 2024-10-11 07:52:39

编译器只是将源代码转换为可执行文件的东西。因此,它用什么编写并不重要 - 它可以是正在编译的相同语言,也可以是任何其他具有足够功能的语言。

当您为某个平台的语言编写编译器时,乐趣就来了,该编译器是用同一种语言编写的,而该平台还没有适用于您的实现语言的编译器。您在这里的选择是在您有编译器的另一个平台上进行编译,或者用另一种语言编写编译器,然后使用它来编译“真正的”编译器。

A compiler is just something that transforms source code into an executable. So it doen't matter what it is written in - it can be the same language it is compiling or any other language of sufficient power.

The fun comes when you are writing a compiler for a language for a platform, written in the same language, that doesn't yet have a compiler for your implementation language. Your choices here are to compile on another platform for which you do have a compiler, or write a compiler in another language, and use that to compile the "real" compiler.

怀念你的温柔 2024-10-11 07:52:39

这是一个两步过程:

  1. 用其他语言(如 C)编写 Ruby 编译器,假设 Ruby 编译器还不存在
  2. ,因为您现在有了 Ruby 编译器,您可以编写一个 Ruby 程序,它是(新的)Ruby 编译

器已经写了一个 Ruby 编译器(Matz),你“只”需要做第二部分。说起来容易做起来难。

It's a 2 step process:

  1. write a Ruby compiler in some other lanaguage like C, assuming a Ruby compiler doesn't yet exist
  2. since you now have a Ruby compiler, you can write a Ruby program that is a (new) Ruby compiler

Since somebody already wrote a Ruby compiler (Matz), you "only" have to do the second part. Easier said than done.

山人契 2024-10-11 07:52:39

到目前为止的所有答案都解释了如何使用不同的编译器来引导编译器。但是,还有一种替代方法:手动编译编译器。编译器没有理由必须由机器执行,它也可以由人类执行。

All of the answers so far have explained how to bootstrap the compiler by using a different compiler. However, there is an alternative: compiling the compiler by hand. There's no reason why the compiler has to be executed by a machine, it can just as well be executed by a human.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文