编译器如何编译编译器? [英] How does a compiler compile a compiler?

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

问题描述

来自高级编程背景,我对学习低级编程感兴趣。我想知道编译器的编译方式吗?

Coming from a high-level programming background, I am interested in learning about low-level programming. I want to know how a compiler is compiled?

在Wiki中查看了一些文章之后,数字机器代码被认为是最低级别的语言,但是必须有一个编译器才能编译该机器代码。该编译器使用哪种语言?

After looking at some articles in wiki, Numerical machine code is said to be lowest level language, but there has to be a compiler to compile this machine code. What language is that compiler written in?

推荐答案

通常,编译器作者采用以下两种方法之一:

Typically, compiler authors go one of two routes:


  • 用其他现有语言编写整个编译器。通常,这是最简单的选择。

  • Write the entire compiler in some other existing language. This is generally the simplest option.

用其他某种语言编写足够的代码,以使用最少的翻译程序,并以脚手架为基础用要编译的语言正确编写编译器。这比较复杂,通常需要更长的时间,但是从本质上来说,通过在真实项目中测试语言,可以消除语言的错误和弱点。

Write just enough code in some other language to have a minimally usable translator, and use that "scaffolding" as a base to write the compiler proper in the language it's intended to compile. This is more complicated, and usually takes longer, but inherently offers the chance to flush out language bugs and weaknesses by testing the language in a real project.

第一个翻译代码的程序至少部分是用机器代码编写的-实际数字告诉CPU该怎么做。这是最低级别,因为机器代码 * 并没有真正的编译器;只是数字以某种方式排列,并且CPU内部有电路可以在没有外部帮助的情况下处理它们。

The first program to translate code was written at least partly in machine code -- the actual numbers that tell the CPU what to do. It's the lowest level because there is not really a "compiler" for machine code*; it's just numbers arranged a certain way, and the CPU has circuitry within it to process them without outside help.

* 这些程序可以帮助设计用于解释和执行指令的硬件,但是可以说它超出了编译器的定义范围。这样的程序生成硬件描述(电路图等),而不是编译器输出的直接可执行文件。

* There are programs to help design the hardware that interprets and executes the instructions, but that arguably sits outside the definition of a compiler. Such programs generate hardware descriptions -- circuit diagrams and the like -- as opposed to the directly executable files a compiler outputs.

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

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