Programming Assignment 4

A Compiler

Computer Science 102

Spring 2001

Due: TUE, APR 24 11:59 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.

Introduction

Your project is to design a program that implements a simple compiler. A compiler translates 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. It takes the IR code and translates it into the assembly language for a specific machine. 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 Java assignment statements involving real operators only. You do this by reading a postfix expression assignment statement for the Java 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 file. Write a method called build() that builds an expression tree from a postfix expression (it was the answer to question 4 on the last exam.) The function that evaluates an expression tree appears at the URL http://cs.nyu.edu/courses/spring01/V22.0102-002/apr5/expresstree.java

The IR code output for the sample input are also on the home page. Alter the evaluation method so that instead of performing calculations, it produces IR code.

As an example, an input of EAB*CD-/= 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 switch statement in the method 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.

For the stack, use the version on the WEB that is a stack of Object at the URL http://cs.nyu.edu/courses/spring01/V22.0102-002/apr5/sock.java