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

How to implement left associativity if there is a method (instead of a variable) after the operator sign?

Lately I've been getting back to coding (haven't done it in a while). I am trying to write a program which will save and perform operations on sets in left associative order instead of right associative order. SetInterface
user3707951
  • 29
  • 2
  • 8
1
vote
1 answer

BNF grammar associativity

I'm trying to understand how left and right associative grammars work and I need a little help. So I decided to come up an example and ask for some clarification. Basically, I want to create a grammar for two logical operations: and + implication. I…
Sreten Jocić
  • 165
  • 1
  • 14
1
vote
1 answer

Reordering parentheses using associative property in Racket

I am having trouble implementing a function that given something like this: '((+(d + e) + f) + (a + (a + c)) Returns this: '(d + (e + (f + (a + (a + c))))) Now the catch is that I have to use associativity to get the answer so I only want to use…
Will
  • 143
  • 2
  • 13
1
vote
1 answer

Operator overloading associativity compiler optimization

I'm not familiar with the details of how the addition and multiplication operators are defined in C++, and I also don't know what information is or is not available to the compiler to optimize out inefficient orders of operation. Let's say we have a…
1
vote
1 answer

What in the implementation needs to change in order for a left associative operator to become right associative?

I'm reading up on Haskell and as expected there are some operators that are left associative and others that are right associative. That got me thinking, how would the implementation differ for a new operator ¤, when comparing the left associative…
Patrik Iselind
  • 187
  • 1
  • 11
1
vote
1 answer

How to interpret the operator associativity?

Parentheses and pointer symbol have same priority, and they are dealed from left to right. Why does the following code try to get the member nfct from skb, then do the type conversion? It's seems that the associativity is from right to left. (struct…
river
  • 694
  • 6
  • 22
1
vote
1 answer

Associativity between type cast and . in Visual Studio

Following is the code in question. void SomeClass::Init( const vector& args ) { int argc = (int)args.size(); //... } I work in Visual Studio 2015 Update 3 on Windows 7 64-bit. My question is: according to my understanding of…
1
vote
3 answers

C++, conditional operator associativity

in this code : finalgrade = (grade > 90) ? "high pass" : (grade < 60) ? "fail" : "pass"; A book says the the ?: operator is right associative. I did an internet search so I understand what associativity means. But I can't really understand what it…
gigi
  • 669
  • 6
  • 11
1
vote
1 answer

BNF grammar and Operator Associativity

(First of all this is not HW, I have all the answers) I have a simple BNF grammar ::= ( ) | a | b | c ::= not | ::= and | ::= | or and operator…
newprint
  • 6,936
  • 13
  • 67
  • 109
1
vote
1 answer

what is the output and explain how?

I am confused about the output of following 2 programs. Could someone explain the precedence and associativity rules? Program 1: char arr[] = "geeksforgeeks"; char *p = arr; *p++; printf(" %c", *p); Program 2: char arr[] = "geeksforgeeks"; char…
1
vote
1 answer

Operator precedence with LR(0) parser

A typical BNF defining arithmetic operations: E :- E + T | T T :- T * F | F F :- ( E ) | number Is there any way to re-write this grammar so it could be implemented with an LR(0) parser, while still retaining the precedence and…
Fredrik
  • 97
  • 7
1
vote
1 answer

Operator precedence for custom types and is it possible to have lazy evaluation when multiples of the same operator are present?

Suppose array is a custom type defined as a class that essentially manages a resource class array { public: size_t size; float *data; }; To make the operator - to perform element-wise scalar addition to array and be able to handle both left…
user5262733
1
vote
4 answers

operation on post++ and --pre operator

I am wondering with post and pre increment and decrement operation. what I know in Java precedence of post operator is high and associativity is left-to-right.while associativity of pre operator is right-to-left Oracle Java Tutorial but my code…
user4768611
1
vote
1 answer

associativity of operations regarding floating points

I am trying to understand tthe associativity of operations when it comes to floating points. In the lecture notes i have, the following is stated: "suppose floating-point values store seven digit of accuracy. Considee the problem of adding 11…
1
vote
2 answers

Why does a unary operator have associativity?

In a expression like "10 - 3 - 2", it's easy to understand why - and + operators are left associative. To match mathematical convention and have 5 instead of 9 as the result. As I understood it, associativity means the order when some operators have…