C99 associativity for operators - where is it specified?

前端 未结 2 1822
轮回少年
轮回少年 2021-01-18 01:29

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

相关标签:
2条回答
  • 2021-01-18 01:50

    The grammar itself specifies associativity, by the productions used:

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

    This means that in a * b * c, c must be parsed as a cast-expression, and a * b as one multiplicative-expression, before further parsing of a * b itself. Thus the left-associativity of multiplication is forced into the syntax-tree by the parsing rules.

    0 讨论(0)
  • 2021-01-18 02:03

    Operator associativity isn't specified explicitly as “right-associative” or “left-associative”. You deduce it from the grammar. In your example, the multiplicative-expression term refers to itself recursively, and the recursion is on the left-hand side of the operator. That means that a parser encountering a * b * c must parse a * b * c like (a * b) * c, which is left-associative.

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

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

    So a parser encountering a = b = c has to parse it like a = (b = c), which is right-associative.

    0 讨论(0)
提交回复
热议问题