哪个是好的且简单的中间代码?
假设作业的目的是编写一个适用于 C 语言子集的编译器(您可以假设任何语言的子集,仅支持基本的脚本表达能力,而无需将复杂的事物作为对象)。
什么样的中间代码可以用来验证编译器的正确性?我正在与一位教授交谈,他谈到了这样一个事实:他不知道应该为他的学生提供什么作为用于“编译代码”的虚拟机,所以我想知道这可能是一个好的解决方案。
Subset of C -> Compiler -> Code? -> VM
其中代码可以是二进制格式,也可以是更好的 ASCII 格式(类似于伪 asm)。
我正在寻找已经制作好的东西,而不是如何构建这个中间代码和VM,只是一个简单的准备好用于测试一些编译程序的东西。
Suppose that the purpose of an assignment is to write a compiler that works on a subset of C language (you can assume a subset of whatever language, just supporting basic scripting expressiveness without having complex things as objects).
What kind of intermediate code could be used to verify the correctness of the compiler? I was talking with a professor and he spoke about the fact that he didn't know what to give to his students as the VM to be used for the "compiled code" so I wondered which could be a good solution.
Subset of C -> Compiler -> Code? -> VM
in which code could be either in binary format or better in an ASCII format (something like pseudo-asm).
I'm looking for something already made, not how to structure this intermediate code and the VM, just an easy and simple one ready to be used to test some compiled programs..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以描述一些抽象的机器设计,然后为其提供列表格式的指令集。我的小型 LISP 解析器是解析器中的轻而易举的事。
另外,编写一个 lisp 解释器来读取此内容也是轻而易举的事情。
You could describe some abstract machine design and then provide it an instruction set in list-format. I small LISP parser is a nobrainer in parsers.
Also, writing a lisp interpreter to read this in is a nobrainer.
您可以在现有虚拟机中找到许多中间代码/字节码示例。根据您的定义,它们可能很简单,也可能不简单。示例:
You can find many examples of intermediate code/bytecode in existing VMs. Depending on your definition, they may or may not be simple. Examples:
编译为脚本语言(例如 JavaScript)怎么样?它是人类可读的并且已经制作完成。
How about compiling to a scripting language (e.g. JavaScript)? It's human-readable and already made.
针对 Java 虚拟机怎么样?不确定它有多简单,但它有很好的文档记录,因此如果学生感到好奇,他们可以前往 amazon.com 并获取一本有关中间代码的实际含义以及虚拟机如何工作的书。
您还可以创建真正的 80x86 或 68000 程序集,使用汇编器获取机器代码,然后使用模拟器运行它。如果你已经编写了一个编译器并且它已经有大量的调试器和其他可用的实用程序,那么真正的硬件并不会让我觉得比一些虚构的虚拟机更复杂。
但我确实喜欢 LISP 建议:-)
How about targeting the Java Virtual Machine? Not sure how simple it is but it is very well documented, so if the students where curious, they could head over to amazon.com and get a book about what the intermediate code actually means and how the vm works.
You could also just create real 80x86 or 68000 assembly, use an assembler to get machine code and then use an emulator to run it. Real hardware doesn't strike me as more complicated than some made-up VM if you're already gone through writing a compiler and it has tons of debuggers and other utilities available already.
But I do like the LISP suggestion :-)
llvm 怎么样?
How about llvm?