Programming Assignment 4, A Compiler

Computer Science 102

Spring 2000

Due: FRI, APR, 28, 11:59 pm (midnight)

No late submissions will be accepted. .

This is a fairly simple assignment; much of infrastrucure of the compiler has already been discussed in class or done on the last exam as the 4th problem.


Your project is to design a program that implements a simple compiler. A compiler translate a high level source code program into an assembly language program and consists of a front end and a back end. The front end of a compiler has the following components:

The front end is language dependent but machine independent. On the other hand, the back end is machine dependent and language independent. Thus the back end written for the Pentium processor should be able to take the IR code for any language and translate it into Pentium assembly code. In this assignment you will be asked to write the front end of a compiler for simple assignment statements.

The project

You are being asked to read a postfix assignment statement for the C language and produce IR code. You can download this statement from the home page. This statement has been written so that each token is one character long. It should be read by your program as a text file. Write a function called root that forms an expression tree from the postfix input (it was the answer to question 4 on the last exam). The function that evaluates an expression tree appears on page 821. The evaluation function and the IR code output for the sample input are also on the home page. Alter the evaluation function so that it instead of performing calculations produces IR code. The function heading should be:

FUNCTION Evaluate(P: Tree; var j: integer): string;

where j is the subscript of the temporary variable T, ,e.g., T3 as explained below:

As an example, an input of EAB*CD-/= produces the following IR code:

   gen_mult(A, B, T6)
   gen_minus(C, D, T7)
   gen_div(T6, T7, T3)
   gen_assign(E, T3)

where T is a temporary variable. It's subscript is the counter j used as the second parameter in function Evaluate.

You will have to alter the case statement in the function Evaluate so that, for instance, the "+" option is written as:

  '+': gen_plus( op1, op2, Eval);

where gen_plus is a procedure that writes the IR code for addition and Eval is the string representing the temporary variable that is the result of the addition.

Use the version of stackadt.pas on the WEB that employs a linked list:

To facilitate the formation of the T's, use the str function, page 494 in the text.