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
1
vote
2 answers

Why do string additions give different answers?

System.out.println(7 + 5 + " "); This prints out 12, but in another order System.out.println(" " + 5 + 7); it prints out 57. Why is this?
zeds
  • 137
  • 1
  • 10
1
vote
3 answers

Ambiguity about ++ operator Associativity

"C How to Program 6th edition - page 119" says that the associativity of ++ operator is "right to left". I wonder what does this mean, because ++ is a unary operator so in what case I may experience the associativity of a unary operator? Can you…
CPX
  • 33
  • 1
  • 4
1
vote
1 answer

Associativity and commutativity of inner joins sql

Let us say I have this schema. Boats _____ bid bname Reserves ________ sid bid date Sailors _______ sid sname I know that inner joins are supposed to be both associative and commutative, but I cannot really understand why. Given the query: SELECT…
user3903214
  • 203
  • 1
  • 9
1
vote
1 answer

Does Z3 have a feature for facilitating the matching of subformulas in chains of associative / commutative operators?

Let's say I have a user-defined commutative and associative operator op. The code below is invalid because I'm using op with more than two arguments. Let's suppose for a moment that it is valid and that it means "the way op is applied is…
simon1505475
  • 675
  • 3
  • 9
1
vote
3 answers

unbound variables in monad associativity law

Using ghci I have computed: Prelude> let m = [1,2] Prelude> let ys = [4, 5, 6] Prelude> m >>= (\x -> ys >>= (\y -> return (x, y))) [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6)] The monadic expression above doesn't seem to correspond to either side of the…
Leo D
  • 209
  • 2
  • 5
1
vote
3 answers

Fix expression; operator precedence

I am reading C++ Primer, 5th Edition to learn C++ however I have come across a question that I am kind of stuck at. The question is as follows: The following expression fails to compute due to operator precedence. How would you fix it? string s =…
liamjnorman
  • 784
  • 1
  • 16
  • 30
1
vote
3 answers

Is a parenthesis in an expression evaluated before anything else?

In C, the order of evaluation of operands has nothing to do with operator precedence and associativity. Suppose I have an expression in C: expr1 * expr2 + (expr3 + expr4) (no sequence points in between). When this expression is evaluated…
1
vote
2 answers

Precedence and associativity of operators in C

Please have a look at following code snippet: int a = 10, b; b = (a) + (++a); //2 printf("b = %d\n", b); Output: b = 22 In statement 2, there are 4 distinct operators. Out of which () has highest precedence. Since…
rootkea
  • 1,474
  • 2
  • 12
  • 32
1
vote
3 answers

Are C/C++ operator precedence & associativity rules ever violated?

Are operator precedence & associativity rules ever violated in any C/C++ expression? If so, can you give an example? Assume the claims of precedence and associativity rules are: Each operator has a given precedence level, and each precedence level…
Museful
  • 6,711
  • 5
  • 42
  • 68
1
vote
3 answers

Please explain the associativity of the operators in this code

#include int main(void) { char s[] = {'a','b','c','\n','c','\0'}; char *p; p=&s[3]; printf("%d\t",++*p++); printf("%d",*p); return 0; } output: 11 99 Please explain the output. Why there is an increment in the address?
Ashwani R
  • 47
  • 2
  • 6
1
vote
1 answer

Postfix and right-associative operators in LR(0) parsers

Is it possible to construct an LR(0) parser that could parse a language with both prefix and postfix operators? For example, if I had a grammar with the + (addition) and ! (factorial) operators with the usual precedence then 1+3! should be 1 + 3! =…
Ian
  • 19
  • 2
0
votes
3 answers

Associativity and Sequence Points in C

Since the associativity of '?' is from right to left,any 2 consecutive '?' operators must be treated as such,Right? Now, int x=-1; int y=x?x++?x:-1:1; I expect this to be executed as: int y = x ? (x++?x:-1) : 1; Now since its being executed from…
nikel
  • 3,402
  • 11
  • 45
  • 71
0
votes
3 answers

Associativity and Precedence in C

i) What does if(0) mean? Everytime I use it to test what output i will get, it returns the false part. Is it equivalent to if(0 == 0), incase of which the true part is evaluated. ii) Associativity of logical NOT ! is right to left. Link:…
user980411
  • 1,179
  • 3
  • 12
  • 18
0
votes
1 answer

Is there any difference between these two?

I donno if this is possible or not but am confused. Are they both same? I know that in the first case we are allocating memory dynamically for 20 elements of type int. int *p; p=(int *) malloc(20*sizeof(int)); and p=(int *) malloc(sizeof(int)*20);
blogger10
  • 23
  • 5
0
votes
0 answers

Associativity of an if expression in Python

In Python language, which is the associativity of an if expression: left or right? For example if we write this: if else if else what is the corresponding form? [ if else ] if else…
Duy Duy
  • 521
  • 1
  • 4
  • 9