Homework 1
|
The text describes a simple language (not iterators, functions, data structures, just assignment, print and compound statements) and suggests an implementation for an interpreter. A complier for languages such as C++ and Java generally:
Our simple language will be represented not in text form but in the intermediate AST representation, delaying the need to transform text to the AST. For some examples of program text and equivalent program AST:
Text AST 37 new NumExp(37) NumExp(37) a = 37 new AssignStm("a", new NumExp(37)) AssignStm
/ \
"a" NumExp(37)a := 5 + 3 ;
print ( a ) ;new CompoundStm(
new AssignStm( "a",
new OpExp(
new NumExp(5),
OpExp.Plus,
new NumExp(3)
)
),
new PrintStm(
new LastExpList(
new IdExp("b")
)
)
);CompoundStm
/ \
AssignStm PrintStm
/ \ / \
"a" OpExp LastExpList IdExp("b")
/ | \
NumExp(5) OpExp.Plus NumExp(3)
The program can be interpreted (executed) by traversing the AST and performing the operations at each node of the AST.
Download the following files:
At IUS enter in the DOS Command window:
javac -classpath . StraightLine.java
del Program.class
javac -classpath . Interpreter.java
java -cp . Interpreter