Concepts of Programming Languages

(Sean Pound) #1
7.2 Arithmetic Expressions 319

high-level programming languages. Most of the characteristics of arithmetic
expressions in programming languages were inherited from conventions that
had evolved in mathematics. In programming languages, arithmetic expressions
consist of operators, operands, parentheses, and function calls. An operator
can be unary, meaning it has a single operand, binary, meaning it has two
operands, or ternary, meaning it has three operands.
In most programming languages, binary operators are infix, which means
they appear between their operands. One exception is Perl, which has some
operators that are prefix, which means they precede their operands.
The purpose of an arithmetic expression is to specify an arithmetic com-
putation. An implementation of such a computation must cause two actions:
fetching the operands, usually from memory, and executing arithmetic opera-
tions on those operands. In the following sections, we investigate the common
design details of arithmetic expressions.
Following are the primary design issues for arithmetic expressions, all of
which are discussed in this section:


  • What are the operator precedence rules?

  • What are the operator associativity rules?

  • What is the order of operand evaluation?

  • Are there restrictions on operand evaluation side effects?

  • Does the language allow user-defined operator overloading?

  • What type mixing is allowed in expressions?


7.2.1 Operator Evaluation Order


The operator precedence and associativity rules of a language dictate the order
of evaluation of its operators.

7.2.1.1 Precedence
The value of an expression depends at least in part on the order of evaluation
of the operators in the expression. Consider the following expression:

a + b * c

Suppose the variables a, b, and c have the values 3 , 4 , and 5 , respectively. If
evaluated left to right (the addition first and then the multiplication), the result
is 35. If evaluated right to left, the result is 23.
Instead of simply evaluating the operators in an expression from left to
right or right to left, mathematicians long ago developed the concept of placing
operators in a hierarchy of evaluation priorities and basing the evaluation order
of expressions partly on this hierarchy. For example, in mathematics, multi-
plication is considered to be of higher priority than addition, perhaps due to
its higher level of complexity. If that convention were applied in the previous
Free download pdf