Docsity

Assignment 4 Cexpr - Compiler Construction | COP 5621, Assignments of Computer Science

Toggle sidebar
bg1
COP5621 - Fall 2008
Assignment 4
cexpr
Your assignment is to write a program using yacc and lexthat implements a calculator of C integer
expressions. The calculator will process expressions until it encounters EOF or invalid syntax. Each calcu-
lation is terminated by a semicolon. Tokens can be separated by whitespace (but no comments). After each
calculation, the calculator prints its result. To kens include integer numbers and 26 predefined integer vari-
ables. There will be one variable corresponding to each of the lowercase letters in the alphabet. The table
belowdefines 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
-neg ation unary right
*/% mult, div, r em 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.Howev er, 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 assign-
ment.
In addition, you have todetect error conditions, which include integer overflowand 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. Yo uwill also not perform anyassignments in a cal-
culation after encountering an error.
In addition to calculating an expression, twoother 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.
Youneed to create files called cexpr.y,scan.l,and makefile that will contain the parser,scanner and
makefile. The makefile should makeanexecutable called cexpr.You should attach all three of these files
in a single e-mail message and mail them to to whalley@cs before class on October 9. To get you started, I
have placed the ex.y,ex.l,and makefile files in the ˜whalley/cop5621exec directory.You should extend
these files for this assignment.
pf2

Partial preview of the text

Download Assignment 4 Cexpr - Compiler Construction | COP 5621 and more Assignments Computer Science in PDF only on Docsity!

COP5621 - Fall 2008 Assignment 4 cexpr

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 calcu- lation 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 vari- ables. 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 assign- ment.

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 cal- culation 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 to whalley@cs before class on October 9. To get you started, I have placed the ex.y , ex.l , and makefile files in the ˜whalley/cop5621exec directory. You should extend these files for this assignment.

Example Input:

a = 5 5-3; b = c = a -42; a+bc; dump; clear; c = 6 ; a = b ; a = 1 0000; b = 1 00000; 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.