📜  语言处理器:汇编器、编译器和解释器

📅  最后修改于: 2021-09-28 09:53:52             🧑  作者: Mango

语言处理器 –

编译器,解释器,将用高级语言编写的程序翻译成计算机可以理解的机器代码。汇编程序将用低级或汇编语言编写的程序翻译成机器代码。在编译过程中,有几个阶段。为了帮助程序员编写无错误的代码,可以使用工具。

汇编语言依赖于机器,但用于表示其中指令的助记符不能被机器直接理解,高级语言是独立于机器的。计算机理解机器代码中的指令,即以 0 和 1 的形式。直接用机器代码编写计算机程序是一项繁琐的任务。这些程序大多是用Java、C++、 Python等高级语言编写的,被称为源代码。这些源代码不能由计算机直接执行,必须转换成机器语言才能执行。因此,使用专门的翻译系统软件将用高级语言编写的程序翻译成机器码,称为语言处理器,翻译成机器码后的程序(目标程序/目标代码)。

语言处理器可以是以下三种类型中的任何一种:

1. 编译器:
将高级语言编写的完整源程序整体读取并翻译成机器语言的等价程序的语言处理器称为编译器。示例:C、C++、C#、 Java。

在编译器中,如果源代码没有错误,它就会成功地转换为目标代码。当源代码中存在任何错误时,编译器会在编译结束时用行号指定错误。在编译器再次成功重新编译源代码之前,必须删除这些错误

2. 装配工:
汇编器用于将用汇编语言编写的程序翻译成机器代码。源程序是包含汇编语言指令的汇编器的输入。汇编器生成的输出是计算机可以理解的目标代码或机器代码。汇编程序基本上是第一个能够与机器进行人机交流的界面。我们需要一个 Assembler 来填补人和机器之间的空白,以便它们可以相互通信。用汇编语言编写的代码是某种助记符(指令),如 ADD、MUL、MUX、SUB、DIV、MOV 等。并且汇编程序基本上能够将这些助记符转换为二进制代码。在这里,这些助记符还取决于机器的架构。

比如intel 8085和intel 8086的架构就不同。

3. 翻译:
将源程序的单个语句翻译成机器代码是由语言处理器完成的,并在移动到下一行之前立即执行被称为解释器。如果语句中有错误,解释器将在该语句处终止其翻译过程并显示一条错误消息。只有在错误消除后,解释器才会移动到下一行执行。解释器直接执行以编程或脚本语言编写的指令,而无需事先将它们转换为目标代码或机器代码。

示例:Perl、 Python和 Matlab。

编译器和解释器之间的区别 –

Compiler Interpreter
A compiler is a program that converts the entire source code of a programming language into executable machine code for a CPU.  
 
An interpreter takes a source program and runs it line by line, translating each line as it comes to it
The compiler takes a large amount of time to analyze the entire source code but the overall execution time of the program is comparatively faster. 
 
An interpreter takes less amount of time to analyze the source code but the overall execution time of the program is slower.
The compiler generates the error message only after scanning the whole program, so debugging is comparatively hard as the error can be present anywhere in the program.   Its Debugging is easier as it continues translating the program until the error is met.
The compiler requires a lot of memory for generating object codes. It requires less memory than a compiler because no object code is generated.
Generates intermediate object code.  No intermediate object code is generated.
For Security purpose compiler is more useful. The interpreter is a little vulnerable in case of security.
Examples: C, C++, Java   Examples: Python, Perl, JavaScript, Ruby