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
8
votes
3 answers

C++ overloaded operator with reverse order of associativity

It was very hard to come up with a title... (I'm not a native English speaker.) struct A { int value; A operator+(int i) const { A a; a.value=value+i; return a; }; }; int main(){ A a; a.value=2; …
Hassedev
  • 621
  • 6
  • 17
7
votes
2 answers

BNF grammar for left-associative operators

I have the following EBNF grammar for simple arithmetic expressions with left-associative operators: expression: term {+ term} term: factor {* factor} factor: number ( expression ) How can I convert this into a BNF grammar…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
6
votes
2 answers

Relation between grammar and operator associativity

Some compiler books / articles / papers talk about design of a grammar and the relation of its operator's associativity. I'm a big fan of top-down, especially recursive descent, parsers and so far most (if not all) compilers I've written use the…
LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
6
votes
2 answers

How to explain this operator associativity?

According to this table, ++ has right to left associativity. So, I run this code: int a = 5; ++a + ++a * ++a and expect the expression to be 50 (as 8 + 7 * 6, increment starts from right to left). But the expression is evaluated from left to right…
Gaurang Tandon
  • 6,504
  • 11
  • 47
  • 84
5
votes
2 answers

Syntax for partial application of curried functions with reverse-associative infix notation

In other words, is there a good reason why this shouldn't compile? def f(xs: List[Int]) = xs.foldLeft(0) _ // OK def f(xs: List[Int]) = (xs :\ 0) _ // OK def f(xs: List[Int]) = (0 /: xs) _ :15: error: missing arguments for method /:…
Luigi Plinge
  • 50,650
  • 20
  • 113
  • 180
5
votes
1 answer

How do operator associativity, the associative law and value dependencies of monads fit together?

On the one hand the monadic bind operator >>= is left associative (AFAIK). On the other hand the monad law demands associativity, i.e. evaluation order doesn't matter (like with monoids). Besides, monads encode a value dependency by making the next…
user5536315
5
votes
1 answer

Can monad with broken associativity law yield incorrect result in for-comprehension?

Here is a Monad instance for ListT (copied from montrivo) case class ListT[M[_], A](value: M[List[A]]) implicit def listTMonad[M[_]: Monad] = new Monad[ListT[M, *]] { override def flatMap[A, B](fa: ListT[M, A])(f: A => ListT[M, B]): ListT[M, B] = …
Mario Galic
  • 47,285
  • 6
  • 56
  • 98
5
votes
1 answer

Why is my recursive descent parser right-associative

I'm writing my own programming language, and I have the tokenizer (lexer) all done. But for parsing, I'm having trouble writing a recursive descent parser. It seems to be right associative, when it should be left, and I don’t know why. For example,…
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
5
votes
3 answers

Associativity of comparison operators in Python

What is the associativity of comparison operators in Python? It is straightforward for three comparisons, but for more than that, I'm not sure how it does it. They don't seem to be right- or left-associative. For example: >>> 7410 >= 8690 <= -4538 <…
5
votes
1 answer

Why is Nil coalescing operator Right Associative?

Shouldn’t it be Left Associative? I think let a = b ?? c ?? d is grouped like let a = (b ?? c) ?? d not let a = b ?? (c ?? d) But it is declared as a Right Associative. Do I misunderstand or miss something?
5
votes
2 answers

Why is the $! operator right-associative?

I'm just learning Haskell and I'm still not entirely clear on when and how strict evaluation is forced When I want a function to evaluate its arguments strictly I find myself writing ((f $! x) $! y ) $! z which seems weird. Shouldn't $! be…
dspyz
  • 5,280
  • 2
  • 25
  • 63
5
votes
3 answers

If left to right and right to left - both associativity of operator are present in a statement then What will be considered?

int i=-1; int a=65; int b=a*i + ++i; What is the value of b? Here associativity of =,+ is left to right and associativity of *,prefix increment (++) is right to left. So What order of evaluation should I consider for int b=a*i + ++i; left to…
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72
5
votes
0 answers

Understanding PHP's Left Assocative Ternary Operator

Is there a documented explanation/reason why PHP's ternary operator ? : is left associative rather then right associative. Surely a reason exists for differing the operator from all other imperative languages?
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
5
votes
4 answers

Do the binary boolean operators have associativity?

Is a && b && c defined by the language to mean (a && b) && c or a && (b && c)? Wow, Jerry was quick. To beef up the question: does it actually matter? Would there be an observable difference between a && b && c being interpreted as (a && b) && c or…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
5
votes
2 answers

When does operator associativity matter?

Most programming languages have a table of precedence and associativity for binary operators. Associativity matters in some cases e.g. (a - b) - c != a - (b - c). However, for an associative operator like && it would seem not to matter, yet most…
rwallace
  • 31,405
  • 40
  • 123
  • 242
1 2
3
12 13