实现编译器和解释器有什么区别? [英] What is the difference between implementing a compiler and an interpreter?

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

问题描述

我最近读过整本龙书(只是为了好玩,我真的不打算实现一个真正的编译器),我留下这个大问题悬在我的头。

I've read the whole Dragon Book recently (just for fun, I'm not really planning to implement an actual compiler), and I was left with this big question dangling in my head.

实现编译器和解释器之间有什么区别?

What is different between implementing a compiler and an interpreter?

对我来说,编译器由以下组成:

To me a compiler is made up of:


  • Lexer

  • 解析器(用于构建语法树)

  • 生成中间代码像3个地址代码)

  • 执行所有这些疯狂的事情以优化: - )

  • 生成汇编或本机代码 3地址代码。

  • Lexer
  • Parser (which builds the syntax tree)
  • Generate Intermediate code (like 3 address code)
  • Do all these crazy things to optimize if you want :-)
  • Generate "assembly" or "native code" from the 3 address code.

现在,显然,解释器也有与编译器相同的词法分析器和解析器。

但是之后它做了什么呢?

Now, obviously, the interpreter also has the same lexer and parser as the compiler.
But what does it do after that?


  • 它是否读取语法树并直接执行? (类似具有指向树中当前节点的指令指针,并且执行是一个大树遍历加上调用栈的内存管理)(如果是这样,它是如何做的?我希望执行比一个巨大的switch语句检查它是什么类型的节点)

  • Does it "read" the syntax tree and execute it directly? (kind of like having an instruction pointer pointing to the current node in the tree, and the execution is one big tree traversal plus the memory management for the call stack) (and if so, how does it do it? I'm hoping the execution is better than a huge switch statement that checks what type of node it is)

它生成3个地址代码并解释?

Does it generate 3 address code and interpret that? (if so, how does it do it? Again, I'm looking for something more elegant than a mile long switch statement)

虚拟机的概念切入?你在一种语言中使用虚拟机是什么? (要清楚我的无知水平,对我来说虚拟机是VMWare,我不知道VM的概念如何应用于编程语言/执行程序)。

Also, at which point does the concept of "virtual machine" cut in? What do you use a virtual machine for in a language? (to be clear about my level of ignorance, to me a virtual machine is VMWare, I have no idea how the concept of VM applies to programming languages / executing programs).

如您所见,我的问题相当广泛。我主要寻找不仅使用哪种方法,而是主要是首先了解大概念,然后详细了解它的工作原理。我想要丑陋,原始的细节。显然,这更是一个寻求读的东西,而不是期望你在这里回答所有这些细节。

As you can see, my question is quite broad. I'm mostly looking for not only which method is used but mostly to first understand the big concepts, and then get into how it works in detail. I want the ugly, raw details. Obviously, this is more a quest for references to things to read rather than expecting you to answer all these details in here.

谢谢!

Daniel

Thanks!
Daniel

编辑:感谢您的回答。我意识到我的标题是误导。我理解编译器和解释器之间的功能区别。

我正在寻找的是如何实现解释器与编译器的区别。 br>
我现在明白了编译器是如何实现的,问题是解释器与这个不同。

Thank you for your answers so far. I realized my title was misleading though. I understand the "functional" difference between a compiler and an interpreter.
What i'm looking for is the difference as to how you implement an interpreter, vs a compiler.
I understand now how a compiler is implemented, the question is how an interpreter differs from that.

例如:VB6显然是一个编译器,口译员。我现在理解编译器部分。然而,我不能理解,当在IDE中运行时,它可以让我在任何点停止程序,更改代码,并恢复执行与新的代码。
这只是一个小例子,这不是我想要的答案。我试图理解,我解释下面,是我有一个解析树之后会发生什么。编译器将以目标语言生成新的代码。

For example: VB6 is clearly both a compiler and an interpreter. I understand now the compiler part. However, I can not grasp how, when running inside the IDE, it could let me stop the program at any arbitrary point, change the code, and resume execution with the new code. That's just one tiny example, it's not the answer i'm looking for. What i'm trying to understand, as I explain below, is what happens after I have a parse tree. A compiler will generate new code from it in the "target" language. What does an interpreter do?

推荐答案

p> short answer:

short answer:


  • 一个编译器将源代码转换为可执行格式供以后执行

  • 解释器评估源代码以立即执行

有很多余地是如何实现的。解释器可以生成本地机器代码,然后执行它,而虚拟机的编译器可以生成p代码而不是机器代码。线程解释语言如Forth在字典中查找关键字,并立即执行其相关的本机代码函数。

there is a great deal of leeway in how either are implemented. It is possible for an interpreter to generate native machine code and then execute that, while a compiler for a virtual machine may generate p-code instead of machine code. Threaded interpreted languages like Forth look up keywords in a dictionary and execute their associated native-code function immediately.

编译器通常可以更好地优化,因为他们有更多的时间学习代码并产生用于以后执行的文件;解释器具有较少的优化时间,因为它们倾向于在一开始就执行代码

compilers can generally optimize better because they have more time to study the code and produce a file for later execution; interpreters have less time to optimize because they tend to execute the code "as is" upon first sight

在后台优化的解释器,学习更好的方式来执行代码也可以

an interpreter that optimized in the background, learning better ways to execute the code is also possible

总结:差别在于准备代码以供日后执行或立即执行代码

summary: the difference really comes down to 'prepare the code for later execution' or 'execute the code right now'

这篇关于实现编译器和解释器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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