(Click or use local copy.)

 CS 3721, Spring 2004
 Programming Languages

 Recitation 6: February 16, 18
 Tiny Compiler 1: Translate Assignments
    MW   09:00-09:50 pm, SB 3.01.04
 Due: 2004-02-23 23:59:59

Recitation 6 must be submitted following directions at: submissions on or before
  • 2004-02-23  23:59:59 (that's Monday, 23 February 2004, 11:59:59 pm) for full credit.
  • 2004-02-27  23:59:59 (that's Friday, 27 February 2004, 11:59:59 pm) for 75% credit.


Overview: The term semantic action refers to extra code added to a parser so that it will carry out some task, often translating from the language the grammar describes to some other language. During the course of the parse, the function calls implicitly carry out a complete traversal of the parse tree, though the actual parse tree is not constructed. During this traversal, extra code can be inserted to do the translation task. This process is known as syntax-directed translation.


Initial Examples of Semantic Actions: Consider a recursive descent parser for the grammar giving arithmetic expressions.

Arithmetic Expressions
P ---> E '$'
E ---> T {('+'|'-') T}
T ---> S {('*'|'/') S}
S ---> F '^' S | F
F ---> '(' E ')' | char

It is easy to add extra code to this parser so that it will translate arithmetic expressions in to reverse Polish notation (RPN). So little additional code is needed that this example illustrates the power of this approach.

Here is another example of semantic actions: an evaluator of arithmetic expressions:


Initial Recitation work: For this recitation and the next one, you are to translate the language described in the previous recitation into MIPS assembly code.

For this recitation you should ignore all statements except assignments and output statements, which are of the form:

The recitation is also ignoring the ^ operator. These are what you should translate.


Form of assembler output: There are many forms the output could take. I am suggesting one form here:
How to do the actual translation: This first part of the recitation is only using part of the grammar (just for assignments and output):

Simplified Tiny Grammar
M  --->  { S } '#'
S  --->  A  |  P  |  C
A  --->  lower-case '=' E ';'
P  --->  '<'  E  ';'
C  --->  '<'  upper-case ';'
E  --->  T {('+' | '-') T}
T  --->  U {('*' | '/' | '%') U}
U  --->  F
F  --->  '(' E ')'  |  lower-case  |  digit

As a hint for completing the recitation, you can work with the evaluator code above, since the code you need is actually quite similar. Each of the various functions of the parser must return an integer giving the location in memory where the given operand or expression is to be found: memory locations 0 to 9 for constants 0 to 9, locations 10 to 35 for variables a to z, and locations 36 to 85 for 50 temporaries.


Sample Inputs:
What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item letters: a, b, and c.

 Contents of submission for Recitation 6:

Last Name, First Name; Course Number; Recitation Number (6).

a. Java or C++ code for your translator, perhaps in multiple files.

b. Results of a run of your program using Sample Input 1 above, giving the complete resulting MIPS code.

c. Results of executing the MIPS code in b using the SPIM simulator.

d. Results of a run of your program using Sample Input 2 above, giving the complete resulting MIPS code.

e. Results of executing the MIPS code in d using the SPIM simulator.

f. Results of a run of your program using Sample Input 3 above, giving the complete resulting MIPS code.

g. Results of executing the MIPS code in f using the SPIM simulator.


Key ideas: It is relatively easy to add code to the recursive-descent parser that will carry out tasks, such as translating input assignment statements into MIPS assembler code. This process is known as syntax-directed traslation. It is an extremely powerful technique for writing compilers and for other similar tasks.


Revision date: 2004-01-04. (Use ISO 8601, an International Standard.)