Content deleted Content added
No edit summary |
m typographic enhancements |
||
Line 5:
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 '
with '+', while still giving '+' 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
Line 26:
higher precedences lower numbers. In cases in which two operators
of different precedences compete for the same operand, the operator
with the higher precedence wins. For example, '
precedence than '+', so 3+
[[Operator position]] indicates where, in the sequence, the operator appears. In terms of operator position, an operator may be prefix, postfix, or infix. A prefix operator immediately precedes its operand, as in "
[[Operator associativity]], loosely speaking, describes what operators
Line 39:
or postfix operator can be either associative or non-associative.
If an operator is left-associative, the operators are applied in
left-to-right order. The arithmetic operators '+', '
'
3+4+5-6-7 = ((((3+4)+5)-6)-7).
If an operator is right-associative, the
operators are applied in right-to-left order. In the [[Java (programming language)|Java programming language]], the assignment operator "<tt>=</tt>" is
right-associative. That is, the Java statement "<tt>a = b = c;</tt>" is
equivalent to "<tt>(a = (b = c));</tt>". It first assigns the value of c to
b, then assigns the value of b to a. An operator which is
non-associative cannot compete for operands with operators of equal
precedence. For example, in [[Prolog]], the infix operator
non-associative, so constructs such as "<tt>a :- b :- c</tt>" constitute
syntax errors. There may nonetheless be a use for such
constructs as "<tt>a =|> b =|> c</tt>".
A prefix or postfix operator is associative if and only if it may
compete for operands with operators of equal precedence. The unary
prefix operators '+', '
associative prefix operators. The unary postfix operator '!' and,
in the [[C (programming language)|C]] language, the post-increment and post-decrement operators
"<tt>++</tt>" and "<tt>--</tt>" are examples of associative postfix operators. When
more than one associative prefix or postfix operator of equal
precedence precedes or succeeds an operand, the operators closer to
the operand associate first. For example,
the expression "<tt>x++--</tt>" is equivalent to "<tt>(x++)--</tt>". When prefix and postfix operators of equal precedence coexist,
the order of association is undefined.
Line 68:
do not necessarily have higher precedence than all infix operators.
For example, if the prefix operator 'sin' is given a precedence
between that of '+' and '
sin
The rules for expression evaluation are simple:
Line 77:
# Among operators of equal precedence, associate operands with operators according to the associativity of the operators.
Note that an infix operator need not be binary. [[C (programming language)|C]], for example, has a ternary infix operator "<tt>[[?:]]</tt>". <!-- speculation Would
it, then, be accurate to call
operation? :) -->
|