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

C++ : How is this statement parsed?

I have been trying to learn the associativity of operators in C++ and I have come across a code segment : int a = 10; int C = a++ + ++a + ++a +a; I have also studied that ++a is right to left associative and a++ is left to right associative. Also…
utkarsh867
  • 87
  • 1
  • 10
0
votes
2 answers

LL grammar with associativity and self referring productions

I am trying to write a parser grammar and currently have the following productions for an LL Grammar (in Antlr) and I am trying to parse one or more (numbers or strings) that is separated by a "#" that is right associative. How do I modify the the…
rlhh
  • 893
  • 3
  • 17
  • 32
0
votes
1 answer

How to use right associative op in the expression?

I've defined my own operator: infixr 6 >+ x >+ y = (+ x) y It is right associative. And now I want to use in the next expression: (`mod` 14) (>+ 5) 10 But I get an error: :11:1: Non type-variable argument in the constraint:…
Finkelson
  • 2,921
  • 4
  • 31
  • 49
0
votes
2 answers

In which precedence is this statement evaluated?

++*P--; That is a question from an exam, if P a pointer to any element in an array, explain what this statement really does. I even wrote a simple code to evaluate it: int i; int* array = calloc(10, sizeof(int)); for (i = 0; i < 10; i++)…
Anatoly
  • 5,056
  • 9
  • 62
  • 136
0
votes
1 answer

The evaluation process of a compound expression containing two assignment operators in JavaScript

Here is a compound expression containing two assignment operators: var a = {n: 1}; var b = a; a.x = a = {m: 2}; a; // => {m: 2} b; // => {n: 1, x: {m: 2}} The tricky part is the third line: a.x = a = {m: 2}; IMHO, The assignment operator =…
0
votes
1 answer

Backus Naur Form Assoicativity

Is this the correct way to implement right associativity for Exponentiation PowExp? So that 2^3^4 is actually (2^(3^4)) ::= + | - | ::= * | / |…
TemporaryFix
  • 2,008
  • 3
  • 30
  • 54
0
votes
2 answers

Solving equations with an associative and commutative operator

Consider a goal like this in Isabelle (and don’t worry about ccProd and ccFromList): ccProd {x} (set xs) ⊔ (ccProd {x} (set ys) ⊔ (ccFromList xs ⊔ (ccFromList ys ⊔ ccProd (set xs) (set ys)))) = ccProd {x} (set xs) ⊔ (ccFromList xs ⊔ (ccFromList ys ⊔…
Joachim Breitner
  • 25,395
  • 6
  • 78
  • 139
0
votes
0 answers

Operator associativity and order of evaluation

I've read from here and there, from stackoverflow and from other websites that expression evaluation is dependent from operator precedence and associativity of the operators. The problem is that I've read the line from a certain famous website:…
emilxp
  • 91
  • 1
  • 1
  • 3
0
votes
2 answers

Javascript - style.left will update for subtraction but not addition

function moveLeft(obj){ obj.style.left = parseInt(obj.style.left) - 0.5 + "%"; } function moveRight(obj){ obj.style.left = parseInt(obj.style.left) + 0.5 + "%"; } The code here is supposed to move my image object to the left and to the…
keycoder
  • 13
  • 1
0
votes
3 answers

Haskell Beginner: Currying/List Associativity

From Learn You a Haskell: Think about this list: [5]. That’s just syntactic sugar for 5:[]. On the left side of the :, there’s a value; on the right side, there’s a list. In this case, it’s an empty list. Now how about the list [4,5]? Well,…
-1
votes
1 answer

Why are the precedence and associativity rules different in C and Java?

I found that the precedence and associativity rules are different in C, C++ and Java. Have a look at this code snippet: #include void main(){ int k = 5; int x = ++k*k--*4; printf("%d",x); } The above C program gives the output…
-1
votes
1 answer

Checking If I understood this concept right (associativity)

I have the following expression in C. I'm asked whether we can predict the value of z int x,y,z; z= (x=2) + (y=x) I know that the () operator has left associativity. Does this mean that the left parenthesis will be evaluated before the ones on the…
-1
votes
2 answers

Associativity of operators in C

From the book which I have read: Associativity can be of two types: 1-Left to Right Left to Right associativity means that the left operand must be unambiguous.Unambiguous in what sense? It must not be involved in evaluation of any other…
user6751339
-1
votes
2 answers

a>b>c Evaluation When Each Has Numeric Values

I came upon a competitive C question which goes as follows: Find the output of: #include int main(void) { int a=5, b=10, c=5; int x; x = a>b>c; printf("%d\n", x); return 0; } The compiler responds 0. What my…
Jishan
  • 1,654
  • 4
  • 28
  • 62
-1
votes
1 answer

Converting infix to post fix expression, is the associativity always left to right?

If yes then why is it so? Isnt right associativity valid for postfix expression?
user3202188
  • 131
  • 1
  • 7
1 2 3
12
13