Senin, 08 April 2013

Concepts of Programming Languages Assignment - Chapter 7

Review Questions

3. What is a prefix operator?
prefix operators is the operators that precede the operands

5. What is a nonassociative operator?
Operator with illegal expression

 9. What is a coercion?
 A coercion is the implicit type convertion
 
10. What is a conditional expression?
Conditional expression is a statement that uses if-then-else statements
 
11. What is an overloaded operator?
An overloaded operator is the multiple uses of an operator
 
14.  What is a mixed-mode expression?
A mixed-mode expression is one that has operands of different types
 
21. What is the purpose of a compound assignment operator?
To specifying a commonly needed form of assignment

24. What two languages include multiple assignments?
Perl, Ruby and Lua
 
28. What is a cast?
Cast is explicit type conversions. To specify a cast, the desired type is placed in parentheses just before the expression to be converted

Problem Set

1. Run the code given in Problem 13 (in the problem set) on some system that supports C to determine the values of sum1 and sum2. Explain the result.
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

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's not a good idea to eliminate all operator precedence rules. Although this idea sounds good, but in fact 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.

7. Describe a situation in which the add operator in a programming language would not be commutative.
An expression such as a + fun(b)

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.

11. Write an BNF description of precendence 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> )

15. Explain why it is difficult to eliminate functional side effects in C.
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

Tidak ada komentar:

Posting Komentar