Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operator associativity
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Examples == Associativity is only needed when the operators in an expression have the same precedence. Usually <code>+</code> and <code>-</code> have the same precedence. Consider the expression <code>7 - 4 + 2</code>. The result could be either <code>(7 - 4) + 2 = 5</code> or <code>7 - (4 + 2) = 1</code>. The former result corresponds to the case when <code>+</code> and <code>-</code> are left-associative, the latter to when <code>+</code> and <code>-</code> are right-associative. In order to reflect normal usage, [[addition]], [[subtraction]], [[multiplication]], and [[Division (mathematics)|division]] operators are usually left-associative,<ref>Education Place: [http://eduplace.com/math/mathsteps/4/a/index.html The Order of Operations]</ref><ref>[[Khan Academy]]: [https://www.khanacademy.org/math/pre-algebra/pre-algebra-arith-prop/pre-algebra-order-of-operations/v/introduction-to-order-of-operations The Order of Operations], timestamp [https://www.youtube.com/watch?v=ClYdw4d4OmA&t=5m40s 5m40s]</ref><ref>Virginia Department of Education: [http://www.doe.virginia.gov/instruction/mathematics/middle/algebra_readiness/curriculum_companion/order-operations.pdf#page=3 Using Order of Operations and Exploring Properties], section 9</ref> while for an [[exponentiation]] operator (if present)<ref name="Codeplea_2016">[https://codeplea.com/exponentiation-associativity-options Exponentiation Associativity and Standard Math Notation] Codeplea. 23 Aug 2016. Retrieved 20 Sep 2016.</ref>{{Better source needed|reason=A [[WP:RSSELF|blog isn't a reliable source]].|date=May 2024}} there is no general agreement. Any [[assignment (computer science)|assignment]] operators are typically right-associative. To prevent cases where operands would be associated with two operators, or no operator at all, operators with the same precedence must have the same associativity. === A detailed example === Consider the expression <code>5^4^3^2</code>, in which <code>^</code> is taken to be a right-associative exponentiation operator. A parser reading the tokens from left to right would apply the associativity rule to a branch, because of the right-associativity of <code>^</code>, in the following way: # Term <code>5</code> is read. # Nonterminal <code>^</code> is read. Node: "<code>5^</code>". # Term <code>4</code> is read. Node: "<code>5^4</code>". # Nonterminal <code>^</code> is read, triggering the right-associativity rule. Associativity decides node: "<code>5^(4^</code>". # Term <code>3</code> is read. Node: "<code>5^(4^3</code>". # Nonterminal <code>^</code> is read, triggering the re-application of the right-associativity rule. Node "<code>5^(4^(3^</code>". # Term <code>2</code> is read. Node "<code>5^(4^(3^2</code>". # No tokens to read. Apply associativity to produce parse tree "<code>5^(4^(3^2))</code>". This can then be evaluated depth-first, starting at the top node (the first <code>^</code>): # The evaluator walks down the tree, from the first, over the second, to the third <code>^</code> expression. # It evaluates as: 3{{sup|2}} = 9. The result replaces the expression branch as the second operand of the second <code>^</code>. # Evaluation continues one level up the [[parse tree]] as: 4{{sup|9}} = {{formatnum:262144}}. Again, the result replaces the expression branch as the second operand of the first <code>^</code>. # Again, the evaluator steps up the tree to the root expression and evaluates as: 5{{sup|262144}} β {{val|6.2060699|e=183230}}. The last remaining branch collapses and the result becomes the overall result, therefore completing overall evaluation. A left-associative evaluation would have resulted in the parse tree <code>((5^4)^3)^2</code> and the completely different result (625{{sup|3}}){{sup|2}} = {{formatnum:244140625}}{{sup|2}} β {{val|5.9604645|e=16}}.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)