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
15
votes
4 answers

Does it make sense for unary operators to be associative?

The C++ operator precedence table from http://en.cppreference.com/w/cpp/language/operator_precedence (I know it's not normative, but the standard doesn't talk about precedence or associativity) marks unary operators as right/left associative. From…
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
14
votes
2 answers

Why does x = x * y / z give a different result from x *= y / z for integers?

I have the following function: pub fn s_v1(n: &u64) -> u64 { let mut x: u64 = 1; for i in 1..=*n { x = x * (*n + i) / i; } x } This code gives the correct answer for s_v1(&20) == 137846528820 However, if I change the line…
Aditeya
  • 303
  • 2
  • 10
14
votes
1 answer

Why is Perl 6's right associativity not right?

Clickbaity title but it's too meaty to pass up. I have this operator which I want to be right associative: sub infix:<↑> ( Int:D \n, Int:D \m --> Int:D ) is assoc is equiv(&infix:<**>) { n ** m } put "2**2**2**2 = ", …
brian d foy
  • 129,424
  • 31
  • 207
  • 592
13
votes
2 answers

Associativity of fold-expressions

N4191 proposed fold-expressions to C++. The definition there was that (args + ...) is a left-fold (i.e. (((a0 + a1) + a2) + ...), and that (... + args) is a right-fold (i.e. (... + (a8 + (a9 + a10))). However, the revised paper N4295 reversed…
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
13
votes
1 answer

Expression parser grammar and left-associativity

I have been trying create my parser for expression with variables and simplify them to quadratic expression form. This is my parser grammar: Exercise : Expr '=' Expr Expr : Term [+-] Expr | Term Term : Factor [*/] Term | Factor Factor: Atom [^]…
11
votes
2 answers

Left-associative operators vs Right-associative operators

If we have an expression: a $ b @ c $ is a left-associative operator, @ is right-associative. They have the same precedence. How is this expression parsed? As (a $ b) @ c or as a $ (b @ c)?
Robert Bean
  • 851
  • 2
  • 11
  • 21
11
votes
2 answers

Does the comma operator have to be left-associative?

According to this precedence table, the comma operator is left-associative. That is, a, b, c is parsed as (a, b), c. Is that a necessity? Wouldn't a, (b, c) have the exact same behavior?
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
9
votes
2 answers

C99 associativity for operators - where is it specified?

In the C99 standard, the expressions allow for precedence and associativity. Precedence is documented quite well since the order in which the operators appear in the document are of reducing precedence, so function calls come before multiplicative…
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
9
votes
1 answer

Monad's associativity rule in haskell

(m >>= f) >>= g = m >>= (\x -> f x >>= g) what's different from f and \x->f x ?? I think they're the same type a -> m b. but it seems that the second >>= at right side of equation treats the type of \x->f x as m b. what's going wrong?
user1065942
9
votes
1 answer

Why is <$> left associative?

fmap is also <$> because it is function application ($) in the functor category. (+5) $ (*10) $ 10 -- 105 (+5) <$> (*10) <$> [1,2,3] -- [15,25,35] Then I thought, well in that case <*> is function application in the applicative functor…
hgiesel
  • 5,430
  • 2
  • 29
  • 56
9
votes
4 answers

Why is the integer converted to string in this case?

What is happening below? using System; using System.Collections.Generic; using System.Linq; using System.Text; public class DotNetPad { public static void Main(string[] args) { int i = 10; string k = "Test"; …
deostroll
  • 11,661
  • 21
  • 90
  • 161
8
votes
2 answers

Automatically and deterministicly testing a function for associativity, commutativity etc

Is it possible to construct a higher order function isAssociative that takes another function of two arguments and determines whether that function is associative? Similar question but for other properties such as commutativity as well. If this is…
8
votes
2 answers

*p++->str : Understanding evaluation of ->

My question is about the following line of code, taken from "The C Programming Language" 2nd Edition: *p++->str; The book says that this line of code increments p after accessing whatever str points to. My understanding is as follows: Precedence…
thatmarkdude
  • 233
  • 1
  • 6
8
votes
2 answers

Recursive expressions with pyparsing

I'm trying to figure out how to do a left-associative expression where recursive (not-enclosed in anything) expressions are possible. For example, I'd like to do: expr + OP + expr that parses 2 operations like 1 x 2 x 3 into (expr OP expr) OP expr…
viraptor
  • 33,322
  • 10
  • 107
  • 191
8
votes
1 answer

Why isn't `"repeat" * 3` the same as `3 * "repeat"` in Ruby?

When I type this: puts 'repeat' * 3 I get: >> repeat repeat repeat But it's not working if I do this: puts 3 * 'repeat' Why?
levirg
  • 1,033
  • 2
  • 8
  • 9
1
2
3
12 13