






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Part I: Scanner and Parser, due: 11:59pm on WebCT, Oct. 26, 2006
Part II: Semantic Analysis and Code Generation, due: 11:59pm on WebCT, Nov. 30, 2006
Total: 100 points plus 25 bonus points for each part
The goal of this project is to construct a simplified C compiler. It will not be a full-fledged C compiler, as a substantial portion of the language has been omitted to make the project feasible in one semester. Even with this simplification, the project should be done as a team effort with two to three students, and it is no small feat. You will generate a program that will take a subset of C, from now on called C∗, and generate instructions in the assembly language for a stack processor machine. You are provided with a virtual machine that emulates the stack processor and allows you to run
your compiled code. Full description of the machine is included in this document. The machine is very simple to allow you to focus on the compiler itself, rather than learning computer architectures. To complete this project you’ll use all sort of knowledge covered in class, including regular languages, grammar, parsing and syntax trees, semantics analysis, etc..
2 The language C∗
The language C∗^ is a subset of C. For detailed explanation of the functionality of C∗, please refer to ANSI C for interpretation. A C∗^ program is defined in a single file, without support for preprocessing. The language is case-sensitive. The basic types in C∗^ are int, float, and void, where void is used for function return values and for pointers. The int and float types work in the same way as their C counter- parts, and they can be mixed in expressions by automatic type conversion or type casting. From these basic variable types, it is possible to make more complex variables like struct, enum or arrays. A variable can be one of the simple or complex types, or it could be a pointer to one of those types. Our C∗^ has no memory allocation, so pointers can only be declared, manipulated, casted, assigned and operated on. C∗^ has two conditional code structures: the if statement with an optional else part, and the switch statement with one or more case parts. The ANSI C convention applies here. The conditions in the if statement must be an integer or the result of a comparison, which evaluates to an integer. On the switch cases, only integer or enum constants can be used the case statements. C∗^ supports two types of loops, including the while and the do-while statements. These structures follow the C syntax. It is possible to break the loop using the special instructions break and continue. We allow comparisons between simple types, including integer with integer, float with float, and integer with float. Comparison between structures or arrays are not allowed. Enums are actually integers, so it is OK to compare them with an integer or float. We allow comparisons using one of the following operators: {==,! =, <, ≤, >, ≥}. C∗^ has functions similar to those in C. A function returns a value, which may be a void type. Any type (a basic type or a struct) or its pointer can be a parameter to a function. All parameters are passed by value, i.e. they are copied into the function. The only way to have more than one return value is to use pointers. The scopes of variables in C∗^ work in the same way as in C. There are global variables, function variables, and then block variables (variables declared inside a block). The more nested variables have precedence. The struct type also works in the same way as in C, and it can have many members and be of basic or derived types. Their access follows the same convention as in C. An enum defines a new type to be used as variable type, which is translated to an integer. It also defines an ordered list of keywords that belong to the
4 Tasks
During the first stage of the project, you are expected to implement the scanner and the parser for C∗. You may choose to implement them using Lex and Yacc, or implement them manually. The former approach is preferred over the latter. The key output of this stage should be the syntax trees. In addition, the parser would also need to interact with the symbol table, but you may choose to implement the symbol table during Part II of the project.
4.1.1 Deliverables
For each part, you must submit your code and documentation to WebCT by the due date shown on the front of this document. The code will cover 60% of the points, and will be graded based on its correctness on generating the correct syntax trees for a number of test programs. The documentation will cover 40% of points, and it must explain the following:
4.1.2 Bonus Points
Error checking and recovery is not required for this project, but they are highly desirable for real compilers. An extra 25 points will be honored for systematic efforts on error checking and simple error recovery with useful error messages, including 15 points on the implementation and 10 points for its documentation.
In this part, your need to perform semantic analysis (including manipulation of symbol table, type checking and inference) and code generation.
4.2.1 Deliverables
As for part I, you must submit your code and documentation to WebCT by the due date. Your code will cover 60% of points, and will be graded based on its correctness on generating the correct and runnable code for a number of test programs. The documentation will cover 40% of points, and it must explain the following:
4.2.2 Bonus Points
Extra 25 points will be honored for correct implementation for code generation of the switch statements and do-while loops, including 15 points on the implementation and 10 points for its documentation.
addi push 0 store ; DECLARE LOCAL VARIABLES spi 2 prs Enter a number ini push 0 load store ; FACTORIAL CALL spi 1 ppc push 10 ; OFFSET TO END OF CALL addi push 0 load push 0 load load push 38 ;ADDRESS OF FACTORIAL rpc ; END OF CALL prs The factorial is pri push 0 load lsp push 0 store rpc ; START OF THE FACTORIAL FUNCTION push - psp addi push 0 store push 0 load load push 2
gti jnz 15 ;OFFSET TO END OF IF push 0 load load push 0 load push - addi store push 0 load lsp push 0 store rpc ; END OF IF push 0 load load ; FACTORIAL RECURSIVE CALL spi 1 ppc push 12 ;END OF RECURSIVE CALL addi push 0 load push 0 load load push - addi push 38 ;ADDRESS OF FACTORIAL rpc ; BACK FROM RECURSIVE CALL muli push 0 load push - addi store