Assignment 4

The Operational Semantics of Type Compatibility

Goals

To understand the differences between pure name compatibility of types and to implement type checking in Rigal accordingly.

Procedure

Working with the parser/interpreter posted on the web site (www.cs.nmsu.edu/~rth/cs/cs471/SimpleInterpreterDeclarations.rig) modify the interpreter code to implement a modifed form of name compatibility (see Hints, below).

Use the following simple programs to test your code (note the comments are not part of the program):

 

t1=INTEGER;
x:INTEGER;
y:t1;
x=1;        // name compatible: yes
y=2;        // name compatible: yes
x=y         // name compatible: yes (base types are same)
 
t1=RECORD x:INTEGER;y:INTEGER END;
t2=RECORD a:INTEGER;b:INTEGER END;
x1:t1;
x2:t1;
y:t2;
x1.x=10;
x2.y=5;
y.a=1;
y.b=2;
x1=x2;      // name compatible: yes
x1=y        // name compatible: no

Hints

§         name compatibility can be strict, when only when the types of the receiving variable and the value to be assigned have the same name; however, when a type declaration simple renames an existing type (e.g. t1=INTEGER; t2=t1) this can expand into a slightly different version of name compatibility, sometimes called transitive name compatibility; you should implement this modified form

§         understand the sample interpreter first before modifying code; if there is something you don’t like, let me know before you change the code – you may be altering the intended semantics of the language!

§         look at the output of the parser for the abstract syntax trees corresponding to each program in order to guide your new code

§         you will need to modify the code for assignment and for evaluation of expressions (the RHS of an assignment); you will also have to solve the problem of how to determine (and therefore check) the type of an expression such as, x + 1, where x is declared INTEGER

§         print out a message for assignments with incompatibilities but don’t halt the interpreter, so that all assignments can be checked

§         don’t just write code for the sample programs – write code that will work for any program; write other (simpler) test programs to test your methods and your coding

§         constants, like 2, -345, are compatible with any INTEGER type (see section 5.7 in Sebesta)

Submission

Turn in hard copy of your modified interpreter and evidence that it produces the right results (a print showing your interpreter running on the two examples given above). Also email me (rth@cs.nmsu.edu) your source code file.

Grading

Total 50 points. Partial credit will be available for answers that are along the right lines.

Due Date

February 14th 5:00pm.