CS 5363 Programming Languages and Compilers
Translating Assignment Statements


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. For this assignment, you are to translate a subset of the language of the recursive descent parser into MIPS assembly language. The portion has to do with arithmetic expressions and assignments, but not relational or logical operators. First a few initial examples are presented. Then the portions of the grammar involved in the translation are highlighted. Finally a strategy for translation is presented.

Initial Examples of Semantic Actions:

Consider the recursive descent parser for the following grammar giving arithmetic expressions.

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:

Reference Material About MIPS:

Initial Work on Assignment: For this assignment and the next two, you are to translate portions of the language described in the previous assignment (the recursive descent parser) into MIPS assembly code.

For this recitation you should ignore all statements except assignments and output statements. In particular, ignore function definitions, ignore the exponentiation operator ^, and ignore all 6 relational operators and both logical operators. This subset also uses single-character identifiers and single-character integers.

The output statements are of the form:

Portions of the Grammar to Implement (highlighted in red bold below):

Form of assembler output: There are many different forms the output could take. I am suggesting one form here but please do not consider yourself limited to this form.

How to do the actual translation: You will be adding code to the affected portions of the recursive descent parser. As a hint for completing the assignment, you can study the code above that evaluates an arithmetic expression as a double, since the code you need is actually quite similar.

In this case, instead of returning a double giving the value of an expression, each of the various functions of the parser should return an integer giving the location in memory where the given operand or expression is to be found.

One very simple strategy I used above is for memory locations 0 to 9 to hole constants 0 to 9, memory locations 10 to 35 to hold values of variables a to z, and memory locations 36 to 85 for to hold values of 50 temporaries.

What to Turn In for the Assignment: You should turn in a source listing of your compiler, a listing of the MIPS code you generate, and a listing of the output when you run the MIPS code. It is essential that your actually run your MIPS code in order to check for errors. Just using the single red source below will be sufficient for your run, though you should use other source if you have trouble with this one.

Extensions to Handle Arbitrary Identifiers and Integers: If you want to use arbitrary identifiers, then you will need a symbol table for them and will need keep track of the memory location of each identifier. Arbitrary integers could be conveniently handled by creating each integer as it occurs using an addi instruction. (Better would be to have a table of integer constants, either combined with the first table or separate.) Of course, even with single-character identifiers, you could still use a symbol table.

Suggestions for Debugging: It is important to issue error messages for parser errors, because you may very well have trouble with your parser. You should also keep the parser code as it is and create the compiler as a separate program. Then if you get a new parser error, you can debug it with the simpler parser before working on the compiler.

I also recommend outputting assembler comments, perhaps similar to the ones shown above. That may help you track down errors.


Revision date: 2002-10-20. (Use ISO 8601, an International Standard.)