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

Slightly different answers when changing associativity

With the following simple C++ exercise #include using namespace std; int main() { int euro, cents_v1, cents_v2, do_again; double price; do_again = 1; while(do_again != 0){ cout<<"Insert price in Euro with…
SeF
  • 3,864
  • 2
  • 28
  • 41
2
votes
3 answers

Parser combinator grammar not yielding correct associativity

I am working on a simple expression parser, however given the following parser combinator declarations below, I can't seem to pass my tests and a right associative tree keeps on popping up. def EXPR:Parser[E] = FACTOR ~ rep(SUM|MINUS) ^^ {case a~b…
Chad
  • 2,041
  • 6
  • 25
  • 39
2
votes
1 answer

Antlr4 left-recursive rule appears to produce right-associative parse

The following grammar illustrates the issue: // test Antlr4 left recursion associativity grammar LRA; @parser::members { public static void main(String[] ignored) throws Exception{ final LRALexer lexer = new LRALexer(new…
2
votes
3 answers

Function application associates to the left

According to this table, function application associates to the left. What does that mean? Associativity is important when a binary operator appears multiple times, like in a - b - c. How is that relevant to function application? How would function…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
2
votes
2 answers

Why are there different associativities among operators in C?

Talking about the associativity of operators in C, I was wondering why there are differences associativities among operators that have the same precedence. for example, postfix increment and postfix decrement have left associativity; while prefix…
ipkiss
  • 13,311
  • 33
  • 88
  • 123
2
votes
3 answers

In SQL, what does using parentheses with an OR mean?

Example: select count(*) from my table where column1 is not null and (column1 = 4 OR column1 = 5) Example 2: select count(*) from my table where column1 is not null and column1 = 4 OR column1 = 5 In my database with the real column names, I get…
johnny
  • 19,272
  • 52
  • 157
  • 259
1
vote
0 answers

operator vs () parenthesis in JAVA

int a=5; a+=5+(++a)+(a++); Now the output should be 24 because first the parenthesis will be executed and if this is so then it will be a=a+5+(++a)+(a++)=>a= 7 + 5 + 6 + 6 but the output coming is 22, Why so?? I tried and searched but no where…
1
vote
4 answers

Operators Precedence in C

printf ("%d \n", 2 > !3 && 4 - 1 != 5 || 6 ) ; Can someone explain to me how this is evaluated ? What I am most confused about is the ! symbol in front of the 3... how to evaluate 2 > !3 ?
wadafarfar
  • 41
  • 2
1
vote
0 answers

Can impurity affect the associativity of an operation?

Associativity is a desirable property and quite common for many operations in FP. Now I was wondering if an impure function can interfere with it. The only example I found isn't really convincing, because I'm not sure if nullary functions count as…
1
vote
3 answers

Value of expression with pre-increment and post-decrement in Java

Consider the following expressions: int x = 5, y = 5, z; z = y + x * y-- + ++x; According to my calculations the value of z should be 40 considering ++ and -- have higher precedence than +. So z = 4 + 6 * 5 + 6 = 40. Running the code yields a…
sahanir
  • 63
  • 4
1
vote
2 answers

Operator associavity problem with pre and post increment :(

Possible Duplicate: Could anyone explain these undefined behaviors (i = i++ + ++i , i = i++, etc…) #include< stdio.h > int main() { int i = 1; int x = ++i * ++i * ++i; printf("%d\n", x); printf("%d\n\n",i); return 0; } Im…
siu
  • 312
  • 1
  • 5
  • 10
1
vote
2 answers

Java expressions calculation precedence: method invocation & array indexing

During the study of java expression calculation order I faced with one phenomenon I can't explain to myself clearly. There are two quiz questions. It is asked to define the console output. Example 1 int[] a = {5, 5}; int b = 1; a[b] = b =…
peremeykin
  • 519
  • 5
  • 11
1
vote
1 answer

Is "right to left operator Associativity" the same as the order of evaluation in assignment operator in javaScript

I need to know whether operator Associativity is the same as the order of evaluation of assignment operator and other operators in JavaScript for example var x; x = 10; In the above code I need to know whether the Assignment expression x = 10; …
1
vote
1 answer

Concatenation order and performance under Lazy Evaluation in Haskell

Consider the following two execution orders: a ++ (b ++ c) and (a ++ b) ++ c Why is the first execution order faster than the second? I'm new to Haskell, hoping for a detailed explanation, thanks!
1
vote
1 answer

Can I use associativity on AND and OR on the same time?

I don't think the question describes perfectly my problem. Consider the following: How can I simplify this? Does the AND take priority over the OR? Thanks in advance
Eye Patch
  • 881
  • 4
  • 11
  • 23