简体   繁体   English

为编译器编程解释器

[英]Programming an Interpreter for a Compiler

I'm writing an interpreter for a compiler program in Java. 我正在用Java编写编译器程序的解释器。 So after checking the source code, syntax and semantics, I want to be able to run the source code, which is the input for my compiler. 因此,在检查了源代码,语法和语义之后,我希望能够运行源代码,这是我的编译器的输入。 I'm just wondering if I can just translate some tokens, for example, out (it prints stuff on screen), can I just replace it with System.out.print? 我只是想知道我是否可以只翻译一些令牌,例如,输出(它在屏幕上打印内容),是否可以将其替换为System.out.print? then feed the source code again to run it in java? 然后再次提供源代码以在Java中运行它?

I've heard of using the Java Compiler API, would this be a good plan? 我听说过使用Java Compiler API,这会是一个好计划吗?

Thank you very much in advance! 提前非常感谢您!

What you asking is a virtual machine implementation technique, to run your Java code in general you should implement following: 您要问的是一种虚拟机实现技术,通常要运行Java代码,您应该实现以下目标:

  1. The first few steps I guess you already done (Design/describe the language semantics, construct AST and perform required validation of the code) 我想您已经完成了前几个步骤(设计/描述语言语义,构造AST并执行所需的代码验证)
  2. You need to generate your byte code, original Java works exactly in the same way, it generates another representation of the source code, from human readable to machine readable. 您需要生成字节代码,原始Java的工作方式完全相同,它生成源代码的另一种表示形式,从人类可读到机器可读。 Here you can see how Java byte code looks like http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/ 在这里,您可以看到Java字节码的外观,例如http://www.ibm.com/developerworks/ibm/library/it-haggar_bytecode/
  3. You need to implement virtual aka stack machine that reads byte code and runs it for execution. 您需要实现一个虚拟的又名堆栈计算机,该计算机读取字节码并运行它以执行。

So as you can see you should have 3 separated components (projects) for your task: 1. Language grammar 2. Compiler (byte code generator) 3. Virtual machine (interpreter of byte code) 如您所见,您应该为任务分配3个独立的组件(项目):1.语言语法2.编译器(字节码生成器)3.虚拟机(字节码解释器)

PS I have experience in creation of tiny Java similar compiler from scratch (define grammar with ANTlr, implementation of compiler, implementation of virtual machine), so probably I can share more information with you (even source code) if you need something particular PS我有从零开始创建小型Java类似编译器的经验(使用ANTlr定义语法,编译器的实现,虚拟机的实现),所以如果您需要一些特殊的东西,也许我可以与您共享更多信息(甚至是源代码)

您确实需要阅读一些书籍和/或参加有关编译器的课程-这不能通过SO的两段式答案来解决。

You could create a cross-compiler which reads your language and outputs Java code to do the same thing. 您可以创建一个交叉编译器,以读取您的语言并输出Java代码以完成相同的操作。 This may be the simplest option. 这可能是最简单的选择。

The Java Compiler API can be used to compile Java code. Java编译器API可用于编译Java代码。 You would need to translate your existing code to Java first to use it. 您需要先将现有代码转换为Java才能使用它。

This would not be the same thing as writing an interpreter. 这与编写解释器不一样。 Is this homework? 这是作业吗? Does the task say you have to write the interpreter or can you have the code run any way which works? 任务是说您必须编写解释器,还是可以使代码以任何可行的方式运行?

Unfortunately you did not mention which scripting language are you planning to support. 不幸的是,您没有提到您打算支持哪种脚本语言。 If it is one of well known languages, just use its ready interpreter written in pure java. 如果它是众所周知的语言之一,则只需使用以纯Java编写的现成解释器即可。 See BSF and Java 5 scripting (http://www.ibm.com/developerworks/java/library/j-javascripting1/) 请参阅BSF和Java 5脚本(http://www.ibm.com/developerworks/java/library/j-javascripting1/)

It it is your own language 这是你自己的语言

  1. think twice: do you really need it? 请三思:您真的需要吗?
  2. If you are sure you need your own language think about JavaCC 如果您确定需要自己的语言,请考虑一下JavaCC

First of all, thank you very much for the fast replies. 首先,非常感谢您的快速答复。

As part of our compiler project, we need to be able to compile and run a program written in our own specified language. 作为我们的编译器项目的一部分,我们需要能够编译和运行以我们自己的指定语言编写的程序。 The language is very similar to C. I am confused on how an interpreter works, is there a simpler way to implement this? 该语言与C非常相似。我对解释器的工作方式感到困惑,是否有更简单的方法来实现此功能? Without generating byte codes? 没有生成字节码? My idea was to translate each statement into Java equivalent statements, and let Java handle the byte code generation. 我的想法是将每个语句转换为等效的Java语句,并让Java处理字节码生成。

I would look into the topics mentioned. 我将研究提到的主题。 Again, thank you very much for the suggestions. 再次感谢您的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM