gpt4 book ai didi

运算符的 C99 关联性 - 它在哪里指定?

转载 作者:太空狗 更新时间:2023-10-29 17:01:15 27 4
gpt4 key购买 nike

在 C99 标准中,表达式允许优先级和关联性。

优先级被很好地记录下来,因为文档中运算符出现的顺序是降低优先级的,所以函数调用在乘法运算符之前,而乘法运算符又在加法运算符之前。

但是,我找不到关于结合律的明确描述,无论是左结合律还是右结合律。这很重要,因为 35/5*2 对于一个变体 (35/5)*23 将是 14 code> 用于另一个变体 35/(5*2)

6.5 表达式/3,脚注 74 状态:

The syntax specifies the precedence of operators in the evaluation of an expression, which is the same as the order of the major subclauses of this subclause, highest precedence first.

Within each major subclause, the operators have the same precedence. Left- or right-associativity is indicated in each subclause by the syntax for the expressions discussed therein.

但是,以乘法为例:

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

  Constraints

Each of the operands shall have arithmetic type. The operands of the % operator shall have integer type.

  Semantics

The usual arithmetic conversions are performed on the operands.

The result of the binary * operator is the product of the operands.

The result of the / operator is the quotient from the division of 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 divided, the result of the / operator is the algebraic quotient with any fractional part discarded. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.

我在那里看不到任何提及关联性的内容,标准中的其他地方似乎也没有任何默认设置。

我是不是漏掉了什么?

最佳答案

运算符结合性未明确指定为“右结合”或“左结合”。你从语法中推断出来。在您的示例中,multiplicative-expression 术语递归地引用自身,并且递归位于运算符的左侧。这意味着遇到 a * b * c 的解析器必须像 (a * b) * c 一样解析 a * b * c,这是左结合。

assignment-expression 术语 (6.5.16) 具有以下语法:

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

因此遇到 a = b = c 的解析器必须像 a = (b = c) 那样解析它,这是右结合的。

关于运算符的 C99 关联性 - 它在哪里指定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9408127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com