Code for Interpreter which takes JAVA program as input and performs simple arithmetic subtraction: Code for extended interpreter which performs print, arithmetic operations, function calls, loops etc with integers as well as doubles using regex for lexical analysis and ast for parsing:
javac JavaSubtractionInterpreter.java
java JavaSubtractionInterpreter
You can find some examples of the language in line, Only change this line for testing different test cases.
4
The grammar is C-like
For print with newline
println ""
replace double quotes with anystring you want to print or any variable.
For defining integers:
int x = 10
int y = -5
int res = x - y
where x
, y
, res
and the values 10, -5 can be replaced by any valid name. The convention of variable names is similar to that of c.
For remaining grammar the test file can be referred
You can find some examples of the language in src/main/resources
The result of subtraction is as in the start of the output in the sample case.
ERROR
Compile it using the maven command: (If maven not installed install using sudo apt install maven
in ubuntu)
mvn package
A jar file will be created in the target
folder. Then, to run the interpreter, type in the terminal:
java -jar target/interpreter-1.0.jar src/main/resources/test.c+-