Disambiguation of expressions with neighboring operators of different associativity and same precedence

旧巷老猫 提交于 2019-12-10 11:02:00

问题


Say I have an expression as follows (where and are binary operators which have the same precedence level but not the same associativity):

x ⨁ y ⨂ z

Would y belong to or , and based on what criteria?


回答1:


According to the Edsgar Dijkstra's Shunting-yard algorithm if neighboring two operators in an expressions have the same precedence level then the expression is disambiguated based on the associativity of the second operator.

  1. If the second operator is left associative then the operand belongs to the first operator.
  2. If the second operator is right associative then the operand belongs to the second operator.

Case 1: is left associative. The expression evaluates to:

(x ⨁ y) ⨂ z

Case 2: is right associative. The expression evaluates to:

x ⨁ (y ⨂ z)


来源:https://stackoverflow.com/questions/15117457/disambiguation-of-expressions-with-neighboring-operators-of-different-associativ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!