Download presentation
1
Chapter#7 Problem Set
2
Problem#9 Assume the following rule of associativity and precedence for the expressions: Show the order of evaluation of the following expressions by parenthesizing all sub-expressions and placing a superscript on the right parenthesis to indicate order. Ex: a + b *c + d ---order--> ((a+(b*c)1)2+d)3
3
a * b -1 + c (a*b)1 ( (a*b)1 -1)2 ( ( ( a * b )1 - 1 )2 + c )3
4
a * (b – 1) / c mod d ( b - 1 )1 ( a * ( b - 1 )1 )2
5
(a – b) / c & (d * e / a – 3) ( a - b )1 ( ( a - b )1 / c )2
6
-a or c = d and e ( - a )1 ( c = d )2 (( c = d )2 and e)3
7
a > b xor c or d <=17 ( a > b )1 ( d <= 17 )2
8
-a + b ( a + b )1 ( - ( a + b )1 )2
9
Problem#10 Show the order of evaluation of the expressions of Problem#9, assuming that there are no precedence rules and all operators associate right-to-left a * b -1 + c ( 1 + c )1 ( b - ( 1 + c )1 )2 ( a * ( b - ( 1 + c )1 )2 )3
10
Problem#10 (cont.) a * (b – 1) / c mod d (a – b) / c & (d * e / a – 3)
11
Problem#10 (cont.) -a or c = d and e a > b xor c or d <=17
12
Problem#10 (cont.) -a + b ( a + b )1 ( - ( a + b )1 )2
13
Problem#11 Write a BNF description of the precedence and associativity rules define for the expressions in Problem 9. Assume the only operands are the names a, b, c, d, and e. <expr> <expr> or <e1> | <expr> xor <e1> | <e1> <e1> <e1> and <e2> | <e2> <e2> <e2> = <e3> | <e2> /= <e3> | <e2> < <e3> | <e2> <= <e3> | <e2> > <e3> | <e2> >= <e3> | <e3> <e3> <e4> | -<e4> <e4> <e4> + <e5> | <e4> - <e5> | <e4> & <e5> | <e4> mod <e5> | <e5> <e5> <e5> * <e6> | <e5> / <e6> | not <e6> | <e6> <e6> a | b | c | d | e | const | ( <expr> )
14
Problem#13 Let the function fun be defined as:
int fun(int *k){ *k += 4; return 3 * (*k) -1; } Suppose fun is used in the program as follows: void main() { int i = 10, j = 10, sum1, sum2; sum1 = (i / 2) + fun(&i); sum2 = fun(&j) + (j / 2) What are the values of sum1 and sum2 If the operands in the expressions are evaluated left to right? sum1=46 sum2=48 If the operands in the expressions are evaluated right to left? sum1= 48 sum2= 46
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.