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

Python operator `in` and `==` precedence and associativity

I noticed strange behaviour with python operator in and == precedence and associativity. Here is a snippet example: b = {} b['foo'] = 'coo' print('foo' in b) # prints True, expected True print(('foo' in b) == True) # prints True,…
0
votes
1 answer

right associativity and order of execution of nested ternary operator in c++

I have the solution regarding execution order, but I cant understand how right associativity is linked to SCENARIO 2. a ? b: c ? d : e ? f : g ? h : i // scenario 1 : associativity understood, which is : (a?b:(c?d:(e?f:(g?h:i)))) and a ? b ? c : d…
0
votes
0 answers

Parse left associative operators using a greedy parser

This seems to be a pretty fundamental flaw of greedy parsers that you cannot parse left associative operators because infinite recursion will always occur. Is there a way to prevent this from happening or is this just reality?
Joran
  • 154
  • 1
  • 10
0
votes
1 answer

Why 5 <= -15 == 5 >= 1 != 20 is False?

Can someone tell me why 5 <= -15 == 5 >= 1 != 20 is False. Because when I tried to solve it I did the following: i) <= , >= have higher priority so it becomes False == True != 20 ii) ==,!= have same priority so based on associativity i.e from left…
Hari Kiran
  • 19
  • 3
0
votes
1 answer

Could rounding error occurs in the addition between float and an associative addition of integer?

(1) I understand that integer addition is associative, for example (res1 & res always produce the same result) int i1, i2, i3; int res1 = i1 + i2 + i3; int res2 = i1 + (i2 + i3); (2) I also understand that rounding error may occur in the following…
LI.LE
  • 97
  • 5
0
votes
0 answers

Force right-associativity when using left-associative C++ operator

I'm trying to force right-associativity when using a left-associative C++ operator (like operator+()) but I'm "dying" just before the end (see the last static_assert). Having some objects A a; B b; C c; D d; and the overloaded operator+(), I would…
Solo
  • 105
  • 8
0
votes
0 answers

Ensure associativity of an operation in Scala

In Scala, I would like to ensure that a lambda is associative. For example, I imagine adding an annotation or extending a trait, then concrete implementation would be tested using reflexion. But I would like to have the opinion of people more…
0
votes
2 answers

operators precedence and associativity in C

i would be grateful if somebody could help me with this problem. The book I am currently reading has a question Q What will be the output? #include void main() { int a = 3, b = 2; a = a ==b==0; printf("%d, %d",a,b); } The answer is given…
0
votes
0 answers

Associativity of a unary operator

When building a unary operator in a tree, I usually draw it as a parent-child tree, for example: -4 (-) | | 4 And when drawing a binary operator in a tree, it will have a left and right node, something like: 2-4 - / \ 2 4 It…
carl.hiass
  • 1,526
  • 1
  • 6
  • 26
0
votes
2 answers

Right Associativity of Ternary Operator

std::cout << (true ? "high pass" : false ? "fail" : "pass") is the same as std::cout << (true ? "high pass" : (false ? "fail" : "pass")) Since the ternary operator is right associative, why don't we perform the right-hand operation first?…
JayYay
  • 27
  • 3
0
votes
1 answer

Not getting the right answer from celcius to farenheit

static void CToF(float c){ float f=32+((9/5)*c); System.out.printf("%.2f",f); } public static void main(String[] args) { CToF(27); sc.close(); } Here the priority of the * and / same so according to…
0
votes
3 answers

Why values of i and j are 2 after the execution of statement " j= i++ + ++i"?

The code I used is int i=0, j=0; j=i++ + ++i; And the output I got is i=2 and j=2 Could anyone explain how this happens!
Jahnavi
  • 11
  • 3
0
votes
0 answers

Associative matrix multiplication in R

I have two matrices, A and B, and I am attempting to multiply them in the following way: t(A%*%B)%*%A Typically, the order of matrix multiplication matters, but in this case B is a diagonal matrix with all elements equal. Therefore, I can write B =…
EB727
  • 43
  • 5
0
votes
0 answers

Is associativity and precedence of Operators different in C and C++? If so, Why?

Recently I've come across this code, Which I assumed to understand and didn't bother to run, My answer (that i assumed in my head) was confirmed by the book Let Us C. Later when I try to give it a run in C, to my surprise the output was completely…
0
votes
0 answers

associative law fails for matrix multiplication? output looks like random numbers

What am I missing? The following code seems to show the failure of associative law using numpy. It is not just a little off; 3 matricies A, B, C multiplied as (AB)C looks correct but multiplied as A(BC) the output looks like random numbers. The…