解释器实际上是在内存中预编译的吗?

时间:2021-06-08 20:46:04

Ok guys I thought I'd take my old CS notes and look through compiler theory a little more. I have to say I can't for the life in me remember how all this stuff works but I do have a nice sample application from my college days that helps me understand a few things.

好吧,伙计们,我以为我会用旧的CS笔记,再看一下编译器理论。我不得不说我不能为我的生活记住所有这些东西是如何工作的,但我在大学时代有一个很好的示例应用程序,这有助于我了解一些事情。

This sample application takes the made up language and compiles it down to an intermediate assembly code like language. There is then a simple VM implementation that takes this intermediate language and executes the statements.

此示例应用程序采用组合语言并将其编译为类似语言的中间汇编代码。然后有一个简单的VM实现,它采用这种中间语言并执行语句。

The thing I cant get my head around is, if I this were a straight up interpretor and not a compiler would it still be building up these intermeditary commands in memory to be executed at the end. Or does an interpretor actually "execute" descreet sections of the code chunks at a time?

我无法理解的是,如果我这是一个直接的解释器而不是编译器,它仍然会在内存中构建这些中间命令以便在最后执行。或者一个解释器实际上一次“执行”代码块的descreet部分?

4 个解决方案

#1


Parsers don't compile. There are actually quite a few steps involved when translating a program (from say a high level language like C++ to machine code). It depends on the design if it executes in one go or after multiple passes over the input. Can you make your question a bit more specific? Meanwhile, however much you hate it, have a look here -- specially the frontend and backend sections.

解析器不编译。翻译程序时实际上涉及很多步骤(从像C ++这样的高级语言到机器代码)。如果它一次执行或多次通过输入后,它取决于设计。你能让你的问题更具体一些吗?与此同时,无论你多讨厌它,看看这里 - 特别是前端和后端部分。

#2


It depends on the language. Most modern interpreted languages (Perl, Python, and Ruby, to name a few) precompile the source code into some intermediary form to be executed at the end (citation).

这取决于语言。大多数现代解释语言(Perl,Python和Ruby,仅举几例)将源代码预编译为一些中间形式,以便在最后执行(引用)。

#3


I have written or worked with interpreters that act directly upon parsing tokens in the input, that act directly on an abstract-syntax tree (AST) built by the parser, and that translate the AST to a form designed for efficient execution. So the answer is, it depends.

我已经编写或使用解释器直接解析输入中的令牌,直接作用于解析器构建的抽象语法树(AST),并将AST转换为专为高效执行而设计的表单。所以答案是,这取决于。

  • If your target machine has 8K of RAM, a direct-parsing interpreter would be a sensible choice (think FORTH).
  • 如果你的目标机器有8K的RAM,直接解析解释器将是一个明智的选择(想想FORTH)。

  • If you are using interpreters to teach or learn about the structure and semantics of programming languages, building and interpreting an AST is a good choice.
  • 如果您使用解释器来教授或了解编程语言的结构和语义,那么构建和解释AST是一个不错的选择。

  • If you are using an interpreter for portability and you want fast execution, compiling to a register-based virtual machine is a good choice. (David Gregg and others have shown there is less interpretive overhead in a register-based VM like the Lua VM than in a stack-based VM like the Java VM.)
  • 如果您使用解释器来实现可移植性并且想要快速执行,那么编译到基于寄存器的虚拟机是一个不错的选择。 (David Gregg和其他人已经表明,基于寄存器的虚拟机(如Lua VM)的解释开销少于基于堆栈的虚拟机(如Java VM)。)

#4


Most modern interpreters parse program to intermediate code which later is interpreted. Some store this intermediate code explicitly (eg. Python's .pyc). There are exceptions for example, shell scripts are directly interpreted, rather then parsed to intermediate format.

大多数现代解释器将程序解析为中间代码,后者将被解释。有些人明确地存储了这个中间代码(例如Python的.pyc)。例如,有例外,shell脚本被直接解释,而不是解析为中间格式。

Some more advanced "interpreters" actually do not interpret, but do JIT (just-in-time) compiling (eg. Java or .NET).

一些更高级的“解释器”实际上并不解释,而是进行JIT(即时)编译(例如Java或.NET)。

#1


Parsers don't compile. There are actually quite a few steps involved when translating a program (from say a high level language like C++ to machine code). It depends on the design if it executes in one go or after multiple passes over the input. Can you make your question a bit more specific? Meanwhile, however much you hate it, have a look here -- specially the frontend and backend sections.

解析器不编译。翻译程序时实际上涉及很多步骤(从像C ++这样的高级语言到机器代码)。如果它一次执行或多次通过输入后,它取决于设计。你能让你的问题更具体一些吗?与此同时,无论你多讨厌它,看看这里 - 特别是前端和后端部分。

#2


It depends on the language. Most modern interpreted languages (Perl, Python, and Ruby, to name a few) precompile the source code into some intermediary form to be executed at the end (citation).

这取决于语言。大多数现代解释语言(Perl,Python和Ruby,仅举几例)将源代码预编译为一些中间形式,以便在最后执行(引用)。

#3


I have written or worked with interpreters that act directly upon parsing tokens in the input, that act directly on an abstract-syntax tree (AST) built by the parser, and that translate the AST to a form designed for efficient execution. So the answer is, it depends.

我已经编写或使用解释器直接解析输入中的令牌,直接作用于解析器构建的抽象语法树(AST),并将AST转换为专为高效执行而设计的表单。所以答案是,这取决于。

  • If your target machine has 8K of RAM, a direct-parsing interpreter would be a sensible choice (think FORTH).
  • 如果你的目标机器有8K的RAM,直接解析解释器将是一个明智的选择(想想FORTH)。

  • If you are using interpreters to teach or learn about the structure and semantics of programming languages, building and interpreting an AST is a good choice.
  • 如果您使用解释器来教授或了解编程语言的结构和语义,那么构建和解释AST是一个不错的选择。

  • If you are using an interpreter for portability and you want fast execution, compiling to a register-based virtual machine is a good choice. (David Gregg and others have shown there is less interpretive overhead in a register-based VM like the Lua VM than in a stack-based VM like the Java VM.)
  • 如果您使用解释器来实现可移植性并且想要快速执行,那么编译到基于寄存器的虚拟机是一个不错的选择。 (David Gregg和其他人已经表明,基于寄存器的虚拟机(如Lua VM)的解释开销少于基于堆栈的虚拟机(如Java VM)。)

#4


Most modern interpreters parse program to intermediate code which later is interpreted. Some store this intermediate code explicitly (eg. Python's .pyc). There are exceptions for example, shell scripts are directly interpreted, rather then parsed to intermediate format.

大多数现代解释器将程序解析为中间代码,后者将被解释。有些人明确地存储了这个中间代码(例如Python的.pyc)。例如,有例外,shell脚本被直接解释,而不是解析为中间格式。

Some more advanced "interpreters" actually do not interpret, but do JIT (just-in-time) compiling (eg. Java or .NET).

一些更高级的“解释器”实际上并不解释,而是进行JIT(即时)编译(例如Java或.NET)。