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!
=== 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)