Is left-to-right associativity guaranteed for a mixture of multiplication and division?
Asked Answered
C

2

6

Please note, that this has nothing to do with Operator Precedence.. () and ++ , Undefined behavior and sequence points , Why are these constructs using pre and post-increment undefined behavior? and the hundreds similar questions about this here


Shortly: is the Associativity guaranteed by the standard?

Detailed example: from Wikipedia's article for operator precedence, operator* and operator/ have the same priority and they are Left-to-right operators. Does this mean, that the standard guarantees, that this:

int res = x / y * z / t;

will be evaluated as

int res = ( ( x / y ) * z ) / t;

or it's implementation defined?

If it's guaranteed, could you quote?


It's just out of curiosity, I always write brackets in these cases.
Ready to delete the question, if there's such one.

Craggie answered 17/8, 2012 at 12:16 Comment(1)
Yes (now need a few for characters!)Baxy
C
8

From the latest publicly available draft

5.6 Multiplicative operators [expr.mul]

1 The multiplicative operators *, /, and % group left-to-right.

multiplicative-expression:
pm-expression
multiplicative-expression * pm-expression
multiplicative-expression / pm-expression
multiplicative-expression % pm-expression

So parsing will go like:

int res = x / y * z / t;
int res = (x / y * z) / t;
int res = ((x / y) * z) / t;
Cellarer answered 17/8, 2012 at 12:20 Comment(12)
And not like int res = ( x / y ) * z / t; int res = ( ( x / y ) * z ) / t;? I have some troubles understanding that order. If so, why? Could you explain (and I'll accept the answer)Craggie
I see, that the result is the same, just asking :)Craggie
If you look at the parse tree: it's multiplicative-expression: multiplicative-expression @ pm-expression. When parsing your expression, the rightmost term t is isolated as the pm-expression, and the rest is then evaluated as another multiplicative-expression. The next step will identify z as the pm-experssion.Cellarer
what does pm-expression mean?Craggie
On how to read expresssion grammars, see also #6149205Cellarer
@KirilKirov pm-expression is a plus or minus expression. It makes sure that multiplication/division/modulo have higher precedence than addition/substractionCellarer
To explain this who have not been introduced to formal grammars: This text from the standard states that a multiplicative-expression (which I will abbreviate “ME”) must be formed of either a pm-expression (“PE”) or one of the following combinations: “ME * PE”, “ME / PE”, “ME % PE”. For our purposes, identifiers such as “x” and “y“ are already PEs. To form an ME from “x / y * z”, you must make the “x / y” into an ME, then use that ME to form “ME * z” into another ME. You cannot form “y * z” into an ME first, because that would give you “x / ME”, and since x is a PE, it would be “PE / ME”.…Exciter
Yes in your case. But in x / y * z / (u+v), the u+v would be a pm-expression.Cellarer
However, there is no rule in the grammar that says “PE / ME” can be an “ME” (or any other object in the grammar), so you cannot form the expression this way. Therefore, the grammar compels left-to-right association in these operators.Exciter
TO summarize: because we have ME @ PE, (PE to the right of @), any multiplicative @ evaluates left-to-right.Cellarer
@KirilKirov NP, nice question! (BTW, I always put parentheses everywhere just to avoid errors and confusion)Cellarer
@rhalbersma - yeah, me too (I wrote it in the question, too). I asked just out of curiosity :)Craggie
E
5

n3337 5.6/1

The multiplicative operators *, /, and % group left-to-right.

Read 5 par of standard.

Echikson answered 17/8, 2012 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.