用自己的语言编写编译器 [英] Writing a compiler in its own language

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

问题描述

直观地说,Foo 语言的编译器本身似乎不能用 Foo 编写.更具体地说,语言 Foofirst 编译器不能用 Foo 编写,但任何后续编译器都可以为 Foo 编写.

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?

推荐答案

这称为引导".您必须首先用其他语言(通常是 Java 或 C)为您的语言构建编译器(或解释器).完成后,您可以使用 Foo 语言编写新版本的编译器.您使用第一个引导编译器来编译编译器,然后使用这个编译后的编译器来编译其他所有内容(包括它自己的未来版本).

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.

Scala 就是一个例子.它的第一个编译器是用 Martin Odersky 的实验性语言 Pizza 创建的.从 2.0 版开始,编译器完全用 Scala 重写.从那时起,旧的 Pizza 编译器可能会被完全丢弃,因为新的 Scala 编译器可以用于为未来的迭代进行编译.

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.

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

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