一种语言的编译器如何用该语言编写?
Possible Duplicates:
implementing a compiler in “itself”
Bootstrapping a language
How can you write a compiler in the same language as the language you're writing that compiler for? Isn't that sort of recursive?
Edit:
This may be deleted, but otherwise... :
How to bootstrap:
Why to bootstrap:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通常,编译器的第一个版本是用不同的语言编写的,然后每个后续版本都用该语言编写并使用旧版本进行编译。使用版本
x-1
编译版本x
后,您可以使用新构建的版本x
重新编译自身,利用任何该版本引入的新优化; GCC 就是这样发布的Generally the first version of the compiler is written in a different language, and then each subsequent version is written in that language and compiled with the older version. Once you've compiled version
x
with versionx-1
, you can use the newly built versionx
to recompile itself, taking advantage of any new optimizations that version introduces; GCC does its releases that way这是。您通常需要从另一种语言编译或解释的语言的引导版本。
为了让你更容易理解,几年前我读过作为研究生项目编写的 Pascal 编译器的历史。它用Pascal编写,并使用系统内置的Pascal编译器进行编译。最终,它足以取代系统内置的 Pascal 编译器。不幸的是,他们发现了代码生成中的一个错误,但是代码生成器的修复触发了编译器中的错误,生成了一个错误的编译器。要修复它,需要从已安装的编译器中手动修补二进制文件,然后将补丁应用到源代码以替换自身。
It is. You usually need a bootstrap version of the language either compiled or interpreted from another language.
And to bend your mind a little more, years ago I read the history of a Pascal compiler written as a grad student project. It written in Pascal and compiled with the system's built-in Pascal compiler. Eventually, it was good enough to replace the system's built-in Pascal compiler. Unfortunately, they found a bug in code generation, but the fix for the code generator triggered the bug in the compiler, generating a bad compiler. To fix it required hand-patching the binaries from the installed compiler to then apply the patch to the source to replace itself.
这只是第一个版本的问题。一旦我的编译器 V1.0 工作了,我就可以用我的语言编写 V2.0 并使用 V1.0 编译器来编译它。然后我可以编写V3.0并使用V2.0来编译它,使用V3.0来编译V4.0等等。
It's only a problem for the very first version ever. Once I have V1.0 of the compiler working I can write V2.0 in my language and use the V1.0 compiler to compile it. Then I can write V3.0 and use V2.0 to compile that, use V3.0 to compile V4.0 and so on.
编译器的第一遍通常是用其他东西编写的,直到该语言的格式足够好,能够编译它自己的编译器,然后您就可以进入用 x 编写的 x 。
The first pass of the compiler is normally written in something else until the language is well-formed enough to be able to compile it's own compiler, then you can get into the x is written in x.
在某些时候,您需要一个用不同语言编写的编译器(或解释器)。但它不需要高效,并且可以用一种使解析和原型设计变得容易的语言来完成(LISP 很流行)。一旦你使用它编译了“自编译器”,你就可以丢弃它并使用结果。
At some point, you need a compiler (or interpreter) written in a different language. But it doesn't need to be efficient and can be done in a language that makes parsing and prototyping easy (LISP is popular). Once you have used this to compile the "self-compiler", you can discard it and use the result.
一开始,该语言真正的第一个编译器当然不是用该语言编写的。第二个可以用那种语言写。此外,给定一种语言的规范,您可以在引导编译器中实现基本核心,然后使用“引导”编译器理解的子集以该语言编写完全兼容的编译器。第二代编译器也可能忘记“引导”编译器。
At the very beginning, the real first compiler of that language, was written not in that language of course. Very second could be written in that language. Moreover, given a spec of a language, you can implement a basic core in a bootstrap compiler, and then write the full compliant compiler in that language using the subset understood by the "bootstrap" compiler. Second generation compilers can forget "bootstrap" compiler too.