Questions tagged [associativity]

Anything related to notational associativity of binary operators. The associativity of operators relates to the order of evaluation of subexpressions in an expression where operators having the same precedence are used repeatedly, such as in `a+b+c-d`.

Anything related to notational associativity of binary operators. The associativity of operators relates to the order of evaluation of subexpressions in an expression where operators having the same precedence are used repeatedly, such as in a+b+c-d.

See the Wikipedia page on operator associativity in programming languages.

Note: the concept is related to the associative property of operators in mathematics, but there are some subtle differences. Compare with Wikipedia page on associative property.

A common problem when first learning to parse infix operators using recersive descent is that one will get expressions such as 1+2 or 3-1 or 3*4 or 6/3 to work. Then they will try using multiple sequential of an operator such 1+2+3 which will work but find that 7-3-2 which is equivalent to (7-3)-2 which is 2 will not work and typically get 6 because they have parsed it as 7-(3-2) which is incorrect.

For solutions to this problem
See: Parsing Expressions by Recursive Descent by Theodore Norvell

185 questions
102
votes
10 answers

What is associativity of operators and why is it important?

What is associativity for an operator and why is it important?
nonopolarity
  • 146,324
  • 131
  • 460
  • 740
89
votes
6 answers

C# conditional AND (&&) OR (||) precedence

We get into unnecessary coding arguments at my work all-the-time. Today I asked if conditional AND (&&) or OR (||) had higher precedence. One of my coworkers insisted that they had the same precedence, I had doubts, so I looked it up. According to…
65
votes
3 answers

Why does expression (true == true == true) produce a syntax error?

Ruby: true == true == true syntax error, unexpected tEQ vs. JavaScript: true == true == true // => true vs. C: 1 == 1 == 1 // => 1
47
votes
5 answers

Ternary operator left associativity

In the PHP manual, I find the following 'user contributed note' under "Operators". Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity. You cannot write code like this (as you may…
Kapil Sharma
  • 10,135
  • 8
  • 37
  • 66
34
votes
1 answer

Why is function composition in Haskell right associative?

Mathematically the function composition operation is associative. Hence: f . (g . h) = (f . g) . h Thus the function composition operation may be defined to be either left associative or right associative. Since normal function application in…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
31
votes
3 answers

Associativity of function call operator in C

I was going through the topic of associativity of C operators. There I came across this fact that the function call operator () has a left to right associativity. But associativity only comes to play when multiple operators of the same precedence…
Deepu
  • 7,592
  • 4
  • 25
  • 47
29
votes
5 answers

Who defines C operator precedence and associativity?

Introduction In every textbook on C/C++, you'll find an operator precedence and associativity table such as the following: http://en.cppreference.com/w/cpp/language/operator_precedence One of the questions on StackOverflow asked something like…
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
25
votes
6 answers

When does operator<< refer to the insertion operator and when to the bitwise left shift?

When does operator << refer to the insertion operator and when does it refer to the bitwise left shift? This will output 10, and operator << refers to the left shift. cout << a.b() << a.a.b << endl; And this will output 11, operator << refers to…
jmmom
  • 313
  • 3
  • 7
25
votes
3 answers

Why are logical operators in JavaScript left associative?

The logical AND and OR operators are the only lazy operators in JavaScript along with the ternary conditional operator. They are tested for short-circuit evaluation using the following rules: false && anything === false true || anything ===…
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
22
votes
5 answers

Is floating-point addition and multiplication associative?

I had a problem when I was adding three floating point values and comparing them to 1. cout << ((0.7 + 0.2 + 0.1)==1)<
Karen Tsirunyan
  • 1,938
  • 6
  • 19
  • 30
21
votes
3 answers

Is right-to-left operator associativity in R possible?

I'm new to R, and I just discovered I suffer from Bracket Phobia (see comment in the link). I like the way magrittr notation %>% works, because it avoids nested parenthesis in some situations, and makes code more readable. I came from Mathematica,…
Murta
  • 2,037
  • 3
  • 25
  • 33
19
votes
2 answers

What does it mean that Python comparison operators chain/group left to right?

The Python documentation for operator precedence states: Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons...) What…
Rob Bednark
  • 25,981
  • 23
  • 80
  • 125
19
votes
5 answers

Ternary operator associativity in C# - can I rely on it?

Ahh, don't you just love a good ternary abuse? :) Consider the following expression: true ? true : true ? false : false For those of you who are now utterly perplexed, I can tell you that this evaluates to true. In other words, it's equivalent to…
Vilx-
  • 104,512
  • 87
  • 279
  • 422
16
votes
1 answer

How can (<$>) be left associative

I just noticed that (<$>) has a fixity of infixl 4. How can this be? (+1) <$> (/5) <$> [5,10] obviously works right to left.
hgiesel
  • 5,430
  • 2
  • 29
  • 56
15
votes
3 answers

Multiple assignment confusion

I understand that the assignment operator is right associative. So for example x = y = z = 2 is equivalent to (x = (y = (z = 2))) That being the case, I tried the following: foo.x = foo = {a:1} I expected that the object foo would be created with…
Danield
  • 121,619
  • 37
  • 226
  • 255
1
2 3
12 13