CSE 304: Compiler Design

Fall 2009

Homework #1

Due: Mon, Sep 21


This is a programming assignment. You are expected to write a program that implements an interpreter for a simple stack-based assembly language. Your program should be written in C or C++ and must use lex (or flex), the lexican analysis generator. Your program should take input from standard input (stdin or cin in C and C++, respectively) and write output to standard output (stdout/cout) and error messages (if any) to standard error (stderr/cerr). In addition to training you in the use of (f)lex, this assignment will be a mild test of your C/C++ programming skills.

The program's input consists of a sequence of instructions for a stack machine. This machine has no general purpose registers. All data is kept on a stack, and the machine has instructions to manipulate the top (few) entries on the stack. The machine manipulates only integer data.

The assembly language consists of the following instructions.

An assembly language program is a sequence of instructions. Each instruction may be preceded by an optional label and a semi-colon (":"). A label is a sequence of alphabetic characters or numeric characters or underscore ("_"), beginning with an alphabetic character. An integer num is a sequence of numeric characters, optionally preceded by a minus ("-"). The instructions in the assembly language should always be in lower-case.

Note that instructions ildc, jz, jnz and jmp take one argument; the other instructions have no arguments.

The label may be separated from the following instruction by a sequence of zero or more whitespace (blank, tab, newline) characters. An instruction and its argument will be separated by a sequence of one or more whitespace characters. In the examples below, each instruction is on a line of its own; in general, though, there may be more than one instruction in a line; or a single instruction may be split over multiple lines.

The input assembly program may also have comments. A comment begins with "#" symbol and ends at the end of the line.

When a program is evaluated, it starts with an empty stack. Program evaluation continues until the last instruction in the program is evaluated. When an input program is completely evaluated, the interpreter should print the top-most value on the stack and exit.

If an input program has errors (e.g., improperly formed labels, invalid instruction, invalid format for numbers, labels that appear in destinations of jumps but not defined elsewhere, etc.), the interpreter should give an error message and exit. The details (i.e. the source of the error, line number, etc.) in the error message are up to you. All I expect is that your interpreter should at least say that there is some error and exit instead of trying to execute an erroneous program.

Similarly, when executing a program, if the program tries to access values in an empty stack, the interpreter should signal an error and exit. Again, you can choose the error message format.

Examples:

Project Path:

First assume that your input will always be only valid (error-free) programs without labels. Example 1 above is one such program. Write a lexical analysis specification for executing such programs. You may use the given "Postfix Calculator" source (see here for tar archive of source) as a template for this step.

Second, consider programs without labels that may have errors. Now you have to first check for errors before you try to execute an input program. Modify your code to do this.

Finally consider programs with labels and comments.

Grading:

The homework itself is worth 3% of your overall grade. It will be graded out of 6 points. Your score will then be scaled down (by a factor of 2) when it is used to compute your overall grade.

We will use the following criteria to evaluate your program.

Submission:

Your submission will consist of a directory (folder) of the following form. Tar the entire folder, e.g. go up one level from the source direcrtory and say
      tar   cvf   yourid.tar   yourid
    
to create tha tar file yourid.tar, and submit the tar file via Blackboard. See Homework #1 in Assignments area of Blackboard for the submission form, and please make sure you submit the homework before midnight of Mon., Sep. 21.

Note: The way Blackboard is set up, you can submit this assignment only once. To avoid problems, please submit only when you are ready!


C.R. Ramakrishnan