Project 3 - Generating Intermediate Code for Expressions | CSCE 531, Study Guides, Projects, Research of Computer Science

Material Type: Project; Class: COMPILER CONSTRUCTION; Subject: Computer Science & Engineering; University: University of South Carolina - Columbia; Term: Spring 2006;

Typology: Study Guides, Projects, Research

Pre 2010

Uploaded on 09/02/2009

koofers-user-b1g
koofers-user-b1g ๐Ÿ‡บ๐Ÿ‡ธ

5

(1)

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCE 531, Spring 2006, Matthews
Project 3: Generating Intermediate Code for Expressions
Assigned: March 3, Due: March 19, 11:59PM
March 15, 2006
1 Overview
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.
2 Arithmetic Expressions
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:
Eโ†’E โ€™+โ€™ E
|E โ€™-โ€™ E
|E โ€™*โ€™ E
|E โ€™/โ€™ E
|โ€™-โ€™ E
|โ€™(โ€™ E โ€™)โ€™
|โ€™&โ€™ E
|โ€™*โ€™ E
|โ€™(โ€™ 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.
1
pf3
pf4

Partial preview of the text

Download Project 3 - Generating Intermediate Code for Expressions | CSCE 531 and more Study Guides, Projects, Research Computer Science in PDF only on Docsity!

CSCE 531, Spring 2006, Matthews

Project 3: Generating Intermediate Code for Expressions

Assigned: March 3, Due: March 19, 11:59PM

March 15, 2006

1 Overview

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.

2 Arithmetic Expressions

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:

E โ†’ E โ€™+โ€™ E

| E โ€™-โ€™ E

| E โ€™*โ€™ E

| E โ€™/โ€™ E

| โ€™-โ€™ E

| โ€™(โ€™ E โ€™)โ€™

| โ€™&โ€™ E

| โ€™*โ€™ E

| โ€™(โ€™ 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).

3.1 Type errors in expressions

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

  1. test1, ยท ยท ยท testn - test files for your compiler
  2. out1, ยท ยท ยท outn - the output from testing your compiler