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
3
votes
1 answer

Disambiguation of expressions with neighboring operators of different associativity and same precedence

Say I have an expression as follows (where ⨁ and ⨂ are binary operators which have the same precedence level but not the same associativity): x ⨁ y ⨂ z Would y belong to ⨁ or ⨂, and based on what criteria?
3
votes
2 answers

C++: input and output stream operators: associativity

Input/Output stream operators associativity in theory: LEFT TO RIGHT (for example, according to this: Sait Mary's University website Input/Output stream operators associativity on practice: #include int func0() { std::cout << "func0…
nickolay
  • 3,643
  • 3
  • 32
  • 40
3
votes
8 answers

output of expression in (--i + ++i) in java

int i=9; System.out.println(--i + ++i); output on execution : 17 The final value of i is : 9 But according to associativity and precedence rules in java,, ++i should be executed first i.e from Right to left which gives 10 and then --i gives 9…
Dean Mathew
  • 31
  • 1
  • 2
2
votes
5 answers

Could iostream inserters and extractors be class members instead of global overloads?

Having to declare "global friend operator overloading" to do serialization always struck me as kludgey. It didn't seem foundational to have to declare serialization operators outside of your class. So I was looking for a solid answer for…
2
votes
1 answer

Is the comma in Perl associative in all contexts?

Suppose E, F, and G are expressions that don't involve operators of higher precedence than the comma. Are the expressions ((E, F), G) and (E, (F, G)) equivalent in scalar, list, and/or void contexts? More precisely, can you always replace ((E, F),…
fmg
  • 813
  • 8
  • 18
2
votes
1 answer

How to rearrange newly defined associative terms in Coq?

I wonder what is the most elegant way of rearranging associative terms in Coq. I think it is a well-defined problem for some existing operators, such as plus. I have defined new associative operater and operands with corresponding types. In short,…
2
votes
8 answers

Operator associativity, precedence

I just wonder if, for the following code, the compiler uses associativity/precedence alone or some other logic to evaluate. int i = 0, k = 0; i = k++; If we evaluate based on associativity and precedence, postfix ++ has higher precedence than =,…
2
votes
2 answers

Why does == have higher precedence than postfix ++ in Java?

Please, could someone help me to figure out why equality has higher priority before postfix here? int x = 6; System.out.println(7 == x++); Output: false According to some sources of precedence of operators in Java: postfix should have higher…
2
votes
2 answers

can someone explain me where in the ECMAScript specification operator precedence and operator associativity is mentioned

I was reading mdn docs about operator precedence and operator associativity "operator precedence and operator associativity(MDN)" and wanted to know more about it reading the ECMAScript specification. But i didn't find anything about operator…
2
votes
1 answer

What order of evaluation does?

I am trying to figure out what order of evaluation is. I really don't understand point of order of evaluation. Evaluate it operands, subexpressions, expressions or what ? I was trying to do some research and look at the difference between gcc and vc…
2
votes
1 answer

Operator hierarchy in ++structure.field

The example: ++structure.field; increments field instead of giving "wrong type argument to increment" compiler error, although ++ and . operators are equaly hierarchized ergo: should've been executed from left to right. Am I missing something here?
jbulatek
  • 154
  • 10
2
votes
1 answer

Proving General Associativity in Groups

For a project I'm coding group theory through Coq, obviously associatvity of 3 elements is a given, however I'm struggling to prove it holds for a string of length n. That is, (x1 * ... * xn) is always the same regardless of how many brackets are in…
2
votes
4 answers

Understanding Folds in Haskell

From what I understand about folds in Haskell, foldl (-) 0 [1..5] gives a result of -15 by calculating 0-1-2-3-4-5, and foldr (-) 0 [1..5] gives a result of -5 by calculating 5-4-3-2-1-0. Why is it then that both foldl (++) "" ["a", "b", "c"] and…
2
votes
1 answer

Information Conflicts About Operator Precedence

I've recently been reading Pointers on C book of Kenneth A. Reek. The book has a really nice table of C operators and their precedence levels. However, when I checked other resources to make sure information provided in the book is correct and…
nmd_07
  • 666
  • 1
  • 9
  • 25
2
votes
3 answers

Right associativity of PHP's null coalesce operator

According to PHP documentation, the null coalescing operator ?? is right-associative, i.e. $a ?? $b ?? $c is equivalent to $a ?? ($b ?? $c) What is the significance of this? Does it make any difference for developers that it is left-associative or…
SOFe
  • 7,867
  • 4
  • 33
  • 61