?:和赋值的相对优先级 [英] relative precedence of ?: and assignment

查看:72
本文介绍了?:和赋值的相对优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人向我指出,各种C ++书籍不同意

相对优先级?:和赋值运算符。


为了一劳永逸地满足自己的需求,

我查看了C ++标准中的语法。相对片段

如下:


条件表达式:

logical-or-expression

logical-or-expression?表达式:赋值表达式


赋值表达式:

条件表达式

逻辑 - 或 - 表达式赋值 - 运算符赋值 - 表达

throw-expression


形式,如果它完全遵守,会说

条件表达式的优先级高于

赋值表达式。


但是,与这种形式有一个重要区别:条件表达式第二行中最右边的

标记。是

" assignment-expression"而不是条件表达式。

这种差异使得语法在

优先条款中难以理解。


但是,如果我们采用赋值表达式的定义并且

用其替代方法替换条件表达式的使用,我们得到:


赋值表达式:

logical-or-expression

logical-or-expression?表达式:赋值表达式

逻辑 - 或 - 表达式赋值 - 运算符赋值 - 表达式

throw-expression


我们可以用这种方式重写:


cond-or-asn-op:

赋值运算符

?表达式:


赋值表达式:

logical-or-expression

logical-or-expression cond-or-asn- op assignment-expression

throw-expression


从这次重写中,应该清楚分配和?:

运算符具有相同的优先权,并且它们是正确关联的。


我错过了什么吗?如果没有,我想敦促所有作者和

C ++教师描述这些运算符的优先级

方式;我觉得它比任何我想见的更好的东西都要容易理解。

-

Andrew Koenig, ar*@acm.org

It has been pointed out to me that various C++ books disagree about
the relative precedence of ?: and the assignment operators.

In order to satisfy myself about the matter once and for all,
I looked at the grammar in the C++ standard. The relative fragments
are as follows:

conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression

assignment-expression:
conditional-expression
logical-or-expression assignment-operator assignment-expression
throw-expression

Ordinarily, this grammar is almost, but not quite, in a well-known
form that, were it adhered to exactly, would say that
conditional-expressions have higher precedence than
assignment-expressions.

However, there is one crucial difference from this form: The rightmost
token in the second line of "conditional-expression" is
"assignment-expression" rather than "conditional-expression".
This difference makes the grammar somewhat harder to understand in
terms of precedence.

However, if we take the definition of assignment-expression and
replace the use of conditional-expression by its alternatives, we get:

assignment-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
logical-or-expression assignment-operator assignment-expression
throw-expression

which we can rewrite this way:

cond-or-asn-op:
assignment-operator
? expression :

assignment-expression:
logical-or-expression
logical-or-expression cond-or-asn-op assignment-expression
throw-expression

From this rewrite, it should be clear that the assignment and ?:
operators have the same precedence, and they are right-associative.

Am I missing anything? If not, I''d like to urge all authors and
teachers of C++ to describe the precedence of these operators this
way; I think it''s much easier to understand than any alternatives I''ve
seen.
--
Andrew Koenig, ar*@acm.org

推荐答案



Andrew Koenig写道:< about" teaching">

[...]

Andrew Koenig wrote: <about "teaching">
[...]
我错过了什么?




嗯,


"语法指定运算符在表达式中的优先级

表达式,与本子条款的主要

子条款的顺序,首先是最高优先级。因此,对于

示例,允许作为二进制+

运算符(6.5.6)的操作数的表达式是6.5.1中通过
定义的表达式
6.5.6。例外情况是强制转换表达式(6.5.4)作为

一元运算符(6.5.3)的操作数,以及

中任何一对运算符之间包含的操作数:分组括号()(6.5.1),

下标括号[](6.5.2.1),函数调用括号()

(6.5.2.2),以及条件运算符?:( 6.5.15)。"


问候,

亚历山大。


-

好工作SUN - 你已经弄脏了

新贱民的臭味。在这种情况下你的敌人的敌人不是你的朋友。我希望IBM购买你的遗憾资产,因为我们已经完成了你的b $ b。"

- " teambpsi" @ slashdot



Uhmm,

"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. Thus, for
example, the expressions allowed as the operands of the binary +
operator (6.5.6) are those expressions defined in 6.5.1 through
6.5.6. The exceptions are cast expressions (6.5.4) as operands of
unary operators (6.5.3), and an operand contained between any of
the following pairs of operators: grouping parentheses () (6.5.1),
subscripting brackets [] (6.5.2.1), function-call parentheses ()
(6.5.2.2), and the conditional operator ?: (6.5.15)."

regards,
alexander.

--
"Good job SUN -- you''ve soiled yourselves with the stink of the
new pariah. The enemy of your enemy in this case was not your
friend. I hope IBM buys your sorry assets out, because we''re
done with you."
-- "teambpsi" @ slashdot


Alexander> 语法指定运算符在

Alexander>中的优先级。评价表达式,与订单相同

Alexander>本条款的主要子条款中,最高的

亚历山大>先优先。因此,例如,表达式

Alexander>允许作为二元+运算符的操作数(6.5.6)

Alexander>是6.5.1到6.5.6中定义的那些表达式。

亚历山大>例外情况是演员表达式(6.5.4)作为操作数

Alexander>一元运算符(6.5.3),以及

Alexander>之间包含的操作数。以下任何一对运算符:分组

Alexander>括号()(6.5.1),下标括号[](6.5.2.1),

亚历山大> function-call括号()(6.5.2.2)和条件

Alexander> operator?:( 6.5.15)。"


无论描述性材料可能会说什么,它都不会对语法添加任何额外的约束

。换句话说,

如果语法要求以特定的

方式解析表达式,那么这就是它被解析的方式。
< br $> b $ b -

Andrew Koenig, ar*@acm.org
Alexander> "The syntax specifies the precedence of operators in the
Alexander> evaluation of an expression, which is the same as the order
Alexander> of the major subclauses of this subclause, highest
Alexander> precedence first. Thus, for example, the expressions
Alexander> allowed as the operands of the binary + operator (6.5.6)
Alexander> are those expressions defined in 6.5.1 through 6.5.6. The
Alexander> exceptions are cast expressions (6.5.4) as operands of
Alexander> unary operators (6.5.3), and an operand contained between
Alexander> any of the following pairs of operators: grouping
Alexander> parentheses () (6.5.1), subscripting brackets [] (6.5.2.1),
Alexander> function-call parentheses () (6.5.2.2), and the conditional
Alexander> operator ?: (6.5.15)."

Regardless of what the descriptive material may say, it does not add
any additional constraints to the grammar. Putting it differently,
if the grammar requires an expression to be parsed in a particular
way, then that is the way it''s parsed.

--
Andrew Koenig, ar*@acm.org




Andrew Koenig写道:

Andrew Koenig wrote:

Alexander> 语法指定了
Alexander>中运算符的优先级。表达式的评估,与订单相同
Alexander>本条款的主要子条款,最高的亚历山大>先优先。因此,例如,表达式
Alexander>允许作为二元+运算符的操作数(6.5.6)
Alexander>是6.5.1到6.5.6中定义的那些表达式。
Alexander>例外情况是演员表达式(6.5.4)作为亚历山大>的操作数。一元运算符(6.5.3),以及
Alexander>之间包含的操作数。以下任何一对运算符:分组
Alexander>括号()(6.5.1),下标括号[](6.5.2.1),
亚历山大> function-call括号()(6.5.2.2),以及条件
Alexander> operator?:( 6.5.15)。

无论描述性材料可能会说什么,它都不会对语法添加任何额外的约束。换句话说,
如果语法需要以特定的方式解析表达式,那么这就是它被解析的方式。

Alexander> "The syntax specifies the precedence of operators in the
Alexander> evaluation of an expression, which is the same as the order
Alexander> of the major subclauses of this subclause, highest
Alexander> precedence first. Thus, for example, the expressions
Alexander> allowed as the operands of the binary + operator (6.5.6)
Alexander> are those expressions defined in 6.5.1 through 6.5.6. The
Alexander> exceptions are cast expressions (6.5.4) as operands of
Alexander> unary operators (6.5.3), and an operand contained between
Alexander> any of the following pairs of operators: grouping
Alexander> parentheses () (6.5.1), subscripting brackets [] (6.5.2.1),
Alexander> function-call parentheses () (6.5.2.2), and the conditional
Alexander> operator ?: (6.5.15)."

Regardless of what the descriptive material may say, it does not add
any additional constraints to the grammar. Putting it differently,
if the grammar requires an expression to be parsed in a particular
way, then that is the way it''s parsed.




你可能想看看* C * grammer。或者我只是缺少

和/或误解了什么?


问候,

亚历山大。

-

自由是必然的。言论自由并不是说无论你想要什么,都要说b $ b。越早意识到这一点,你的生活就会越好。

- Victor Bazarov



You might want to take a look at *C* grammer. Or am I just missing
and/or misunderstanding something?

regards,
alexander.

--
"Freedom is a perceived necessity. Freedom of speech is NOT saying
whatever you want. The sooner as you realize that, the better your
life is going to be."
-- Victor Bazarov


这篇关于?:和赋值的相对优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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