I'm in the process of writing a scripting language and I want to copy the (pretty well standardized) C order of operations.
One thing that I never had a firm grasp of as a formal concept though is associativity. Why are some operator groups left-to-right and others right-to-left?
Can someone give me a few examples of how a line of code could look different if the rules were all left-to-right or the opposite of what they were? Or why the associativity is the way it is, as it seems to me just a arbitrary choice, but I assume they had a reason for it.
Also, just to note, I do know what associativity means, I just can't think of any examples where left-to-right (or vice-versa) is better than the other choice
Associativity is the left-to-right or right-to-left order for grouping operands to operators that have the same precedence. An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higher-precedence operators are evaluated first.
Associativity can be either Left to Right or Right to Left. For example: '*' and '/' have same precedence and their associativity is Left to Right, so the expression “100 / 10 * 10” is treated as “(100 / 10) * 10”. 1) Associativity is only used when there are two or more operators of same precedence.
Operators may be associative (meaning the operations can be grouped arbitrarily), left-associative (meaning the operations are grouped from the left), right-associative (meaning the operations are grouped from the right) or non-associative (meaning operations cannot be chained, often because the output type is ...
For example, a+b+c can be interpreted as ((a + b) + c) or as (a + (b + c)). We say that + is left-associative if operands are grouped left to right as in ((a + b) + c). We say it is right-associative if it groups operands in the opposite direction, as in (a + (b + c)).
For the most part, each operator has the associativity that makes the most sense for that operator.
All of the non-assignment binary operators have left-to-right associativity. This is useful for the obvious reason that English is read left-to-right and thus the evaluation of x + y + z
is consistent with how it is read. In addition, for arithmetic operators, the semantics match what we expect from the usage of the operators in mathematics.
Assignment operators have right-to-left associativity. Left-to-right assignment would have bizarre and unexpected semantics. For example, x = y = z
would result in x
having the original value of y
and y
having the original value of z
. It is expected that all three variables will have the same value after the expression is complete.
The prefix unary operators have right-to-left associativity, which makes sense because the operators closest to the operand are evaluated first, so in ~!x
, !x
is evaluated first, then ~
is applied to the result. It would be really, really weird were prefix operators applied with left-to-right associativity: to say that ~!x
means evaluate ~x
and then apply !
to the result is the complete opposite of how we think about expressions (or, at least, how most people think about expressions...).
Examples:
5 - 4 - 3
(5 - 4) - 3 = -2 // left association is correct
5 - (4 - 3) = 4 // right is incorrect
a == b == c // What does this equal?
// It is common to have == be non-associative because of this.
x = y = z
x = (y = z) // right association is correct, sets x and y
(x = y) = z // left is incorrect, does not set y
Most operators inherit their associativity from math. Bitwise can be seen as arithmetic operators and thus have left associativity.
Unary is right associative because it groups that way:
~!-x = ~(!(-(x)))
The other way wouldn't make much sense unless postfix.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With