CS 3723/3721 Programming Languages
Tiny Compiler 2: Semantic Actions --
Translation of 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.

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

Initial Examples of Semantic Actions Based on the Above Grammar:

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):

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.

What to turn in to the recitation instructor:

Key ideas:


Revision date: 2002-12-19. (Use ISO 8601, an International Standard.)