Principles of Programming Language

–Operator precedence rules? –Operator associativity rules? –Order of operand evaluation? –Operand evaluation side effects? –Operator overloading? –Type mixing in expressions? Arithmetic Expressions: Operators A unary operator has one operand A binary operator has two operands A ternary operator has three operands Arithmetic Expressions: Operator Precedence Rules The operator precedence rules for expression evaluation define the order in which “adjacent” operators of different precedence levels are evaluated Typical precedence levels

– parentheses – unary operators – ** (if the language supports it) – *, / – +, -

Arithmetic Expressions: Operator Associativity Rule which adjacent operators with the same precedence level are evaluated

•The operator associativity rules for expression evaluation define the order in

•Typical associativity rules –Left to right, except **, which is right to left –Sometimes unary operators associate right to left (e.g., in FORTRAN) •APL is different; all operators have equal precedence and all operators associate right to left

G. NARAYANAMMA INSTITUTE OF TECHNOLOGY & SCIENCE (For Women) DEPARTMENT OF IT

Principles of Programming Language

•Precedence and associativity rules can be overriden with parentheses Arithmetic Expressions: Conditional Expressions Conditional Expressions

–C-based languages (e.g., C, C++) –An example: –Evaluates as if written like if (count == 0) average = 0 else average = sum /count

average = (count == 0)? 0 : sum / count

Arithmetic Expressions: Operand Evaluation Order Operand evaluation order

•Variables: fetch the value from memory •Constants: sometimes a fetch from memory; sometimes the constant is in the machine language instruction

•Parenthesized expressions: evaluate all operands and operators first •The most interesting case is when an operand is a function call Arithmetic Expressions: Potentials for Side Effects Functional side effects: when a function changes a two-way parameter or a non-local variable

•

•Problem with functional side effects: –When a function referenced in an expression alters another operand of the expression; e.g., for a parameter change: a = 10; /* assume that fun changes its parameter */ b = a + fun(a); Functional Side Effects

•Two possible solutions to the problem •Write the language definition to disallow functional side effects

G. NARAYANAMMA INSTITUTE OF TECHNOLOGY & SCIENCE (For Women) DEPARTMENT OF IT

Principles of Programming Language

references

•No two-way parameters in functions •No non-local references in functions •Advantage: it works! •Disadvantage: inflexibility of one-way

parameters and lack of non-local

Write the language definition to demand that operand evaluation order be fixed

•

•Disadvantage: limits some compiler optimizations •Java requires that operands appear to be evaluated in left-to-right order Overloaded Operators Use of an operator for more than one purpose is called operator overloading Some are common (e.g., + for int and float) Some are potential trouble (e.g., * in C and C++) of compiler error detection (omission of an operand should be a detectable error)

–Loss

–Some loss of readability –Can be avoided by introduction of new symbols (e.g., Pascal’s div for integer division) Overloaded Operators (continued) C++, Ada, Fortran 95, and C# allow user-defined overloaded operators Potential problems:

–Users can define nonsense operations –Readability may suffer, even when the operators make sense Type Conversions 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 Type...