nav

       HOME    BINUS EVENT   HOW TO   KBP   THOUGHTS

About



Saturday, December 13, 2014

Assignment #7

Source:
Concepts of Programming Languages, Robert W. Sebesta.
Chapter 7: Expressions and Assignment Statements, page 342-344

By:
Name : Helena Natanael
NIM  : 1801380333



Review Question:

6. What associativity rules are used by APL?
In APL, all operators have the same level of precedence. Thus, the order of evaluation of operators in APL expressions is determined entirely by the associativity rule, which is right to left for all operators.

7. What is the difference between the way operators are implemented in C++ and Ruby?
What sets Ruby apart from the C-based languages in the area of expressions is that all of the arithmetic, relational, and assignment operators, as well as array indexing, shifts, and bitwise logic operators, are implemented as methods. For example, the expression a + b is a call to the + method of the object referenced by a, passing the object referenced by b as a parameter.

8. Define functional side effect.
A side effect of a function, naturally called a functional side effect, occurs when the function changes either one of its parameters or a global variable. (A global variable is declared outside the function but is accessible in the function.)

 9. What is a coercion?
Coercion is an implicit type of conversion only if they are widening (from a “smaller” type to a “larger” type). So int to float coercions are done across the assignment operator, but float to int coercions are not.


10. What is a conditional expression?
Conditional Expression is a logical feature of programming language which perform different actions based on true or false. 
In trinary, the format is statement ? action_1 (if true) : action_2 (if false).



Problem Sets:

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 C should, because it will ease the increment or even decrement while we use in looping rather than manually by the assigning, and also by using that we can easily know that it is not operation, instead it is an increment or decrement which is usually used in repetition.

7. Describe a situation in which the add operator in a programming language would not be commutative.
It wouldn’t be commutative when it deals with the negative integers.

8. Describe a situation in which the add operator in a programming language would not be associative.
It is not associative when it includes the other operator with higher precedence like the multiplication and division.

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 sub expressions 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

1. a * b – 1 + c
( ( (a * b)1– 1)2 + c)3
2. a* ( b – 1) / c mod d
( ( (a* ( b – 1)1 )2/ c )3 mod d)4
3. (a – b) / c & (d * e / a – 3)
( ( (a – b)1/ c)2 & ( ( (d * e)3 / a)4 – 3)5 )6
4. –a or c = d and e
( (–a)1 or ( (c = d)2 and e)3 )4
5. a > b xor c or d <= 17
( ( (a > b)1 xor c)3 or (d <= 17)2 )4
6. –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