Homework 4
Interpreter of Straight-line Program  

powered by FreeFind

Modified: 

Overview

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. 

Assignment

  1. complete the given skeleton of the Interpreter
  2. interpret the 2 given StraightLine programs. The skeleton will interpret only the test1 StraightLine program given below.   

Files

Download the following files:

  1. Interpreter.java - Interpreter skeleton.
  2. Main.java - Calls lexer, parser and interpreter.
  3. StraightLine.js - StraightLine grammar.
  4. Test 1, 2 - test programs.

Getting Started

Turn In

  1. Cover sheet with your name, C431, date, and Homework 4.
  2. Print out of Interpreter.
  3. Email Interpreter.java to: with subject: HW4 Solution
  4. Print out of test results for test1 and test2.

Resources

Chapter 4 - CONCRETE versus ABSTRACT SYNTAX

SableCC3 - Concrete to Abstract tutorial