Operator associativity
From Wikipedia, the free encyclopedia
In programming languages, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. Operators may be left-associative, right-associative or non-associative. The associativity and precedence of an operator depends on the programming language in question. Many programming language manuals provide a table of operator precedence and associativity.
Contents |
[edit] What is associativity
Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence. Consider the expression 7-4+2. The result could be either (7-4)+2 = 5 or 7-(4+2) = 1. The former result corresponds to the case when + and - are left-associative. The latter result corresponds to the case when + and - are right-associative. Operators with the same precedence always have the same associativity. Usually the operators +, -, * and / are left-associative, while exponentiation and assignment operators are right-associative.
A left-associative operator can be said to associate "to the left", and similarly a right-associative operator can be said to associate "to the right". To understand the intuition behind these names, consider again the expression 7-4+2. If + and - are left-associative then the middle operand (4) belongs to the operator on its left (hence the name "to the left"). If + and - are right-associative then the middle operand belongs to the operator on its right (hence the name "to the right").
A left-associative operator may also be said to have "left to right" associativity, and a right-associative operator may also be said to have "right to left" associativity. This is somewhat counter-intuitive considering the above paragraph. To understand the intuition behind these names consider the expression 1+2+3+4+5. If + is left-associative, the addition operations are carried out left to right, i.e. the result is (((1+2)+3)+4)+5. If + is right-associative, the addition operations are carried out right to left, i.e. the result is 1+(2+(3+(4+5))).
[edit] Note about right-associative operators
It may seem counter-intuitive that assignment operators such as =, and also +=, -=, etc, have an associativity if you are unfamiliar with the idea of treating an assignment as an expression with side effects. In languages such as C, the assignment a=b is an expression that returns the value b, with the side effect of setting a to the value of b. Thus an assignment can be performed in the middle of an expression. Note that an expression with side effects can be made into a statement by following it with a semicolon (i.e. a=b is an expression but a=b; is a statement). The right-associativity of = allows statements such as a=b=c; which means a=(b=c); which sets both a and b to the value of c. The alternative (a=b)=c; does not make sense because a=b is not an lvalue.
Exponentiation makes perfect sense as a right-associative operator. In Python, exponentiation is denoted by the ** operator. The expression a ** b ** c means a ** (b ** c). The alternative (a ** b) ** c would usually be equivalent to a ** (b * c) by the mathematical identity (ab)c = abc, but calculated less efficiently.
[edit] Non-associative operators
In Prolog, the infix operator :- is non-associative because constructs such as "a :- b :- c" constitute syntax errors. In python, the operators >, >=, <= and < are non-associative because expressions such as a < b < c are allowed and are shorthand for (a < b) and (b < c). Thus operator associativity doesn't make sense.
[edit] See also
- Order of operations (in arithmetic and algebra)
- Common operator notation (in programming languages)
- Associativity (the mathematical property of associativity)