Assignment 2: Operational Semantics

 

Goal

To sketch an operational semantic definition for a simple imperative language as a virtual machine interpreter.

 

The language

This is the same as the language introduced in class. Its abstract syntax is:

 

P ::= S

S ::= I = E | S1;S2 | if B then S1 else S2 end | while B do S end

E ::= N | I | E1 + E2 | E1 – E2

B ::= true | false | I == E | I > E | I < E | B1 or B2 | B1 and B2 | not B

N ::= … -2 | -1 | 0 | 1 | 2 …

I ::= [any identifier]

 

All of the operators used are intended to have their usual meaning.

 

Operational Semantics

The necessary components are a virtual machine (which really means defining the “machine code” of the machine, and the structures they operate on), and the interpreter which runs on that machine. For this assignment we will target a very simple machine with the following instruction set.

Instruction Set

M is the (single) memory, organized as a sequence of locations, indexed (like an array) by an integer starting at zero, where each location can contain an integer. n is any constant integer; i, j, k are taken from the range 0..max where max is some large but finite positive integer; l, m are labels taken from the same range of positive integers. Every instruction has a label where the next instruction has a label one higher than the previous one. The first label is 1.

 

l: M[i] = n                     // put a constant in location i

l: M[i] = M[j]                // copy contents of location j into location i

l: M[i] = M[M[j]]            // indirect copy where contents of M[j] is used as index on RHS

l: M[M[j]] = M[i]     // indirect copy with index on LHS

l: M[i] = M[j] + M[k]    // addition

l: M[i] = M[j] – M[k]    // subtraction

l: if M[i] > 0 goto m  // test greater than zero and branch

l: if  M[i] = 0 goto m  // test equal to zero and branch

l: goto m                       // unconditional branch

l: gosub m                     // branch to label m and store label l+1 for return

l: return            // branch to stored label

l: read M[i]                   // copy value from input stream to location

l: write M[i]                  // copy value from location to output stream

l: halt                            // stop the machine

 

The Interpreter

Even for a simple language, given the nature of the VM’s instruction set, the interpreter would be very large. However, several issues arise when defining the interpreter. Give as much detail as you think is necessary to answer these questions for the following program:

 

p = 0

n = x

while n > 0 do

    p = p + y

    n = n – 1

end

 

  1. Since this is about semantics, we can assume that the program has been parsed and the parse tree stored in the memory M. How could this be done? (Hint: Think about the structure of the program, not its syntax, and how structures (sub-trees) can be distinguished by the interpreter.) There are many solutions, so find a simple one.
  2. Some of the leaves of the parse tree will be identifiers (p, n, x, y). The VM must store these identifiers along with a location to hold the variables’ values. This will be the model of program state represented in the interpreter. How might this be done? (Hint: the interpreter will need to look up identifiers to retrieve values and also to alter values through assignment.)
  3. Without writing the whole interpreter, what are the necessary components and how do they interact? Give the interpreter code for S1;S2 in as much detail as is necessary to show the principle of interpretation at work.
  4. Write out a “compiled” version of the program in the VM language. How is this different from interpreting the program? (Hint: are the components of the interpreter you mentioned in 3 present in the compiled version?)

 

Grading

This assignment is worth 50 points. Questions 1, 2 and 4 are worth 10 points each, and Questions 3 is worth 20 points each.

Submission

This assignment must be submitted through WebCT, so it must be prepared electronically. Read the drop box information page before submitting it. Pay close attention to the availability “window” of the assignment. When the window closes, you will not be able to submit an answer.