Pada kesempatan kali ini saya akan menjawab soal-soal yang ada dalam buku "CONCEPTS OF Programming Languages (TENTH EDITION)" - ROBERT W. SEBESTA. Chapter 7 Expressions and
Assignment Statements
REVIEW QUESTION
11. What is an overloaded operator?
Answer : Use of an operator for more than one purpose is called operator overloading.
12. Define narrowing and widening conversions.
Answer : A narrowing conversion is one that converts an object to a type that cannot include all of the values of the original type e.g., float to int
A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type. e.g., int to float.
A widening conversion is one in which an object is converted to a type that can include at least approximations to all of the values of the original type. e.g., int to float.
13. In JavaScript, what is the difference between == and ===?
Answer : It is the same but === prevent its operands from being coerced.
14. What is a mixed-mode expression?
Answer : A mixed-mode expression is one that has operands of different types.
15. What is referential transparency?
Answer : A program has the property of referential transparency if any two expressions in the program that have the same value can be substituted for one another anywhere in the program, without affecting the action of the program.
PROBLEM SET
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.
Answer : Precedence: Highest *, /, not
+, -, &, mod
- (unary)
=, /=, <, <=, >=, >
and
Lowest or, xor
Associativity: Left to right
<expression> → <expression> or <or_exp>
| < expression > xor <or_exp>
| <or_exp>
<or_exp> → <or_exp> and <and_exp>
| <and_exp>
< and_exp > → <and_exp> = <expr>
| <and_exp /= <expr>
| <and_exp> < <expr>
| <and_exp> <= <expr>
| <and_exp> >= <expr>
| <and_exp> > <expr>
| <expr>
<expr> → – <unary_expr>
| <unary_expr>
<unary_expr> → <unary_expr> + <term>
| <unary_expr> – <term>
| <unary_expr> & <term>
| <unary_expr> mod < term>
| <term>
< term > → < term > * < factor >
| < term > / < factor >
| not < factor >
| < factor >
< factor > → ( < expr > )
| <operand>
<operand> → a | b | c | d | e
+, -, &, mod
- (unary)
=, /=, <, <=, >=, >
and
Lowest or, xor
Associativity: Left to right
<expression> → <expression> or <or_exp>
| < expression > xor <or_exp>
| <or_exp>
<or_exp> → <or_exp> and <and_exp>
| <and_exp>
< and_exp > → <and_exp> = <expr>
| <and_exp /= <expr>
| <and_exp> < <expr>
| <and_exp> <= <expr>
| <and_exp> >= <expr>
| <and_exp> > <expr>
| <expr>
<expr> → – <unary_expr>
| <unary_expr>
<unary_expr> → <unary_expr> + <term>
| <unary_expr> – <term>
| <unary_expr> & <term>
| <unary_expr> mod < term>
| <term>
< term > → < term > * < factor >
| < term > / < factor >
| not < factor >
| < factor >
< factor > → ( < expr > )
| <operand>
<operand> → a | b | c | d | e
12. Using the grammar of Problem 11, draw parse trees for the expressions
of Problem 9.
Answer :
13. Let the function fun be defined as
int fun(int *k) {
*k += 4;
return 3 * (*k) - 1;
}
Suppose fun is used in a 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
a. if the operands in the expressions are evaluated left to right?
b. if the operands in the expressions are evaluated right to left?
Answer :
a. sum1 = (10/2) + (3 * 10 - 1) = 5 + 29 = 34
sum2 = (3 * 14 - 1) + (14/2) = 41 + 7 = 49
b. sum1 = (14/2) + (3 * 14 - 1) = 7 + 41 = 49
sum2 = (3 * 10 - 1) + (10/2) = 29 + 5 = 34
b. sum1 = (14/2) + (3 * 14 - 1) = 7 + 41 = 49
sum2 = (3 * 10 - 1) + (10/2) = 29 + 5 = 34
14. What is your primary argument against (or for) the operator precedence
rules of APL?
Answer : The operator precedence rules of the common imperative languages are nearly all the same, because they are based on those of mathematics.
15. Explain why it is difficult to eliminate functional side effects in C.
Answer : One reason functional side effects would be difficult to remove from C is that all of C’s subprograms are functions, providing the ability of returning only a single data value (though it could be an array). The problem is that in many cases it is necessary (or at least convenient) to return more than one data value, which is done through the use of pointer actual parameters, which are a means of creating functional side effects