Operator associativity [操作符的结合性](2008-05-22 09:22:23)

From Wikipedia, the free encyclopedia 译文:bigleo

In programming languages, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. Operators may be left-associative, right-associative or non-associative. The associativity and precedence of an operator depends on the programming language in question. Many programming language manuals provide a table of operator precedence and associativity.[译文:在程序设计语言中,操作符的结合性用于在缺少括号的情况下,如何确定相同优先级的操作符的被分组的情况.操作符可能是左结合的,右结全的或无结合的.操作符的结合性和优先级依赖于计论的程序设计语言.大多数程序设计语言提供一个操作条符的优先级和结合性的表格.]

Contents

What is associativity

Associativity is only needed when the operators in an expression have the same precedence. Usually + and - have the same precedence. Consider the expression 7-4+2. The result could be either (7-4)+2 = 5 or 7-(4+2) = 1. The former result corresponds to the case when + and - are left-associative. The latter result corresponds to the case when + and - are right-associative. Operators with the same precedence always have the same associativity. Usually the operators +, -, * and / are left-associative, while exponentiation and assignment operators are right-associative.

A left-associative operator can be said to associate "to the left", and similarly a right-associative operator can be said to associate "to the right". To understand the intuition behind these names, consider again the expression 7-4+2. If + and - are left-associative then the middle operand (4) belongs to the operator on its left (hence the name "to the left"). If + and - are right-associative then the middle operand belongs to the operator on its right (hence the name "to the right").

A left-associative operator may also be said to have "left to right" associativity, and a right-associative operator may also be said to have "right to left" associativity. This is somewhat counter-intuitive considering the above paragraph. To understand the intuition behind these names consider the expression 1+2+3+4+5. If + is left-associative, the addition operations are carried out left to right, i.e. the result is (((1+2)+3)+4)+5. If + is right-associative, the addition operations are carried out right to left, i.e. the result is 1+(2+(3+(4+5))).

Note about right-associative operators

It may seem counter-intuitive that assignment operators such as =, and also +=, -=, etc, have an associativity if you are unfamiliar with the idea of treating an assignment as an expression with side effects. In languages such as C, the assignment a=b is an expression that returns the value b, with the side effect of setting a to the value of b. Thus an assignment can be performed in the middle of an expression. Note that an expression with side effects can be made into a statement by following it with a semicolon (i.e. a=b is an expression but a=b; is a statement). The right-associativity of = allows statements such as a=b=c; which means a=(b=c); which sets both a and b to the value of c. The alternative (a=b)=c; does not make sense because a=b is not an lvalue.

Exponentiation makes perfect sense as a right-associative operator. In Python, exponentiation is denoted by the ** operator. The expression a ** b ** c means a ** (b ** c). The alternative (a ** b) ** c would usually be equivalent to a ** (b * c) by the mathematical identity (ab)c = abc, but calculated less efficiently.

Non-associative operators

In Prolog, the infix operator :- is non-associative because constructs such as "a :- b :- c" constitute syntax errors. In python, the operators >, >=, <= and < are non-associative because expressions such as a < b < c are allowed and are shorthand for (a < b) and (b < c). Thus operator associativity doesn't make sense.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值