0

I'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!

2
  • 4
    Personally I'm not quite sure what you mean by "an interpreter for a compiler program" - interpreters would theoretically be able to run any program, unless you mean that you're restricting its knowledge to a particular subset of tokens that occur in this program. In any case, if you explained more about how you expect this to work and/or what you hope to achieve by it (in particular, why javac followed by java isn't good enough), hopefully it will make it easier to understand exactly what you're after. Commented Feb 8, 2011 at 13:27
  • Some detailed example of what you want would be good, since interpreter and compiler normally exclude each other. Structured and detailed examples are always good. Commented Feb 8, 2011 at 14:08

5 Answers 5

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

..or..*take courses on compilers*, I bet that's what's the OP is doing
@bestsss - he must have skipped lectures then :-)
1

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?

Comments

1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.