Compiler - 编译器

The first compiler was build by John Backum and his group between 1954 and 1957 at IBM.
编译器就是将高级语言翻译为低级语言的程序。

FORTRAN one was the first successful high level language
FORTRAN语言是Formula Translation的缩写,意为“公式翻译”。它是为科学、工程问题或企事业管理中的那些能够用数学公式表达的问题而设计的,其数值计算的功能较强。
FORTRAN语言是世界上第一个被正式推广使用的高级语言。它是1954年被提出来的,1956年开始正式使用,直到2021年已有六十年的历史,但仍历久不衰,它始终是数值计算领域所使用的主要语言。

1. What is a Compiler?

A compiler is a computer program which helps you transform source code written in a high-level language into low-level machine language. It translates the code written in one programming language to some other language without changing the meaning of the code. The compiler also makes the end code efficient which is optimized for execution time and memory space.
编译器是一种计算机程序,可以帮助你将以高级语言编写的源代码转换为低级机器语言。它将以一种编程语言编写的代码转换为另一种语言,而无需更改代码的含义。编译器对代码执行时间和存储空间进行优化,使最终代码高效率。

The compiling process includes basic translation mechanisms and error detection. Compiler process goes through lexical, syntax, and semantic analysis at the front end, and code generation and optimization at a back end.
编译过程包括基本的转换机制和错误检测。编译器过程在前端进行词法,语法和语义分析,在后端进行代码生成和优化。

front end: lexical, syntax, and semantic analysis
back end: code generation and optimization

lexical ['leksɪk(ə)l]:adj. 词汇的
syntax ['sɪntæks]:n. 句法,句法规则,语构
semantic [sə'mæntɪk]:adj. 语义的

 

在这里插入图片描述

2. 编译器类型

2.1 Single Pass Compilers

在这里插入图片描述

In single pass Compiler source code directly transforms into machine code. For example, Pascal language.

2.2 Two Pass Compilers

在这里插入图片描述

Two pass Compiler is divided into two sections, viz.

  1. Front end: It maps legal code into Intermediate Representation (IR).
  2. Back end: It maps IR onto the target machine

The Two pass compiler method also simplifies the retargeting process. It also allows multiple front ends.

videlicet [vɪ'deləsɪt]:adv. 即
viz. [vɪz]:adv. 即,就是
 

2.3 Multipass Compilers

在这里插入图片描述

The multipass compiler processes the source code or syntax tree of a program several times. It divided a large program into multiple small programs and process them. It develops multiple intermediate codes. All of these multipass take the output of the previous phase as an input. So it requires less memory. It is also known as ‘Wide Compiler’.
multipass compiler 会多次处理程序的源代码或语法树。它将一个大型程序划分为多个小型程序并进行处理。它开发了多个中间代码。所有这些多通道都将前一阶段的输出作为输入。因此,它需要更少的内存。它也被称为 Wide Compiler。

3. Language processing systems

源代码 (source code) -> 预处理器 (preprocessor) -> 编译器 (compiler) -> 目标代码 (object code) -> 链接器 (Linker) -> 可执行程序 (executables)

在这里插入图片描述

Preprocessor: The preprocessor is considered as a part of the Compiler. It is a tool which produces input for Compiler. It deals with macro processing, augmentation, language extension, etc.
预处理器被视为编译器的一部分。它是为编译器生成输入的工具。它处理宏处理,扩充,语言扩展等。

Interpreter: An interpreter is like Compiler which translates high-level language into low-level machine language. The main difference between both is that interpreter reads and transforms code line by line. Compiler reads the entire code at once and creates the machine code.
解释器就像编译器一样,可以将高级语言翻译成低级机器语言。两者之间的主要区别是解释器逐行读取和转换代码。编译器立即读取整个代码并创建机器代码。

Assembler: It translates assembly language code into machine understandable language. The output result of assembler is known as an object file which is a combination of machine instruction as well as the data required to store these instructions in memory.
它将汇编语言代码转换为机器可理解的语言。汇编器的输出结果称为目标文件,该文件是机器指令以及将这些指令存储在内存中所需的数据的组合。

Linker: The linker helps you to link and merge various object files to create an executable file. All these files might have been compiled with separate assemblers. The main task of a linker is to search for called modules in a program and to find out the memory location where all modules are stored.
链接器可帮助您链接和合并各种目标文件以创建可执行文件。所有这些文件可能都已使用单独的汇编器进行了编译。链接器的主要任务是在程序中搜索被调用的模块,并找出所有模块的存储位置。

Loader: The loader is a part of the OS, which performs the tasks of loading executable files into memory and run them. It also calculates the size of a program which creates additional memory space.
加载程序是操作系统的一部分,它执行将可执行文件加载到内存中并运行它们的任务。它还计算程序的大小,该程序将创建额外的内存空间。

Cross-compiler: A cross compiler is a platform which helps you to generate executable code.
交叉编译器是一个可帮助您生成可执行代码的平台。

Source-to-source Compiler: Source to source compiler is a term used when the source code of one programming language is translated into the source of another language.
源到源编译器是将一种编程语言的源代码转换为另一种语言的源时使用的术语。

4. Compiler Construction Tools

These tools use specific language or algorithm for specifying and implementing the component of the compiler.
这些工具使用特定的语言或算法来指定和实现编译器的组件。

Scanner generators: This tool takes regular expressions as input. For example LEX for Unix Operating System.
该工具将正则表达式作为输入。例如,用于 Unix 操作系统的 LEX。

Syntax-directed translation engines: These software tools offer an intermediate code by using the parse tree. It has a goal of associating one or more translations with each node of the parse tree.
这些软件工具通过使用解析树提供中间代码。它的目标是将一个或多个转换与解析树的每个节点相关联。

Parser generators: A parser generator takes a grammar as input and automatically generates source code which can parse streams of characters with the help of a grammar.
解析器生成器将语法作为输入,并自动生成可在语法帮助下解析字符流的源代码。

Automatic code generators: Takes intermediate code and converts them into Machine Language
接受中间代码并将其转换为机器语言。

Data-flow engines: This tool is helpful for code optimization. Here, information is supplied by user and intermediate code is compared to analyze any relation. It is also known as data-flow analysis. It helps you to find out how values are transmitted from one part of the program to another part.
该工具有助于代码优化。在此,信息由用户提供,并且将中间代码进行比较以分析任何关系。也称为数据流分析。它可以帮助您了解如何将值从程序的一个部分传输到另一部分。

intermediate representation,IR:中间表示
front end:前端
back end:后端

References

https://www.guru99.com/compiler-design-tutorial.html
http://personal.kent.edu/~rmuhamma/Compilers/MyCompiler/phase.htm

posted @ 2021-05-14 09:33  古锁阳关  阅读(475)  评论(0编辑  收藏  举报