将表达式与具有不同关联性和相同优先级的相邻运算符进行歧义消除 [英] Disambiguation of expressions with neighboring operators of different associativity and same precedence
本文介绍了将表达式与具有不同关联性和相同优先级的相邻运算符进行歧义消除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个表达式如下(其中⨁
和⨂
是具有相同优先级但不具有相同关联性的二进制运算符):
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
y
是否属于⨁
或⨂
,并且基于什么条件?
Would y
belong to ⨁
or ⨂
, and based on what criteria?
推荐答案
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: ⨂
处于关联状态.该表达式的计算结果为:
Case 1: ⨂
is left associative. The expression evaluates to:
(x ⨁ y) ⨂ z
情况2: ⨂
是正确的关联.该表达式的计算结果为:
Case 2: ⨂
is right associative. The expression evaluates to:
x ⨁ (y ⨂ z)
这篇关于将表达式与具有不同关联性和相同优先级的相邻运算符进行歧义消除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文