开发者

Programming an Interpreter for a Compiler

I'开发者_C百科m writing an interpreter for a compiler program in 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? then feed the source code again to run it in java?

I've heard of using the Java Compiler API, would this be a good plan?

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:

  1. The first few steps I guess you already done (Design/describe the language semantics, construct AST and perform required validation of the code)
  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. Here you can see how Java byte code looks like 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)

P.S. 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


You really need to read some books and/or take courses on compilers - this can't be solved by a two-paragraph answer on SO.


You could create a cross-compiler which reads your language and outputs Java code to do the same thing. This may be the simplest option.

The Java Compiler API can be used to compile Java code. You would need to translate your existing code to Java first to use it.

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. See BSF and Java 5 scripting (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


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? Without generating byte codes? My idea was to translate each statement into Java equivalent statements, and let Java handle the byte code generation.

I would look into the topics mentioned. Again, thank you very much for the suggestions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜