为什么关联性是运算符的基本属性,而不是优先级的属性 [英] Why Associativity is a Fundamental Property of Operators But Not that of Precedence Levels

查看:124
本文介绍了为什么关联性是运算符的基本属性,而不是优先级的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何编程语言教科书中,总是告诉我们该语言中的每个运算符如何具有左或右关联性.似乎关联性是任何运算符的基本属性,而不管它采用多少个操作数.在我看来,无论如何将关联性分配给其他运算符,都可以将任何关联性分配给任何运算符.

In any programming language textbooks, we are always told how each operator in that language has either left or right associativity. It seems that associativity is a fundamental property of any operator regardless of the number of operands it takes. It also seems to me that we can assign any associativity to any operator regardless of how we assign associativity to other operators.

但是为什么会这样呢?也许一个例子更好.假设我要设计一种假设的编程语言.以这种任意方式(都具有相同的优先级)将关联性分配给这些运算符是否有效:

But why is it the case? Perhaps an example is better. Suppose I want to design a hypothetical programming language. Is it valid to assign associativity to these operators in this arbitrary way (all having the same precedence):

unary operator: 
! right associative 
binary operators:
+ left associative
- right associative
* left associative 
/ right associative

! +-*/是我的5个运算符都具有相同的优先级.

! + - * / are my 5 operators all having the same precedence.

如果是,我的假设解析器如何将2 + 2!3 + 5 * 6/3-5!3!3-3 * 2括起来?以及为什么.

If yes, how would an expression like 2+2!3+5*6/3-5!3!3-3*2 is parenthesized by my hypothetical parser? And why.

第一个示例(2 + 2!3 + 5 * 6/3-5!3!3-3 * 2)不正确.也许忘了一元运算,让我这样说吧,是否可以像我上面那样分配具有相同优先级且不同关联性的运算符?如果是,将如何评估示例2 + 3-4 * 5/3 + 2?因为大多数编程语言似乎都给具有相同优先级的运算符分配了相同的关联性.但是,我们总是谈论运营商的关联性,就好像它是单个运营商的财产,而不是优先级的财产一样.

The first example (2+2!3+5*6/3-5!3!3-3*2) is incorrect. Perhaps forget about the unary op and let me put it this way, can we assign operators having the same precedence different associativity like the way I did above? If yes how would an example,say 2+3-4*5/3+2 be evaluated? Because most programming language seems to assign the same associativity to the operators having the same precedence. But we always talk about OPERATOR ASSOCIATIVITY as if it is a property of an individual operator - not a property of a precedence level.

推荐答案

让我们记住关联性的含义.接受任何运算符,例如@.众所周知,它的关联性是消除a @ b @ c形式的表达式的歧义的规则:如果@保持关联性,则将其解析为(a @ b) @ c;如果正确关联,则a @ (b @ c).也可能是非关联的,在这种情况下,a @ b @ c是语法错误.

Let us remember what associativity means. Take any operator, say @. Its associativity, as we all know, is the rule that disambiguates expressions of the form a @ b @ c: if @ is left associative, it's parsed as (a @ b) @ c; if it's right associative, a @ (b @ c). It could also be nonassociative, in which case a @ b @ c is a syntax error.

如果我们有两个不同的运算符,例如@#,该怎么办?如果一个优先级比另一个优先级高,那么就无话可说了,没有任何工作要做.优先考虑消除歧义.但是,如果它们具有同等的优先权,我们就需要协会的帮助.共有三种简单的情况:

What about if we have two different operators, say @ and #? If one is of higher precedence than the other, there's nothing more to say, no work for associativity to do; precedence takes care of the disambiguation. However, if they are of equal precedence, we need associativity to help us. There are three easy cases:

  • 如果两个运算符都保持关联,则a @ b # c表示(a @ b) # c.
  • 如果两个运算符都正确关联,则a @ b # c表示a @ (b # c).
  • 如果两个运算符都是非关联的,则a @ b @ c是语法错误.
  • If both operators are left associative, a @ b # c means (a @ b) # c.
  • If both operators are right associative, a @ b # c means a @ (b # c).
  • If both operators are nonassociative, then a @ b @ c is a syntax error.

在其余情况下,操作员不同意关联性.哪个运营商的选择优先?您可能可以设计这样的关联性优先规则,但是我认为施加的最自然的规则是声明任何这种大小写语法错误.毕竟,如果两个运算符具有相同的优先级,为什么一个运算符比另一个运算符具有更高的优先级?

In the remaining cases, the operators do not agree about associativity. Which operator's choice gets precedence? You could probably devise such associativity-precedence rules, but I think the most natural rule to impose is to declare any such case syntax errors. After all, if two operators are of equal precedence, why one would have associativity-precedence over the other?

根据我刚才给出的自然规则,您的示例表达式是语法错误.

Under the natural rule I just gave, your example expression is a syntax error.

现在,我们当然可以为具有相同优先级的运算符分配不同的关联性.但是,这意味着存在相同优先级的运算符组合(例如您的示例!),它们是语法错误.大多数语言设计人员似乎更喜欢避免这种情况,并为所有优先级相同的运算符分配相同的关联性.这样,所有组合都是合法的.我认为这只是美学.

Now, we could certainly assign differing associativities to operators of the same precedence. However, this would mean that there are combinations of operators of equal precedence (such as your example!) that are syntax errors. Most language designers seem to prefer to avoid that and assign the same associativity to all operators of equal precedence; that way, all combinations are legal. It's just aesthetics, I think.

这篇关于为什么关联性是运算符的基本属性,而不是优先级的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆