Assignment II for Compiler Construction | EECS 665, Assignments of Electrical and Electronics Engineering

Material Type: Assignment; Professor: Kulkarni; Class: Compiler Construction; Subject: Elect Engr & Computer Science; University: University of Kansas; Term: Fall 2008;

Typology: Assignments

Pre 2010

Uploaded on 03/11/2009

koofers-user-7lc
koofers-user-7lc 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
EECS 665 - Fall 2008
Assignment 2
cexpr - calculator for C integer expressions
Your assignment is to write a program using yacc and lex that implements a calculator of C
integer expressions. The calculator will process expressions until it encounters EOF or invalid
syntax. Each calculation is terminated by a semicolon. Tokens can be separated by whitespace
(but no comments). After each calculation, the calculator prints its result. Tokens include integer
numbers and 26 predefined integer variables. There will be one variable corresponding to each
of the lowercase letters in the alphabet. The table below defines the C operators you need to
implement. The operators are listed in order of decreasing precedence. No operators may precede
an assignment operator in a calculation except another assignment operator.
symbols comments type assoc
( ) parentheses unary n/a
bitwise not unary right
negation unary right
/% mult, div, rem binary left
+add, sub binary left
<< >> shifts binary left
& bitwise and binary left
bitwise xor binary left
|bitwise or binary left
= += -= *= /= %= assignment binary right
<<=>>= &= =|=
Yacc does provide mechanisms for specifying the associativity and precedence of operators
in an unambiguous grammar. However, you are not allowed to use these mechanisms. Instead
you should define extra nonterminals and productions to enforce the specified associativity and
precedence for this assignment. In addition, you have to detect error conditions, which include
integer overflow and divide by zero. When there is an error condition, you will print out an
appropriate message indicating the first type of error encountered rather than the value after the
calculation. You will also not perform any assignments in a calculation after encountering an
error.
In addition to calculating an expression, two other commands are supported. The first is to
dump the values of the different variables when the command dump; is detected. The second is
to clear all of the values of the different variables to zero when the command clear; is encountered.
All variables should be zero at the beginning of the execution.
You need to create files called cexpr.y, scan.l, and makefile that will contain the parser, scanner
and makefile. The makefile should make an executable called cexpr. You should attach all three
of these files in a single e-mail message and mail them to [email protected] before class
on November 3. To get you started, I have placed the ex.y, ex.l, and makefile files on my web-
page: http://www.ittc.ku.edu/kulkarni/teaching/eecs665/. You should extend these files for
this assignment.
pf2

Partial preview of the text

Download Assignment II for Compiler Construction | EECS 665 and more Assignments Electrical and Electronics Engineering in PDF only on Docsity!

EECS 665 - Fall 2008

Assignment 2

cexpr - calculator for C integer expressions

Your assignment is to write a program using yacc and lex that implements a calculator of C integer expressions. The calculator will process expressions until it encounters EOF or invalid syntax. Each calculation is terminated by a semicolon. Tokens can be separated by whitespace (but no comments). After each calculation, the calculator prints its result. Tokens include integer numbers and 26 predefined integer variables. There will be one variable corresponding to each of the lowercase letters in the alphabet. The table below defines the C operators you need to implement. The operators are listed in order of decreasing precedence. No operators may precede an assignment operator in a calculation except another assignment operator.

symbols comments type assoc ( ) parentheses unary n/a ∼ bitwise not unary right − negation unary right ∗/% mult, div, rem binary left

  • − add, sub binary left << >> shifts binary left & bitwise and binary left ∧ bitwise xor binary left | bitwise or binary left = += -= *= /= %= (^) assignment binary right <<= >>= &= ∧= |=

Yacc does provide mechanisms for specifying the associativity and precedence of operators in an unambiguous grammar. However, you are not allowed to use these mechanisms. Instead you should define extra nonterminals and productions to enforce the specified associativity and precedence for this assignment. In addition, you have to detect error conditions, which include integer overflow and divide by zero. When there is an error condition, you will print out an appropriate message indicating the first type of error encountered rather than the value after the calculation. You will also not perform any assignments in a calculation after encountering an error.

In addition to calculating an expression, two other commands are supported. The first is to dump the values of the different variables when the command dump; is detected. The second is to clear all of the values of the different variables to zero when the command clear; is encountered. All variables should be zero at the beginning of the execution.

You need to create files called cexpr.y, scan.l, and makefile that will contain the parser, scanner and makefile. The makefile should make an executable called cexpr. You should attach all three of these files in a single e-mail message and mail them to [email protected] before class on November 3. To get you started, I have placed the ex.y, ex.l, and makefile files on my web- page: http://www.ittc.ku.edu/∼kulkarni/teaching/eecs665/. You should extend these files for this assignment.

Example Input: a = 55-3; b = c = a-42; a+bc; dump; clear; c = 6; a = b; a = 10000; b = 100000; ab; ab10; c/d; d/c; ^D

Example Output:

52 10 152 a: 52 b: 10 c: 10 d: 0 e: 0 f: 0 g: 0 h: 0 i: 0 j: 0 k: 0 l: 0 m: 0 n: 0 o: 0 p: 0 q: 0 r: 0 s: 0 t: 0 u: 0 v: 0 w: 0 x: 0 y: 0 z: 0 6 0 10000 100000 1000000000 overflow dividebyzero 0

Calculator off.