Mark Hobley 
OPERATOR PRECEDENCE


Programming languages usually use rules of precedence to determine the order in which operators within an expression are used during evaluation. For example, look at the following expression:

3 + 4 x 5
An inexperienced programmer may be expect that a result of 35 is obtained, but this actually produces a value of 23. This is because the multiplication in the expression is performed before the addition, because multiplication has a higher precedence than addition. Within an expression, each operator has a predetermined position, precedence, and associativity.

Position

The position of the operator is the position of operator as a component within the expression, and relates to other components within the expression:

12 + (2 x -4) + 5^2
The above expression contains the following components:

Position Component Notes
1 12 The first component is the number 12. This is also the first element
2 + The second component is the plus operator. This is the first operator
3 ( The third component is the opening bracket. The expression (2 x -4) is the second element
4 2 The fourth component is the number 2. This is the first element of the second element
5 x The fifth component is the multiplication operator. This is the first operator in the second element and the second operator within the expression.
6 - The sixth component is the minus operator. This is the second operator in the second element and the third operator within the expression.
7 4 The seventh component is the number 4. This is the second element of the second element
8 ) The eighth component is the closing bracket.
9 + The ninth component is the plus operator. This is the fourth operator.
10 5 The tenth component is the number 5. The expression 5^2 is the third element
11 ^ The eleventh component is the caret exponent operator. This is the fifth operator
12 2 The twelfth component is the number 2. This is the third component of the third expression

Prefix, Postfix, Infix

An operator is either a prefix operator, a postfix operator or an infix operator depending upon is positioning within the expression.

Prefix Operators

A prefix operator immediately precedes its operand. The unary operators used to indicate whether a number is positive or negative is a prefix operator:

Postfix Operators

A postfix operator immediately follows its operand within an expression.

Infix Operators

An infix operator is positioned between its left and right operands:

Precedence

The precedence of an operator is determined by a numerical value representing the priority of the operator whilst determining the order in which the operators will be evaluated. Note that operators to be evaluated first have a higher value, although some implementations give operators with higher precedence, lower priority numbers.

Competing for Operands

Operands go to the operator with the highest level of precedence

In cases where two operators of different precedences compete for the same operand, the operand belongs to the operator with the highest precedence:

3 + 4 x 5
operator has the higher precedence, it owns the In the above expression, both the plus operator and the multiplication operator are competing for the second element (numeral 4) as an operand. Because the multiplicationoperand, so the number 4 is associated with the multiplication operator, rather than the addition operator.

Operators of equal precedence are evaluated from left to right

In cases where two operators of the same precedence compete for operands, the operand belongs to the operator on the left:

6 + 5 - 3
In the above expression, because both the addition and subtraction operator have equal precedence, the second element (numeral 5) belongs to the addition operator, so the expression is evaluated mathematically as:

(6 + 5) - 3
Note that rules of operator associativity affect the grouping of operands to operators prior to the left to right rule being effective.

Associativity

The associativity of operators, determines which operands an operator can associate with, before the operators are matched to their operands within the expression. In cases where operators of equal precedence compete for common operands, The rules of operator associativity determine the grouping of operands to operators. The infix operators can be be leftassociative, rightassociative, or nonassociative. The prefix and postfix operators can be either have associativity or nonassociativity.

Left Associativity

If an operator is leftassociative, the operators are applied in left to right order. For example,the basic arithmetic operators +, −, ×, and ÷ are all leftassociative:

2 + 3 - 4 + 5
Because the basic arithmetic operators all leftassociative, the above expression is evaluated as:

(((2 + 3) - 4) + 5)

Right Associativity

If an operator is rightssociative, The operators, when grouped are applied in right to left order. In some programming languages, the exponent operator, ^ is rightassociative:

4 ^ 3 ^ 2
Because the exponent operator is rightassociative, the above expression is equivalent to:

4 ^ (3 ^ 2)

Non Associativity

An operator which is nonassociative cannot compete for operands with operators of equal precedence. For example, the infix eyesnose operator, :- is nonassociative.

The following construct causes a syntaxerror, because the eyesnose operator is nonassociative:

a :- b :- c    # This will cause a syntax error, because the operators are nonassociative

Associativity of prefix and postfix operators

The prefix and postfix operators are considered to be associative only if they can compete for operands with operators of equal precedence.

For example, the unary prefix operators for numerical sign have right associativity:

The postfix nudge operators may have left associativity in some implementations:

Operators closest to the operand are associated first

When more than one prefix or postfix operator of equal precedence are associated with an operand, the operators closest to the operand are associated first:

-sin a    # This is equivalent to -(sin(x))
The sin function is associated with the value of the a operand before the minus operator, because it is the closest to the operand.

In the following expression both the ++ and -- nudge operators have equal precedence. However, the ++ operator is first associated with the operator, because it is the closest to the operand:

a++--    # This is equivalent to (a++)--
When prefix and postfix operators of equal precedence coexist, the order of association is undefined in most implementations.

Prefix and postfix operators do not necessarily have higher precedence than infix operators

The prefix and postfix operators do not necessarily have higher precedence than infix operators. For example, the sin prefix operator may have a level of precedence between the addition and multiplication operator:

sin 2 × 3 + 4    # This is equivalent to ((sin(2 × 3)) + 4)

Evaluation

Expression evaluation is achieved using the following rules:

  1. The sub-expressions most deeply nested in parenthesis are evaluated first
  2. Any sub-expressions in parentheses are treated as single evaluated operand.
  3. Higher precedence operators gain operands before lower precedence operators
  4. The operators of equal precedence gain operands according to the associativity of the operators
  5. Expression elements are evaluated with highest precedence operators first