C99 associativity for operators - where is it indicated?

In the C99 standard, expressions allow priority and associativity.

The priority is well-documented, since the order in which the operators appear in the document has a reduction in priority, so function calls come in front of multiplicative operators, which in turn come in front of additive operators.

However, I cannot find a definitive description of associativity, be it left or right. This is important because (35/5)*2 35/5*2 will be 14 for one option (35/5)*2 and 3 for another option 35/(5*2) .

Section 6.5 Expressions /3, footnote 74 state:

The syntax determines the priority of operators when evaluating an expression, which coincides with the order of the main subparagraphs of this subparagraph, with the highest priority.

In each main subquery, operators have the same priority. Left or right associativity is indicated in each subclause by the syntax of the expressions discussed in it.

However, taking a multiplicative case, for example:

6.5.5 Multiplicative Operators
Syntax
multiplicative-expression:
cast-expression
multiplicative-expression * cast-expression
multiplicative-expression / cast-expression
multiplicative-expression % cast-expression

Obstacles

Each of the operands must have an arithmetic type. The operands of the % operator must be of integer type. Semantics

Normal arithmetic conversions are performed on operands. The result of the binary operator * is the product of the operands.

The result of the operator / is the factor of dividing the first operand by the second; the result of the % operator is the remainder. In both operations, if the value of the second operand is zero, the behavior is undefined.

When integers are divisible, the result of the / operator is an algebraic relation with any fractional part being discarded. If the expression a/b is representable, the expression (a/b)*b + a%b will be equal to a .

I don't see anything where associativity is mentioned, and there seems to be no default setting elsewhere in the standard.

Did I miss something?

+8
c iso associativity
source share
2 answers

Associative associativity is not explicitly indicated as "right associative" or "left associative." You derive it from the grammar. In your example, the term multiplicative-expression refers to itself recursively, and recursion is on the left side of the statement. This means that the parser encountering a * b * c must parse a * b * c as (a * b) * c , which is left-associative.

The term assignment-expression (6.5.16) has this grammar:

 assignment-expression: conditional-expression unary-expression assignment-operator assignment-expression 

Thus, a parser that encounters a = b = c must parse it as a = (b = c) , which is right-associative.

+9
source share

The grammar itself indicates the associativity of the works used:

 multiplicative-expression: cast-expression multiplicative-expression * cast-expression 

This means that in a * b * c , c you need to parse as cast-expression and a * b as one multiplicative-expression , before further analyzing a * b . Thus, the left-associativity of multiplication is forcibly introduced into the syntax tree by the rules of parsing.

+3
source share

All Articles