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
0
votes
2 answers

What's the order of evaluation with this compound assignment in Java?

ListNode odd = head; ListNode even = head.next; odd = odd.next = even.next; even = even.next = odd.next; With this line: odd = odd.next = even.next; is even.next assigned to odd.next and then odd.next is assigned to odd? or is…
Bob
  • 71
  • 1
  • 7
0
votes
1 answer

Multi-element subtraction in Z3

I'm using Z3 to solve a problem that needs subtraction and I've run into the fact that subtraction in Z3 allows multiple arguments. This seems odd to me as subtraction is not an associative operation. This can be seen from the following…
4e554c4c
  • 478
  • 3
  • 12
0
votes
1 answer

How does associativity of operator work during evaluation of expression?

I was learning precedence rule in C++. In which conditional operator is said to have Right to Left associativity. I interpret this as evaluation of expression start from right and proceed to left. So, for the below code- int a=1, b=2, c; c=a>b?…
0
votes
1 answer

++: does not appear to be right associative?

I am trying to learn scala. I was looking at the documentation for queues (https://www.scala-lang.org/api/current/scala/collection/immutable/Queue.html). It is my understanding that methods that end in a colon are right associative. However, to…
William Allcock
  • 134
  • 2
  • 9
0
votes
1 answer

How to identify the order of evaluation of a mathematical expression and its correctness?

Is a - b + c - d = a + c - b - d mathematically correct? I believe this statement can be correct but only sometimes if the order of evaluation does not matter so if I were to do {(a - b) + c} - d and choose numbers that would evaluate to {(a + c) -…
C2K
  • 43
  • 6
0
votes
2 answers

Clarification on math expression using prefix increment operator and bracket precedence

Given this code: int p,k=8; p=k*(++k-8); System.out.println(p); when ++k is evaluated k=9 and then that becomes k*(9-8) giving 9*1 int p,k=8; p=(++k-8)*k; System.out.println(p); But this gives 9 as output
0
votes
0 answers

JavaScript operator precedence (associativity)

I'm looking at the JavaScript operator precedence. What does n/a in associativity mean? For example, new has associativity of n/a, but I'm confused on why it's not left-to-right.
stumped
  • 3,235
  • 7
  • 43
  • 76
0
votes
1 answer

weird operator precedence and assignment behavior in borland turboC++

I have to use borland TurboC++ for C programming in my college. They say our examination board recommends it. I have to use it.. The problem is that they gave this operator precedence related question: int a=10,b=20,result; result1 = ++a + b-- -…
IBA4
  • 3
  • 3
0
votes
2 answers

Why does FULL JOIN order make a difference in these queries?

I'm using PostgreSQL. Everything I read here suggests that in a query using nothing but full joins on a single column, the order of tables joined basically doesn't matter. My intuition says this should also go for multiple columns, so long as every…
Lenoxus
  • 545
  • 1
  • 4
  • 19
0
votes
2 answers

Equality operator precedence not working

In C++, the equality operator's associativity is left to right, as stated here and here and the returned value of an assignment operation is the value assigned to the given variable. (As shown here, here, here, and here, (section 6.5.16, pages…
Kröw
  • 504
  • 2
  • 13
  • 31
0
votes
1 answer

Can I change the associativty of an operator?

There is a very nice table of Ruby's operators and their respective associativities here. According to this table, a fair number of operators are left associative, and this is not particularly useful for me. Is there a way that I can change a left…
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
0
votes
1 answer

Operator Associativity

I have the following EBNF expression grammar: -> { (+|-) } -> { (*|/|%) } -> { ** } -> ( ) | -> A | B | C I need to determine if the…
Noles320
  • 1
  • 2
0
votes
0 answers

Is right-to-left operator associativity in R possible? (other version)

In R left-to-right associativity is the norm, for instance: 5 - 3 - 1 == (5 - 3) - 1 # TRUE 5 - 3 - 1 == 5 - (3 - 1) # FALSE Is there a way to get right-to-left associativity instead, for a given operator? Note that I am aware of this post;…
polmath
  • 335
  • 2
  • 13
0
votes
1 answer

Effect of yacc operator associativity declaration on expressions that have just a few tokens

When you have a grammar like this one: B: 'a' A 'a' | 'b' A 'b' A: 'a' 'a' | 'a' The %right 'a' declaration causes aa.a not to be accepted because a shift happens instead of a reduce at '.', and %left 'a' doesn't accept neither aa.aa nor…
user2373145
  • 332
  • 2
  • 14
0
votes
1 answer

Precedence and associativity in operations in compilers

How is a~b~c^d evaluated when both operators have same precedence and ~ and ^ are left and right associative respectively. Can somebody explain. Thanks