CS 440/540
Program 3

due: June 16, 2008

The Program

You will finish code generation for all of Paxi except for subprograms (procedures), local variables, and parameters. Your compiler will generate programs having only one subprogram (main) with no local variables and no procedure calls but otherwise will compile the entire language.

The pieces you will add for this program are the writestr statement for quoted strings, Boolean expressions, and control structures.

Boolean Expressions

Boolean expressions are described in productions 39 through 43 of the Paxi Language Definition. They involve the relational operators (= , < , <=, >, >=, and #) and Boolean operators (and, or, not).

A Boolean atom such as (var1 < var2) should leave a value 0 or 1 on the stack. Each of var1 and var2 in the above example will result in a value being left on the stack (by arithmetic_expression). You will generate code to subtract these two values, test the result, and push either 0 or 1 onto the stack. The code generated will use a goto and a label and will depend on the relational operator which appears in the Boolean atom. Details will be discussed in class.

Boolean expressions (using operators and, or, and not) are implemented in a manner similar to arithmetic expressions. Here you will leave integer values on the stack as the values of Boolean expressions, Boolean terms, and Boolean factors: 0 will be treated as false and 1 will be treated as true.

Control Structures

The only control structures are if ... then ... else ... endif, while ... endwhile, and do ... endo which are described in productions 19 through 23 of the Paxi Language Definition. Each of these control structures involves evaluating a Boolean expression and transferring control to a corresponding point in the program. Transfer of control is done with a goto and a label. A number of issues which arise in doing this will be discussed in class.

The Read, Readstr, Write, and Writestr Statements

The skeleton C source file contains functions for read, readstr, and writestr. You need only generate code which calls these functions. For readstr and writestr you will need to read a location from a symbol table and print the int found there as a string. If you will store this string in a char * array called buffer an appropriate C function call is:

sprintf(buffer, "%d", i);
where i is the integer to be converted to a string.

In the case where writestr is writing a string literal your code will print an element from the stringlist. This is a copy of the stringlist from your Program 1 which must be copied into your C output file. In the skeleton code you will see:


char *stringlist[] = {};
You will write the stringlist from your Program 1 into the braces here. A simple example from a stringlist of length three might be:

char *stringlist[] = 
{
"this is the first string",
"and here is a second string",
"and finally a third string"
};

To Hand In

You will hand in a source listing and a sample terminal session. The source listing should include your yacc file and any relevant Java or C/C++ files (you need not turn in your lex file). Sample data will be supplied later.