解释型语言和编译型语言的区别_编译器、解释器、编译型语言、解释型语言

编译器:A compiler is a translator which transforms source language (high-level language) into object language (machine language).

441c0a0eab3b4e6511b59f256a7e3e2f.png
编译器构造

Front end: Include lexical analysis, syntax analysis, and semantic analysis. The front end transforms the input program into an intermediate representation (IR) for further processing by the middle end. IR is the data structure or code used internally by a compiler or virtual machine to represent source code.

Middle end: Also known as optimizer, performs optimizations on the intermediate representation that are independent of the CPU architecture being targeted, in order to improve the performance and the quality of the produced machine code.

Back end: Takes the optimized IR from the middle end and perform more analysis, transformations and optimizations that are specific for the target CPU architecture and operating system.

Compiler operates on two phases: Analysis phase includes lexical analyzer, semantic analyzer and syntax analyzer; Synthesis phase includes code optimizer and code generator.

  1. Lexical Analyzer: It scans the code as a stream of characters, groups the sequence of characters into lexemes and outputs a sequence of tokens with reference to the programming language.
  2. Syntax Analyzer: In this phase, the tokens that are generated in the previous stage are checked against the grammar of programming language, whether the expressions are syntactically correct or not. It makes parse trees for doing so.
  3. Semantic Analyzer: It verifies whether the expressions and statements generated in the previous phase follow the rule of programming language or not and it creates annotated parse trees.
  4. Intermediate code generator: It generates an equivalent intermediate code of the source code. There are many representations of intermediate code, but TAC (Three Address Code) is the used most widely.
  5. Code Optimizer: It improves time and space requirement of the program. For doing so, it eliminates the redundant code present in the program.
  6. Code generator: This is the final phase of the compiler in which target code for a particular machine is generated. It performs operations like memory management, Register assignment, and machine-specific optimization.

12521ba1a64e9f7c4e77fbf348fca358.png
编译系统

解释器:An interpreter is a computer program that directly executes instructions written in a programming or scripting language, without requiring them previously to have been compiled into a machine language program. An interpreter generally uses one of the following strategies for program execution:

  1. Parse the source code and perform its behavior directly.
  2. Translate source code into some efficient intermediate representation and immediately execute this.
  3. Explicitly execute stored precompiled code made by a compiler which is part of the interpreter system.

编译器和解释器的差异

  1. The compiler takes a program as a whole and translates it, but interpreter translates a program statement by statement.
  2. Intermediate code or target code is generated in case of a compiler. As against interpreter doesn't create intermediate code.
  3. A compiler is comparatively faster than Interpreter as the compiler take the whole program at one go whereas interpreters compile each line of code after the other.
  4. The compiler requires more memory than interpreter because of the generation of object code.
  5. Compiler presents all errors concurrently, and it's difficult to detect the errors in contrast interpreter display errors of each statement one by one, and it's easier to detect errors.
  6. In compiler when an error occurs in the program, it stops its translation and after removing error whole program is translated again. On the contrary, when an error takes place in the interpreter, it prevents its translation and after removing the error, translation resumes.
  7. In a compiler, the process requires two steps in which firstly source code is translated to target program then executed. While in Interpreter It's a one-step process in which source code is compiled and executed at the same time.
  8. The compiler is used in programming languages like C, C++, C#, Scala, etc. On the other Interpreter is employed in languages like PHP, Ruby, Python, etc.

无论是编译程序还是解释程序,最终都要将高级语言编译成计算机能识别的机器语言,只是过程和形式不同。

编译型语言:A programming language whose implementations are typically compilers (translators that generate machine code from source code), and not interpreters (step-by-step executors of source code, where no pre-runtime translation takes place).编译型语言编译器处理流程——源代码 (source code) → 预处理器 (preprocessor) → 编译器 (compiler) → 目标代码 (object code) → 链接器 (Linker) → 可执行程序 (executables)→执行。

解释型语言:A type of programming language for which most of its implementations execute instructions directly and freely, without previously compiling a program into machine-language instructions.解释型语言解释器处理流程——源代码 (source code) → 解释器 (interpreter)中间代码(字节码)和机器指令→执行。

随着Java等基于虚拟机的语言的出现,语言不能纯粹地分成解释型和编译型这两种。Java首先是通过编译器编译成字节码文件,然后在运行时通过解释器给解释成机器文件。

跨平台:编译型语言由于编译生成与特定操作系统和CPU相匹配的目标代码,跨平台性较差;而解释型语言在不同的平台有不同的解释器,开发人员不用再考虑每个平台如何去编译,只需要关注代码的编写,编写完的代码在任何平台都能无需修改(或少量修改)就能正确执行。

效率:编译型语言的可执行程序产生的是直接执行机器指令(window exe文件),而解释型语言的每一句源代码都要经过解释器解释为可以执行的机器指令,相比之下解释型语言的执行效率会低一些。部分解释型语言的解释器通过在运行时动态优化代码,甚至能够使解释型语言的性能超过编译型语言。Python在运行时会生成pyc的二进制临时文件,Java编译的字节码都能提高效率。

调试:解释器一次执行一个语句,因此更容易调试且发现错误。

993149261cb5fc680e7c4320102d2bf9.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值