Common operator notation: Difference between revisions

Content deleted Content added
More to the point (better flow, coherence, correctness, and less redundancy).
More tidying up and better examples.
Line 2:
{{Unreferenced|date=August 2009}}
{{Cleanup-reorganize|date=July 2007}}
In [[programming languages]], [[scientific calculator]]s and similar '''common operator notation''' or '''operator grammar''' is a way to define and analyse mathematical and other formal expressions. In this model a linear sequence of tokens are divided into two classes: [[operator (programming)|operator]]s and operands.
 
Operands are objects upon which the operators operate. These include literal [[number]]s and other constants as well as identifiers (names) which may represent anything from simple scalar variables to complex aggregated structures and objects, depending on the complexity and capability of the language at hand as well as usage context. One special type of operand is the parenthesis group. An expression enclosed in parentheses is typically recursively evaluated to be treated as a single operand on the next evaluation level.
 
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 that takes an operand surrounded by two operators of different precedence (or priority). Multiplication normally has higher precedence than addition, for example, so 3+4×5 = 3+(4×5) ≠ (3+4)×5.
 
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 existsis positioned in between itsa left and a right operandsoperand, as in "A + B". Some languages, most notably the C-syntax family, stretches this convention and speaks also of ''ternary'' infix operators (a?b:c). Theoretically it would even be possible (but not nessecarily practical) to define parenthesization as an n-ary bifix operation.
 
'''[[Operator associativity]]''', describesdetermines what happens when an operand is surrounded by operators of the same precedence; subtractionAn isoperator normallycan be left-associative, right-associative, foror examplenon-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, sowhich means that 1-2-3 = (1-2)-3 ≠ 1-(2-3), for instance. AnIn operatorprogramming canlanguages bewhere left-associative,assignment right-associativeis implemented as an operator, orthat nonoperatior is often right-associative. If anso, operatora isstatement left-associativelike a := b := c would be equivalent to a := (b := c), which means that the operatorsvalue areof appliedc inis copied left-to-right orderb which is then copied to a. The basicAn arithmeticoperator which is non-associative cannot compete for operands with operators of equal precedence. In [[Prolog]] for example, arethe normallyinfix alloperator left:- is non-associative., so Thatconstructs is,such as a :- b :- c are syntax errors.
 
Unary prefix operators such as − (negation) or sin (trigonometric function) are typically associative prefix operators, for example. 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).
:<math>3+4+5-6-7 = ((((3+4)+5)-6)-7).</math>
 
Mathematically oriented languages (such as on [[scientific calculator]]s) often allow implicit multiplication with higher priority than prefix operators (such as sin). Therefore, sin 2x+1 = (sin(2x))+1, for instance, just as in mathematics.
Right-associative operators are applied in right-to-left order. In programming languages where assignment is an operator, that operatior is often right-associative. If so, the statement a := b := c is equivalent to 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 :- is non-associative, so constructs such as a :- b :- c are syntax errors. There may nonetheless be constructs like a =|> b =|> c.
 
The unaryHowever, prefix (and postfix) operators '+',do not '', and necessarily'sin', forhave example,higher areprecedence typicallythan associativeall prefixinfix operators. WhenSome more(hypothetical) thanprogramming onelanguage associativemay prefixwell orhave postfixan operator ofcalled equalsin precedencewith precedesa orprecedence succeedslower anthan operand,× thebut operatorshigher closestthan to+ thefor operand goes firstinstance. In Forsuch examplea language, −sinsin x+1 = −(sin(x)+1 would be true, instead of (sin 2)·x+1, as in mathematics.
 
Prefix and postfix operators do not necessarily have higher precedence than all infix operators. For example, <u>if</u> the prefix operator sin was given a precedence between that of + and ×, then
:<math>\sin 4 \cdot 3+2 = ((\sin(4 \cdot 3)) + 2)</math>
not
:<math>(((\sin 4) \cdot 3) + 2)</math>
or
:<math>\sin(((4 \cdot 3)+2))</math>
 
The rules for expression evaluation are usually three-fold:
Line 31 ⟶ 24:
 
Examples:
:<tt>1 + 2 + 3 * 4 * 5 + 6 + 7 = ((((1 + 2) + ((3 * 4) * 5)) + 6) + 7)</tt>
 
:<tt>4 + -x + 3 = ((4 + (-x)) + 3)</tt>
 
==Generalizations of Common Operator Notation==