2

排除将一种高级语言转换为另一种高级语言的编译器,是否任何编译为机器代码的编译器都需要用汇编语言编写?

4

2 回答 2

7

The source code for a compiler does not need to be written in assembly. For example, (a good portion) of the CPython compiler (well, technically interpreter) is written in C: http://en.wikipedia.org/wiki/Cpython

In the very beginning, before there were compilers, the very first compiler did have to be written in assembly. But then someone used this compiler to compile their own compiler. Then someone else used this compiler to compile their own compiler. And so on and so forth.

This brings up the concept of "bootstrapping". A bootstrapping compiler is one that is written in the language it intends to compile. The clang compiler can compile C++ code, but the compiler itself is written in C++! How does this work? Well the very first clang compiler was compiled by a different compiler (g++ probably). After the clang compiler was mature enough, it was able to compile its own code. Now, any changes made to the clang compiler can be recompiled by the clang compiler into another clang compiler! Neat, huh?

于 2013-09-10T16:07:29.770 回答
1

编译器不是一个特殊的、神奇的程序。如果你眯着眼睛看,编译器只是一个将一个文本文件转换为另一个文本文件的程序(好吧,一个二进制文件)。

关于可以用哪种语言实现程序只有一个限制。如果您使用一种语言编写程序,则该语言必须已经实现了编译器或解释器。因此,语言 X 的第一个编译器/解释器不能用 X 编写。(如果您希望能够使用它,至少。)但这对于编译器/解释器来说并不特殊;在有人实现 X 之前,你不能在 X 中有用地编写任何东西。

于 2013-09-10T19:16:26.283 回答