Operator associativity
- Operator associativity
In
programming languages , the associativity of anoperator 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.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))).
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 , but calculated less efficiently.
Non-associative operators
In
Prolog , the infix operator :- is non-associative because constructs such as "a :- b :- c" constitute syntax errors. In python, comparison operators (such as >, =, 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.See also
*
Order of operations (in arithmetic and algebra)
*Common operator notation (in programming languages)
*Associativity (the mathematical property of associativity)
Wikimedia Foundation. 2010.
Look at other dictionaries:
Operator (programming) — Programming languages generally support a set of operators that are similar to operators in mathematics. A language may contain a fixed number of built in operators (e.g., C programming language) or it may allow the creation of programmer defined … Wikipedia
Common operator notation — In programming languages, common operator notation is just one way of notating mathematical expressions as alinear sequence of tokens, or operators, but this is not the only way. The use of operator precedence classesand associativities is just… … Wikipedia
Hyper operator — Articleissues OR=September 2008The hyper operators forming the hyper n family are related to Knuth s up arrow notation and Conway chained arrow notation as follows: extrm{hyper} n (a, b) = extrm{hyper}(a,n,b) = a uparrow^{n 2} b = a o b o (n 2)… … Wikipedia
Vertex operator algebra — In mathematics, a vertex operator algebra (VOA) is an algebraic structure that plays an important role in conformal field theory and related areas of physics. They have proven useful in purely mathematical contexts such as monstrous moonshine and … Wikipedia
Left recursion — In computer science, left recursion is a special case of recursion. In terms of context free grammar, a non terminal r is left recursive if the left most symbol in any of r’s ‘alternatives’ either immediately (direct left recursive) or through… … Wikipedia
Operators in C and C++ — This is a list of operators in the C++ and C programming languages. All the operators listed exist in C++; the third column indicates whether an operator is also present in C. It should also be noted that C does not support operator… … Wikipedia
Boolean algebra (logic) — Boolean algebra (or Boolean logic) is a logical calculus of truth values, developed by George Boole in the late 1830s. It resembles the algebra of real numbers as taught in high school, but with the numeric operations of multiplication xy ,… … Wikipedia
Laws of Form — (hereinafter LoF ) is a book by G. Spencer Brown, published in 1969, that straddles the boundary between mathematics and of philosophy. LoF describes three distinct logical systems: * The primary arithmetic (described in Chapter 4), whose models… … Wikipedia
Algebra — is a branch of mathematics concerning the study of structure, relation, and quantity. Together with geometry, analysis, combinatorics, and number theory, algebra is one of the main branches of mathematics. Elementary algebra is often part of the… … Wikipedia
List of algebraic structures — In universal algebra, a branch of pure mathematics, an algebraic structure is a variety or quasivariety. Abstract algebra is primarily the study of algebraic structures and their properties. Some axiomatic formal systems that are neither… … Wikipedia
Фильмы
- The Mighty Power Of the Commowealth., 1984 — Annotation: The film tells about the activity of the Council for Mutual Economic Assistance (CMEA). History: On the assistance of the socialist countries in the construction industry, space…
- Eleventh Five-Year Period: Time And People., 1986 — Annotation: About improvement of economic mechanism basing on examples of definite enterprises: "Rostselmash" ["Rostov Agriculture Machinery Plant"}, the collective farm named after Frunze (a…
- The Tsar's Illness., 1990 — Annotation: About the illness from which tsarevitch (the son of the Tsar) Alexey suffered- hemophilia, about modern methods of preventionism and treatment. Film description: Film about hemophilia…
