C99关联的运营商 - 它在哪儿规定? [英] C99 associativity for operators - where is it specified?

查看:141
本文介绍了C99关联的运营商 - 它在哪儿规定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C99标准,前pressions允许precedence和结合。

In the C99 standard, the expressions allow for precedence and associativity.

由于该运营商在文档中出现的减少precedence,所以函数调用乘法运算符,反过来,来加法运算符之前来之前的顺序precedence被记录得非常好。

Precedence is documented quite well since the order in which the operators appear in the document are of reducing precedence, so function calls come before multiplicative operators which, in turn, come before additive operators.

不过,我找不到关联的一个明确的描述,无论是左边或右边。这一点很重要,因为 35/5 * 2 14 的一个变体(35 / 5)* 2 3 为其他变量 35 /(5 * 2)

However, I can't find a definitive description of the associativity, whether it's left or right. This is important since 35/5*2 would be 14 for one variant (35/5)*2 and 3 for the other variant 35/(5*2).

6.5防爆pressions / 3,脚注74 状态:

的语法指定一个前pression的评价,这是一样的本节的主要小节,最高precedence第一。

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.

在各主要小节,经营者具有相同的precedence。向左或向右关联,在每个小节的语法其中讨论的前pressions表示。

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.

但是,考虑的乘法的情况下,例如:

However, taking the multiplicative case, for example:

6.5.5乘法运算符结果
    的语法的结果
       乘-EX pression:结果
         投-EX pression 结果
         乘-EX pression *投-EX pression 结果
         乘-EX pression /施放-EX pression 结果
         乘-EX pression%施法-EX pression 搜索结果
     限制的搜索结果
  每个操作数应具有算术类型。在运算符的操作数应
  有整型。搜索结果
    的语义的搜索结果
  通常的算术转换的操作数执行。搜索结果
  二进制 * 运算符的结果是操作数的乘积。搜索结果
  在 / 运算符的结果是第一个操作数由相除所得的商
  第二;在的结果就是运营商的剩余部分。在两个操作中,如果该值
  第二个操作数是零,则行为是不确定的。搜索结果
  当整数划分,在 / 运算符的结果是代数商与任何
  小数部分丢弃。如果商 A / B 重新presentable,前pression (A / B)* B + A%B 应等于 A 搜索结果

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.

我可以看到没有在那里提到的相关性,也不存在似乎是任何默认标准设置其他

I can see nothing in there that mentions the associativity, nor does there seem to be any default setting elsewhere in the standard.

我失去了一些东西在这里?

Am I missing something here?

推荐答案

在没有明确指定运营商的关联性为右结合或左联。您可以从语法推断出这一点。在您的例子中,乘-EX pression 期指本身的递归和递归是对操作者的左侧。这意味着,解析器遇到 A * B * C 必须解析 A * B * C (A * b)* C ,这是左结合。

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.

分配-EX pression 项(6.5.16)有这样的语法:

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

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

因此​​,一个解析器遇到 A = B = C 必须分析它像 A =(B = C) ,这是右结合。

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

这篇关于C99关联的运营商 - 它在哪儿规定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆