Chapter7 Problem Set Problem9 Assume the following rule
Chapter#7 Problem Set
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
-a or c = d and e d) ( - a )1 ( c = d )2 (( c = d )2 and e)3 ( ( - a )1 or (( c = d )2 and e )3 )4
a > b xor c or d <=17 e) ( a > b )1 ( d <= 17 )2 ( ( ( a > b )1 xor c )3 or ( d <= 17 )2 )4
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 a) ( 1 + c )1 ( b - ( 1 + c ) 1 )2 ( a * ( b - ( 1 + c ) 1 )2 )3
Problem#10 (cont. ) -a or c = d and e d) ( d and e )1 ( c = ( d and e )1 )2 ( a or ( c = ( d and e )1 )2 )3 ( - ( a or ( c = ( d and e )1 )2 )3 )4 a > b xor c or d <=17 e) ( d <= 17 )1 ( c or ( d <= 17 )1 )2 ( b xor ( c or ( d <= 17 )1 )2 )3 ( a > ( b xor ( c or ( d <= 17 )1 )2 )3 )4
Problem#10 (cont. ) -a + b f) ( a + b )1 ( - ( a + b ) 1 )2
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> or <e 1> | <expr> xor <e 1> | <e 1> and <e 2> | <e 2> = <e 3> | <e 2> /= <e 3> | <e 2> <= <e 3> | <e 2> >= <e 3> | <e 3> <e 4> | -<e 4> + <e 5> | <e 4> - <e 5> | <e 4> & <e 5> | <e 4> mod <e 5> | <e 5> * <e 6> | <e 5> / <e 6> | not <e 6> | <e 6> a | b | c | d | e | const | ( <expr> )
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, sum 1, sum 2; sum 1 = (i / 2) + fun(&i); sum 2 = fun(&j) + (j / 2) } What are the values of sum 1 and sum 2 a) If the operands in the expressions are evaluated left to right? sum 1=46 sum 2=48 If the operands in the expressions are evaluated right to left? b) sum 1= 48 sum 2= 46
- Slides: 14