Content deleted Content added
Moved esoteric critique of the intro to an esoteric critique session "generalisations of CON". Probably too close to original research too, that bit. |
|||
(25 intermediate revisions by 16 users not shown) | |||
Line 1:
{{about|the concept of operator precedence|operator precedence parsing|operator-precedence parser}}
{{
In [[programming languages]], [[scientific calculator]]s and similar '''common operator notation''' or '''operator grammar''' is
▲In [[programming languages]], '''common operator notation''' is one way of notating mathematical expressions as a linear sequence of tokens, or [[operator (programming)|operator]]s.
Operands are
Each operator is given a position, precedence, and an associativity. The '''operator precedence''' is a number (from high to low or vice versa) that defines which operator takes an operand that is surrounded by two operators of different precedence (or priority). Multiplication normally has higher precedence than addition,<ref name="Bronstein_1987">{{cite book |title=Taschenbuch der Mathematik |title-link=Bronstein and Semendjajew |author-first1=Ilja Nikolaevič<!-- Nikolajewitsch --> |author-last1=Bronstein<!-- 1903–1976 --> |author-first2=Konstantin Adolfovič<!-- Adolfowitsch --> |author-last2=Semendjajew<!-- 1908–1988 --> |editor-first1=Günter |editor-last1=Grosche |editor-first2=Viktor |editor-last2=Ziegler<!-- 1922–1980--> |editor-first3=Dorothea |editor-last3=Ziegler |others=Weiß, Jürgen<!-- lector --> |translator-first=Viktor |translator-last=Ziegler |volume=1 |date=1987 |edition=23 |orig-year=1945 |publisher=[[Verlag Harri Deutsch]] (and [[B. G. Teubner Verlagsgesellschaft]], Leipzig) |___location=Thun and Frankfurt am Main |language=German |chapter=2.4.1.1. |pages=115–120 |isbn=3-87144-492-8}}</ref> for example, so 3+4×5 = 3+(4×5) ≠ (3+4)×5.
▲Operands are mathematical objects upon which the operators operate. These include [[number]]s such as 3 or 1001, [[truth value]]s such as true or false, structures such as vectors, or any other mathematical object. One special type of operand is the parenthesis group. An expression enclosed in parentheses is evaluated recursively and treated, for operator association purposes, as a single operand.
In terms of operator position, an operator may be prefix, postfix, or infix. A [[prefix operator]] immediately precedes its operand, as in −x. A [[postfix operator]] immediately succeeds its operand, as in x! for instance. An [[infix operator]] is positioned in between a left and a right operand, as in x+y. Some languages, most notably the C-syntax family, stretches this conventional terminology and speaks also of ''[[ternary operator|ternary]]'' infix operators (a?b:c). Theoretically it would even be possible (but not necessarily practical) to define parenthesization as a unary bifix operation.
== Operator associativity ==
{{Main|Operator associativity}}
Operator associativity determines what happens when an operand is surrounded by operators of the same precedence, as in 1-2-3: An operator can be [[left-associative]], [[right-associative]], or [[non-associative]]. Left-associative operators are applied to operands in left-to-right order while right-associative operators are the other way round. The basic arithmetic operators are normally all left-associative,<ref name="Bronstein_1987" /> which means that 1-2-3 = (1-2)-3 ≠ 1-(2-3), for instance. This does not hold true for higher operators. For example, [[exponentiation]] is normally right-associative in mathematics,<ref name="Bronstein_1987" /> but is implemented as left-associative in some computer applications like Excel. In programming languages where assignment is implemented as an operator, that operator is often right-associative. If so, a statement like {{mono|1=a := b := c}} would be equivalent to {{mono|1=a := (b := c)}}, which means that the value of c is copied to b which is then copied to a. An operator which is non-associative cannot compete for operands with operators of equal precedence. In [[Prolog]] for example, the infix operator {{mono|1=:-}} is non-associative, so constructs such as {{mono|1=a :- b :- c}} are syntax errors.
Unary prefix operators such as − (negation) or sin (trigonometric function) are typically associative prefix operators. When more than one associative prefix or postfix operator of equal precedence precedes or succeeds an operand, the operators closest to the operand goes first. So −sin x = −(sin x), and sin -x = sin(-x).
Mathematically oriented languages (such as on [[scientific calculator]]s) sometimes allow implicit multiplication with higher priority than prefix operators (such as sin), so that sin 2x+1 = (sin(2x))+1, for instance.{{citation needed|date=November 2015|reason=While this may be correct for some languages, it is not correct mathematically. This needs to be discussed in much better details. See also [[Order of operations]].}}
However, prefix (and postfix) operators do not ''necessarily'' have higher precedence than all infix operators. Some (hypothetical) programming language may well have an operator called sin with a precedence lower than × but higher than + for instance. In such a language, sin 2·x+1 = sin(2·x)+1 would be true, instead of (sin 2)·x+1, as would normally be the case.
# Treat any sub-expression in parentheses as a single recursively-evaluated operand (there may be different kinds of parentheses though, with different semantics).▼
#
Some more examples:
:
==Generalizations of Common Operator Notation==
▲The rules for expression evaluation are simple:
▲# Treat any sub-expression in parentheses as a single recursively-evaluated operand.
▲# Associate operands with operators of higher precedence before those of lower precedence.
▲# Among operators of equal precedence, associate operands with operators according to the associativity of the operators.
▲:<tt>1 + 2 + 3 * 4 * 5 + 6 + 7 = ((((1 + 2) + ((3 * 4) * 5)) + 6) + 7)</tt>
The use of operator precedence classes and associativities is just one way. However, it is not the most general way: this model cannot give an operator more precedence when competing with '−' than it can when competing with '+', while still giving '+' and '−' equivalent precedences and associativities. <!-- speculation... Can it be demonstrated that this is the only sensible way? I do not know, but any such demonstration would be welcome. --> A generalized version of this model (in which each operator can be given independent left and right precedences) can be found at [http://compilers.iecc.com/comparch/article/01-07-068].
Line 60 ⟶ 35:
*[[Order of operations]]
*[[Relational operator]]
*[[Operator (computer programming)]]
*[[Operators in C and C++]]
==References==
{{Reflist}}
{{DEFAULTSORT:Common Operator Notation}}
[[Category:Computer arithmetic]]
[[Category:
|