CS 415: Compilers
Project 2/3: A Pascal Subset Front-End and Code Generator
Due dates: Friday, April 13 (Front-End), and Monday, April 30 (Code Generator)


Pascal Subset

Using flex and bison, you are to write a parser and code generator for a subset of Pascal. Programs consist of single blocks. There are no procedure declarations. Base types are limited to integer and boolean. Composit types are limited to single dimensional arrays of base types, indexed by integers. Only the following statements are included: while-do, if-then, if-then-else, assignment, write, break, and compound. Operators are restricted to arithmetic, logical, and relational. Executing a break statement causes the program to jump out of the immediately nested while loop and go (jump) to the statement following that loop. The grammar that we are using for this language is as follows:

start ::= program ID ; block .
block ::= variables cmpdstmt
variables ::= var vardcls | empty string
vardcls ::= vardcls vardcl ; | vardcl ;
vardcl ::= IDlist : type
IDlist ::= IDlist , ID | ID
type ::= array [ integer_constant .. integer_constant ] of stype
| stype
stype ::= integer | boolean
stmtlist ::= stmtlist ; stmt | stmt
stmt ::= ifstmt | astmt | wstmt | cmpdstmt | writestmt | breakstmt
wstmt ::= while condexp do stmt
ifstmt ::= ifhead then stmt else stmt | ifhead then stmt
ifhead ::= if condexp
cmpdstmt ::= begin stmtlist end
writestmt ::= writeln ( exp )
breakstmt ::= break
astmt ::= lvalue := exp
exp ::= rvalue | exp + exp | exp - exp | exp * exp
| exp and exp | exp or exp | exp exor exp | not exp
| ( exp ) | constant
condexp ::= exp != exp | exp == exp | exp < exp | exp <= exp | ID | boolean_constant
lvalue ::= ID | ID [ exp ]
rvalue ::= ID | ID [ exp ]
constant ::= integer_constant | boolean_constant
boolean_constant ::= true | false


Front-End: Project Description

As part of this project, you will




Specific Details

Names are case sensitive .

Efficiency is not the main concern of this project. You may a simple unordered list to implement your symbol table. If you design your symbol table interface well, it should be simple to replace the underlying table implementation with a hashing scheme later.

Logical operators (and, or, exor, not) require boolean arguments. The arithmetic and relational operators (e.g., +) require integer arguments. Integer and boolean arguments cannot be mixed. The equality/inequality operators (==, !=) apply to any type, as long as the two argument types match.


Provided Code

The following code is provided as a starting point for your project. You may modify any of these files.
  1. declaration of types and attribute operations: attr.h and attr.c
    There is nothing useful yet in these files.
  2. declaration of symbol table and symbol table operations: symtab.h and symtab.c
    There is nothing useful yet in these files.
  3. Scanner: scan.l (flex)
  4. Parser: parse.y (bison)
  5. Makefile
  6. demo-simple , a program that can be successfully parsed with the partial parser that can be generated from the provided code.
In order to test your compiler, we will provide test cases. The following is just a tentative list:
  1. Parser and syntax error testing (more codes will be posted later):
    demo-simple; this test case actually works with the dummy parser provided;
  2. Semantic error / type error testing:

You can generate an executable called parser by typing make . The parser expects the input on stdin, i.e., you can call the parser on an input program as follows: parser < demo-simple .


Due Date

The front-end due at 11:59 pm on April 13, 2007--that is, one minute before midnight.

If you have specific, overriding, personal reasons why these dates are unreasonable, you should discuss them directly with the instructor before the deadline.


Submission Procedure

We will use the web-based handin facility. You need submit the following files: scan.l, attr.h, attr.c, symtab.h, symtab.c, parse.y, ReadMe . The ReadMe file contains may contain comments that you want the grader to know about. It is your reponsibility to verify that your files have been submitted correctly .


Grading Criteria

The project will be mainly graded on functionality. You will receive no credit for the entire project if your front-end code does not compile on remus . We may use automatic testing tools, so make sure that your error messages are exactly as described above.


Code Generator: Project Description

You will write a syntax directed translation scheme that will generate ILOC code for the above language. You may test the correctness of your generated ILOC code by running it on the ILOC simulator provided as part of project 1 (~uli/cs415/spring07/proj1 on the undergraduate cluster.)

Code Shape Requirements


Provided Code

You are expected to extend your solution for the first part of the project (Front End) for the code generation part. However, you may assume that your input programs for this project do not have any parsing or static semantics errors (e.g., no type errors).

You will need to modify files parse.y , attr.h , attr.c , symtab.h , symtab.c , and Makefile .
  1. Parser/Code Generator: parse.y Here is where most of your code will go, as in the first part of the project. Use calls to procedure emit to generate code. This will write the resulting ILOC code into file iloc.out. Please modify your own version of parse.y from part1 of this project accordingly .
  2. instrutil.h and instrutil.c . *** DO NOT MODIFY ***
  3. You may modify your own Makefile of the first part of the project to include the files instrutil.h and instrutil.c , or use Makefile
In order to test your compiler, we will provide some test cases. The test cases will be posted soon. We will provide a sample solution codegen-sol of the required version that will run on the undergraduate cluster. You can use this compiler to run on your own sample codes. This compiler also emits comments. Your compiler is not required to emit any comments, but it may be useful for you to do so. You should use the provided procedure emitComment for this purpose.

Due Date

The code generator is due at 11:59 pm on April 30, 2007--that is, one minute before midnight.

If you have specific, overriding, personal reasons why these dates are unreasonable, you should discuss them directly with the instructor before the deadline.


Submission Procedure

Again, we will use the web-based handin facility. You need submit the following files: attr.h, attr.c, symtab.h, symtab.c, parse.y, ReadMe . The ReadMe file contains may contain comments that you want the grader to know about. It is your reponsibility to verify that your files have been submitted correctly .

Grading Criteria

As the Front End, the this project will be mainly graded on functionality. You will receive no credit for the entire project if your code generator does not compile on remus .