4

Please note, that this has nothing to do with Operator Precedence.. () and ++ , Undefined behavior and sequence points , Why are these constructs (using ++) 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.

Community
  • 1
  • 1
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187

2 Answers2

6

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;
Community
  • 1
  • 1
TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • 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) – Kiril Kirov Aug 17 '12 at 12:33
  • I see, that the result is the same, just asking :) – Kiril Kirov Aug 17 '12 at 12:33
  • 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`. – TemplateRex Aug 17 '12 at 12:35
  • what does `pm-expression` mean? – Kiril Kirov Aug 17 '12 at 12:42
  • On how to read expresssion grammars, see also http://stackoverflow.com/questions/6148705/relation-between-grammar-and-operator-associativity – TemplateRex Aug 17 '12 at 12:42
  • @KirilKirov `pm-expression` is a plus or minus expression. It makes sure that multiplication/division/modulo have higher precedence than addition/substraction – TemplateRex Aug 17 '12 at 12:43
  • 2
    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”.… – Eric Postpischil Aug 17 '12 at 12:45
  • Yes in your case. But in `x / y * z / (u+v)`, the `u+v` would be a `pm-expression`. – TemplateRex Aug 17 '12 at 12:45
  • 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. – Eric Postpischil Aug 17 '12 at 12:46
  • TO summarize: because we have `ME @ PE`, (PE to the right of @), any multiplicative @ evaluates left-to-right. – TemplateRex Aug 17 '12 at 12:48
  • @KirilKirov NP, nice question! (BTW, I always put parentheses everywhere just to avoid errors and confusion) – TemplateRex Aug 17 '12 at 12:54
  • @rhalbersma - yeah, me too (I wrote it in the question, too). I asked just out of curiosity :) – Kiril Kirov Aug 17 '12 at 12:59
5

n3337 5.6/1

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

Read 5 par of standard.

ForEveR
  • 55,233
  • 2
  • 119
  • 133