Programming Assignment 4

A Compiler

Computer Science 102

Spring 1998

Due: FRI, MAY, 1, 6:00 pm

No late submissions will be accepted. .

This is a fairly simple assignment; much of the infrastrucure of the compiler has already been discussed in class.


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.

The project

In this assignment you will be asked to write the front end of a compiler for C assignment statements involving real operators only. You do this by reading an over-parenthesized assignment statement for the C language and producing from this 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. Our Turbo Pascal book on page 812 gives a program that produces an expression tree using the parentheses in the expression to determine operator precedence. The function that evaluates an expression tree appears on page 821. The program (including the evaluation function) and the IR code output for the sample input are also on the home page. It's fairly simple to change this program so that it produces a syntax tree. Alter the evaluation function so that it becomes a procedure and instead of performing calculations produces IR code.

As an example, an input of (E=((A*B)/(C-D))) produces the following IR code:

   gen_mult(A, B, T1)
   gen_minus(C, D, T2)
   gen_div(T1, T2, T3)
   gen_assign(E, T3)

where T is a temporary variable. It's subscript is simply a counter that is advanced by 1 each time the temporary variable is used.

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

'+' : Eval := plus(op1, op2, T_subscript);

where plus is the function that writes the IR code for addition; Eval is the string representing the temporary variable that is the result of the addition, for example, T3; and T_subscript would be the 3 in T3.