CS416
Compilers: Principles, Techniques, and Tools
Fall 2005
Programming Assignment 4
Due Date: December 28, 2005
You are going to write a translation schema to execute the programs of the following toy programming language. The grammar of this toy programming language is:
program ( decls compoundstmt
decls ( decl ; decls | (
decl ( int ID = INTNUM | real ID = REALNUM
stmt ( ifstmt | assgstmt | compoundstmt
compoundstmt ( { stmts }
stmts ( stmt stmts | (
ifstmt ( if ( boolexpr ) then stmt else stmt
assgstmt ( ID = arithexpr ;
boolexp ( arithexpr boolop arithexpr
boolop ( < | > | <= | >= | ==
arithexpr ( multexpr arithexprprime
arithexprprime ( + multexpr arithexprprime | – multexpr arithexprprime | (
multexpr ( simpleexpr multexprprime
multexprprime ( * simpleexpr multexprprime | / simpleexpr multexprprime | (
simpleexpr ( ID | INTUM | REALNUM | ( arithexpr )
In this grammar, program is the start symbol. You may assume that each token is separated with at least one white space character (for simple reading). ID, INTNUM and REALNUM are token types. ID is an identifier (an identifier is a lowercase letter such as a b c … ), INTNUM is a positive integer number (starts with a digit and continues with digits). REALNUM is a positive real number (REALNUM is INTNUM.INTNUM) .
Your translation schema should read a program and print the variables of that program together with their values at the end of that program after that program is executed. If the program is incorrect, it should print an error message and quit (there is no need for error handling in this assignment). You should do type checking in your program to do correct operation for arithmetic expressions (whether integer arithmetic or real arithmetic).
For example, if your translation schema reads the following program:
int a = 1 ; int b = 2 ; real c = 3.0 ;
{ a = a + 1 ;
b = b * a ;
if ( a < b ) then c = c / 2 ; else c = c / 4 ; } Your translation schema should give the following output: a: 2 b: 4 c: 1.5 Test your translation schema with correct and incorrect programs. You should hand-in: · Your source program (email to the assistant (Rabia Nuray) ( .edu.tr)) · Some test runs of your program with correct and incorrect programs (email to the assistant) · Executable of the your program by email and a message indicating how to run your executable on a university machine. (email to the assistant) DO IT YOURSELF – CHEATING WILL BE PUNISHED