


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Project; Class: COMPILER CONSTRUCTION; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2006;
Typology: Study Guides, Projects, Research
1 / 4
This page cannot be seen from the preview
Don't miss anything!



This assignment is an extension to Project 2, in which you developed a Bison specification file for the language โcoreโ as described in this handout.
In this addition to your compiler project we will add the intermediate code generation facilty for the expres- sions we have discussed in class and a few more. In particular the expressions are given by the following grammar:
| โ(โ INT โ)โ E convert E to int | โ(โ FLOAT โ)โ E convert E to float
For these operators your parser should assume/enforce the following precedences and associativities. The precedences decrease as we go down in the table.
Operators Associativity Description unary * prefix right to left dereference pointer unary & prefix right to left address of operator ห prefix right to left exponentiation unary - right to left unary minus *, / left to right +, - left to right
3 Pointers
To handle the address-of and dereference operators we need also to extend the grammar for declarations to allow for pointers to ints and floats. In this still simplified type world you can have pointers to pointers ยท ยท ยท but the only base types are ints and floats. So eventually dereferencing a pointer over and over will eventually lead to one of int or float. For example
int **p, q; // declares p to be a pointer to a pointer to an int
To handle types of this nature you basically need two pieces of information to be saved in the symbol table the base type and the dereference level. In the example above both โpโ and โqโ have base-type = int. The dereference level of โpโ is two and the level of q is 0. So we need to augment the symbol table to include type (int, float) and level (dereference level to get to base type).
In C integer expressions are promoted automatically when operate with a float. So for example consider the declarations:
int i; float f, sum;
Then in the expression โsum := i + f;โ before doing the addition in C the โiโ will be converted(promoted) to a float and then floating point addition is performed. In our language there is no implicit promotion like this. This example should be considered a โmixed type expression errorโ and an appropriate error message including the line number and last token (lexeme) encountered should be printed. To write this correctly in our โcoreโ one must explicitly cast the expression to float as in โsum := (float) i + f;โ
4 Intermediate Code Generation
To be able to support the translation assume that we have the following opcodes in our intermediate lan- guage.
(a) mycc - my core compiler (b) clean - remove all executables (c) nodebug - make a non debugging version of mycc (d) test run mycc on your test files