Ch7 Problem Set
Ch7 Problem Set
Problem Set
1. When might you want the compiler to ignore type differences in an expression?
Suppose Type1 is a subrange of Integer. It may be useful for the difference between
Type1 and Integer to be ignored by the compiler in an expression.
2. State your own arguments for and against allowing mixed-mode arithmetic expressions.
For: A mixed mode arithmetic expression is needed in calculating expressions that might have
decimal results. It allows two different type of number data type to be summed without losing
the precision of the float.
Against: While it is compulsory to have mixed-mode expressions, a mixed mode might produce
a decimal result even though the result wanted is a non-decimal.
3. Do you think the elimination of overloaded operators in your favorite language would be
beneficial? Why or why not?
Yes, I think that the elimination of overloaded operators in my favorite language would be
beneficial.
Elimination of overloaded operators will result in:
– Enhance and increase readability.
– Minimize the compiler overhead (choosing the correct operator meaning).
4. Would it be a good idea to eliminate all operator precedence rules and require parentheses
to show the desired precedence in expressions? Why or why not?
No, it is not a good idea to eliminate all operator precedence rules. It affects the flexibility
provided by the language since only one way is used to show the precedence in expressions.
Moreover, using parentheses will future more affects writability and in some cases readability.
5. Should C’s assigning operations (for example, +=) be included in other languages (that do
not already have them)? Why or why not?
Yes, because it’s simpler than the normal <operand> = <operand> + <some values> form, and it
can save much time.
6. Should C’s single-operand assignment forms (for example, ++count) be included in other
languages (that do not already have them)? Why or why not?
Yes, because it’s simpler than the normal addition <operand> = <operand> + 1; and it can save
much time.
7. Describe a situation in which the add operator in a programming language would not be
commutative.
An expression such as : x + fun(y), and fun(y) changes x.
8. Describe a situation in which the add operator in a programming language would not be
associative.
Consider the integer expression A + B + C. Suppose the values of A, B, and C are 20,000, 25,000,
and -20,000, respectively. Further suppose that the machine has a maximum integer value of
32,767. If the first addition is computed first, it will result in overflow. If the second addition is
done first, the whole expression can be correctly computed.
9. Assume the following rules of associativity and precedence for expressions:
Precedence Highest *, /, not
+, –, &, mod
– (unary)
=, /=, < , <=, >=, >
and
Lowest or, xor
Associativity Left to right
Show the order of evaluation of the following expressions by parenthesizing all
subexpressions and placing a superscript on the right parenthesis to indicate order. For
example, for the expression
a+b*c+d
the order of evaluation would be represented as ((a + (b * c)1)2 + d)3
a. a * b - 1 + c ( ( ( a * b )1 – 1 )2 + c )3
b. a * (b - 1) / c mod d ( ( ( a * ( b – 1 )1 )2 / c )3 mod d)4
c. (a - b) / c & (d * e / a - 3) ( ( ( a – b )1 / c )2 & ( ( ( d * e )3 / a )4 – 3 )5 )6
d. -a or c = d and e ( ( -a )1 or ( ( c = d )2 and e )3 )4
e. a > b xor c or d <= 17 ( ( a > b )1 xor ( c or ( d <= 17 )2 )3 )4
f. -a + b ( – ( a + b )1 )2
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) ( a * ( b – ( 1 + c )1 )2 )3
(b) ( a * ( ( b – 1 )2 / ( c mod d )1 )3 )4
(c) ( ( a – b )5 / ( c & ( d * ( e / ( a – 3 )1 )2 )3 )4 )6
(d) ( – ( a or ( c = ( d and e )1 )2 )3 )4
(e) ( a > ( xor ( c or ( d <= 17 )1 )2 )3 )4
(f) ( – ( a + b )1 )2
11. Write a BNF description of the precedence and associativity rules defined 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> + <e5> | <e4> – <e5> | <e4> & <e5> | <e4> mod <e5> | <e5>
<e5> → <e5> * <e6> | <e5> / <e6> | not <e5> | <e6>
<e6> → a | b | c | d | e | const | ( <expr> )
12. Using the grammar of Problem 11, draw parse trees for the expressions of Problem 9.
18. Should an optimizing compiler for C or C++ be allowed to change the order of
subexpressions in a Boolean expression? Why or why not?
No. Because of short-circuit evaluation, the order of subexpressions around an && is important.
19. Answer the question in Problem 17 for Ada.
20. Consider the following C program:
int fun(int *i) {
*i += 5;
return 4;
}
void main() {
int x = 3;
x = x + fun(&x);
}
What is the value of x after the assignment statement in main, assuming
a. operands are evaluated left to right.
x = xold + 4
=3+4
=7
b. operands are evaluated right to left.
x = xnew + 4
=8+4
= 12
21. Why does Java specify that operands in expressions are all evaluated in left-to-right
order?
In Java, the operators are evaluated from left to right as long as it does not violate the
precedence and associativity. This is known as the Java expression evaluation in contrast to the
arithmetical evaluation.Java expression evaluation is equivalent to the arithmetic evaluation.
Java expression evaluation is easier to implement and more efficient to execute.
22. Explain how the coercion rules of a language affect its error detection.
Coercion defined as an implicit type conversion that is initiated by the compiler. Language
designers are not in agreement on the issue of coercions in arithmetic expressions. Those
against a broad range of coercions are concerned with the reliability problems that can result
from such coercions, because they reduce the benefits of type checking. Those who would
rather include a wide range of coercions are more concerned with the loss in flexibility that
results from restrictions.