Concepts of Programming Language: Chapter 7

Taken from Robert Sebesta’s “Concepts of Programming Language” chapter 7 for T0152 assignment

Review: 25% of 28 (exactly 7) and Problemset: 25% of 22 (rounded down to 5)

Review

1. Define operator precedence and operator associativity.

Operator precedence is the rule for expression evaluation that defines the order in which the operators of different precedence levels are evaluated. Example, multiplication is done first than addition.

Operator associativity is the rule which explains if there are two adjacent occurences of operators with the same level of precedence, which one is evaluated first. Example, a left associativity means the leftmost occurence is evaluated first.

2. What is a ternary operator?

A ternary operator is an operator that works for three operands.

5. What is a nonassociative operator?

A nonassociative operator is an operator that has no associativity, means the operator should be parenthesized to show the order, otherwise it’s illegal.

11. What is an overloaded operator?

An overloaded operator is an operator that can be used in multiple ways, for example C++’s + can be used for integer additions and floating point additions.

12. Define narrowing and widening conversions.

Narrowing conversions converts a value to a type that cannot store even approximations of all values of the original type. For example, converting double to float in C++.

Widening conversions converts a value that can include at least approximations of all of the values of the original type. For example, converting int to double in C++.

15. What is referencial transparency?

Referencial transparency is the property of a program 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.

28. What is a cast?

A cast is explicit type conversion in C based languages.

Problemset

1. When might you want the compiler to ignore type differences in an expression?

When I want to evaluate a string as a number, and vice-versa.

7. Describe a situation in which the add operator in a programming language would not be commutative.

An expression of a + fun(b), where function fun(b) changes the value of a.

14. What is your primary argument against (or for) the operator precedence rules of APL?

The operator precedence rules of the common imperative languages are nearly all the same, because they are based on those of mathematics.

17.  Should an optimizing compiler for C or C++ be allowed to change the order of subexpressions in a Boolean expression? Why or why not?

Because of short-circuit evaluation, the order of subexpressions around an && is important.

For instance, we have clause1 && clause2. If clause1 is false, clause2 will not be evaluated.

This is fine, this is what we expect. But if the compiler reorganizes things to clause2 && clause1, and clause2 is true, and clause2 changes some variable or otherwise affects the outcome of clause1, the entire expression may result true. Or, more simply, if clause2 does something that isn’t allowed to happen if clause1 is false, i.e. dereferencing a pointer that clause1 checks to see if it’s null, or something to that effect, you can end up with a runtime error very difficult to track down. (Practically impossible, if it’s being done by the compiler.)

The same goes for || with the words ‘true’ and ‘false’ switched.

 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.

b. Operands are evaluated right to left.

In part a, x = 3 + fun(3), which makes x = 3 + 4 = 7, and the value of the first x is saved first, not after changed into 8. So the result is 7.

In part b, first fun(3) is called, which makes x = 3+5 = 8. While fun(3) remains 4, x now is 8, and so the result is x = 4 + 8 = 12

Leave a comment