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

ternary operator and assignment operator

in Does the C/C++ ternary operator actually have the same precedence as assignment operators? Luchian Grigore's answer says that cases like a ? b : c = d will always be inferred as a ? b : ( c = d ) because both = and ?: associate right to left…
5
votes
4 answers

How does Perl decide which order to evaluate terms in an expression?

Given the code: my $x = 1; $x = $x * 5 * ($x += 5); I would expect $x to be 180: $x = $x * 5 * ($x += 5); #$x = 1 $x = $x * 5 * 6; #$x = 6 $x = 30 * 6; $x = 180; 180; But instead it is 30; however, if I change the ordering of the…
Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
5
votes
2 answers

Why Associativity is a Fundamental Property of Operators But Not that of Precedence Levels

In any programming language textbooks, we are always told how each operator in that language has either left or right associativity. It seems that associativity is a fundamental property of any operator regardless of the number of operands it…
4
votes
1 answer

Unexpected behaviour of "!print("1") || 1" in php

Example1: if(!print("1") || 1){ echo "a"; }else{ echo "b"; } Output 1b The Example 1 is printing "1b" instead of "1a". According to me, inside if the final condition should be if(0 || 1) after solving !print("1"). But the Example 2 is…
4
votes
1 answer

Can parentheses override an expression's order of evaluation?

Grouping operators and operands and Order of Evaluation are two important concepts of expression in C++. Grouping For expression with multiple operators, how the operands grouped with the specific operators is decided by the precedence and…
SLN
  • 4,772
  • 2
  • 38
  • 79
4
votes
2 answers

C++ STL string operator+ associativity

I tried out the following code in VC++ 2015 #include #include using namespace std; int foo(int v) { cout << v << endl; return 10; } string bar(int v) { cout << v << endl; return "10"; } int main() { auto a…
CXuesong
  • 552
  • 5
  • 12
4
votes
3 answers

Why do different operators have different associativity?

I've got to the section on operators in The Ruby Programming Language, and it's made me think about operator associativity. This isn't a Ruby question by the way - it applies to all languages. I know that operators have to associate one way or the…
Skilldrick
  • 69,215
  • 34
  • 177
  • 229
4
votes
2 answers

What does this PHP function return?

public function add($child){ return $this->children[]=$child; } Btw, this is an excerpt from PHP in Action by Dagfinn Reiersol. According to the book, this returns $child, but shouldn't it return true in case of successful assignment and…
Felipe
  • 11,557
  • 7
  • 56
  • 103
4
votes
2 answers

Integer overflow and order of operations

I recently faced a problem on a C++ code of mine making me wonder whether I had some misunderstanding of what the compiler would do with long operations... Just look at the following code: #include int main() { int i = 1024, j =…
Gilles
  • 9,269
  • 4
  • 34
  • 53
4
votes
2 answers

Matching tuples in Prolog

Why does Prolog match (X, Xs) with a tuple containing more elements? An example: test2((X, Xs)) :- write(X), nl, test2(Xs). test2((X)) :- write(X), nl. …
milosz
  • 865
  • 2
  • 7
  • 24
4
votes
2 answers

How is "$foo = 5 && $bar = 15" evaluated, and why is it not a error?

Let's say we got a simple code like this: // $foo and $bar aren't defined before $foo = 5 && $bar = 15; // var_dump() // $foo is (bool) TRUE // $bar is (int) 15 so I assume it works like: $foo = (5 && ($bar = 15)) but in my opinion it should…
4
votes
3 answers

What is the precidency and associtivity for increment operator and assignment operator for the block of code

What is the precidency and associtivity for increment operator and assignment operator for the block of code $a=array(1,2,3); $b=array(4,5,6); $c=1; $a[$c++]=$b[$c++]; print_r($a); As per the execution it outputs Array ( [0]…
bikashphp
  • 165
  • 3
  • 10
4
votes
3 answers

Operator associativity in C specifically prefix and postfix increment and decrement

In C operation associativity is as such for increment, decrement and assignment. 2. postfix ++ and -- 3. prefix ++ and -- 16. Direct assignment = The full list is found here Wikipedia Operators in C My question is when we have int a,…
PJT
  • 3,439
  • 5
  • 29
  • 40
4
votes
1 answer

Expression grammar with exponentiation operator using Boost Spirit

I would like to add the exponentiation operator to the expression grammar provided in the Boost spirit samples. The BNF grammar is the following: (see this answer for example: "Unambiguous grammar for exponentiation operation" ) E -> E + T | E - T |…
4
votes
2 answers

Operator precedence and Associativity in C/C++

Please note, that this has nothing to do with Operator Precedence.. () and ++ , Undefined behavior and sequence points , Why are these constructs (using ++) undefined behavior? and the hundreds similar questions about this here Shortly: is the…
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187