Homework 4
|
Homework 1 implemented an interpreter for a simple language called StraightLine (not iterators, functions, data structures, just assignment, print and compound statements). A parser constructs an Abstract Syntax Tree (AST) from a program's text, from Homework 1 we know that the program can be interpreted while traversing the AST. In Homework 1 StraightLine programs were entered in the intermediate AST representation as below:
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)Interpreting the parser produced AST is essentially the function of scripting languages such as Perl, Python, etc.
Chapter 4 notes discussed concrete and abstract grammars, we will use the abstract grammar transformation of SableCC3.
Download the following files:
Home: java -jar \sablecc-3-beta.3.altgen.20041114\lib\sablecc.jar StraightLine.js
IUS: java -jar
v:\common\user\C431\sablecc.jar StraightLine.js
del StraightLine\*.class
javac -classpath . StraightLine\*.java
java -cp . StraightLine.Main < test1
- Cover sheet with your name, C431, date, and Homework 4.
- Print out of Interpreter.
- Email Interpreter.java to: with subject: HW4 Solution
- Print out of test results for test1 and test2.
Chapter 4 - CONCRETE versus ABSTRACT SYNTAX
SableCC3 - Concrete to Abstract tutorial