ebooksgratis.com

See also ebooksgratis.com: no banners, no cookies, totally FREE.

CLASSICISTRANIERI HOME PAGE - YOUTUBE CHANNEL
Privacy Policy Cookie Policy Terms and Conditions
Operator associativity - Wikipedia, the free encyclopedia

Operator associativity

From Wikipedia, the free encyclopedia

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

[edit] 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))).

[edit] 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.

[edit] 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.

[edit] See also


aa - ab - af - ak - als - am - an - ang - ar - arc - as - ast - av - ay - az - ba - bar - bat_smg - bcl - be - be_x_old - bg - bh - bi - bm - bn - bo - bpy - br - bs - bug - bxr - ca - cbk_zam - cdo - ce - ceb - ch - cho - chr - chy - co - cr - crh - cs - csb - cu - cv - cy - da - de - diq - dsb - dv - dz - ee - el - eml - en - eo - es - et - eu - ext - fa - ff - fi - fiu_vro - fj - fo - fr - frp - fur - fy - ga - gan - gd - gl - glk - gn - got - gu - gv - ha - hak - haw - he - hi - hif - ho - hr - hsb - ht - hu - hy - hz - ia - id - ie - ig - ii - ik - ilo - io - is - it - iu - ja - jbo - jv - ka - kaa - kab - kg - ki - kj - kk - kl - km - kn - ko - kr - ks - ksh - ku - kv - kw - ky - la - lad - lb - lbe - lg - li - lij - lmo - ln - lo - lt - lv - map_bms - mdf - mg - mh - mi - mk - ml - mn - mo - mr - mt - mus - my - myv - mzn - na - nah - nap - nds - nds_nl - ne - new - ng - nl - nn - no - nov - nrm - nv - ny - oc - om - or - os - pa - pag - pam - pap - pdc - pi - pih - pl - pms - ps - pt - qu - quality - rm - rmy - rn - ro - roa_rup - roa_tara - ru - rw - sa - sah - sc - scn - sco - sd - se - sg - sh - si - simple - sk - sl - sm - sn - so - sr - srn - ss - st - stq - su - sv - sw - szl - ta - te - tet - tg - th - ti - tk - tl - tlh - tn - to - tpi - tr - ts - tt - tum - tw - ty - udm - ug - uk - ur - uz - ve - vec - vi - vls - vo - wa - war - wo - wuu - xal - xh - yi - yo - za - zea - zh - zh_classical - zh_min_nan - zh_yue - zu -