表达式和评估顺序和优先级规则 [英] expression and evaluation order and precedence rules

查看:109
本文介绍了表达式和评估顺序和优先级规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




i对表达式中的表达式评估顺序有点困惑

涉及一元increment.decrement运算符和二进制

运营商。


例如,在下面的表达式中


x + = i + j + k ++;


是以下评估顺序


1)因为++具有最高的先行子表达式k ++得到评估

2)i + j被评估然后说结果存储在一个临时的

位置说temp

3)下一个temp + k ++得到评估并且结果存储在临时

位置说temp1

4)接下来x + temp1被评估,结果被分配回x。


所以评估顺序正确或是我错过了

这里有什么东西?

假设给出一个表达式全部一元是否正确

oerators被评估,然后评估二元运算符

th anks,

silpa



i am a bit confused on expression evaluation order in expressions
involving unary increment.decrement operators along with binary
operators.

For example in the following expression

x += i + j + k++;

is the following the sequence of evaluations

1) as ++ has highest precedencce subexpression k++ gets evaluated
2) i + j is evaluated next and say the result is stored in a temporary
location say temp
3) next temp + k++ gets evaluated and result stored in temporary
location say temp1
4) next x + temp1 gets evaluated and the result is assigned back to x.

So is the sequence of evaluations correct or am i missing out
something here?
Would it be correct to assume tht given an expression all unary
oerators are evaluated followed by evaluation of binary operators
thanks,
silpa

推荐答案

si **** @ gmail.com 说:




i am表达式评估顺序在表达式中有点困惑

涉及一元increment.decrement运算符和二进制

运算符。


例如在下面的表达式中


x + = i + j + k ++;


是以下评估顺序

1)因为++具有最高的先行子表达式k ++被评估为


i am a bit confused on expression evaluation order in expressions
involving unary increment.decrement operators along with binary
operators.

For example in the following expression

x += i + j + k++;

is the following the sequence of evaluations

1) as ++ has highest precedencce subexpression k++ gets evaluated



否你可以将优先级和关联性视为整理出来的

表达式将成为任何给定运算符的操作数。优先级

和关联性规则意味着上面的表达式具有相同的

意思是:


x + =((i + j )+(k ++));


未指定表达式的评估顺序。 k ++可能是首先完成的
,或者可能不是。 C不说。然而,它说的是,
是k ++的结果是前一个序列中的k值为
点,所以事实证明它并不是实际上无论如何都很重要。


我不能强调这一点:优先级和结合性都没有* b $ b *任何*与评估顺序有关。

No. You can think of precedence and associativity as sorting out which
expressions get to be the operands for any given operator. Precedence
and associativity rules mean that the above expression has the same
meaning as:

x += ((i + j) + (k++));

The order of evaluation of the expressions is unspecified. k++ might be
done first, or it might not. C doesn''t say. What it does say, however,
is that the result of k++ is the value k had at the previous sequence
point, so it turns out that it doesn''t actually matter anyway.

I cannot stress this enough: neither precedence nor associativity has
*anything* to do with order of evaluation.


假设给出一个表达式所有一元是否正确

oerators被评估,然后评估二元运算符
Would it be correct to assume tht given an expression all unary
oerators are evaluated followed by evaluation of binary operators



编号见上文。


-

Richard Heathfield

" Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

No. See above.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Richard Heathfield写道:
Richard Heathfield wrote:

我不能强调这一点:优先级和结合性都没有* b $ b *任何*与评估顺序有关。
I cannot stress this enough: neither precedence nor associativity has
*anything* to do with order of evaluation.



(fx:nitpick)假,因为P& A确定什么是'/ b
的操作数是什么,并且一般操作数必须在操作之前进行评估

他们是其中的一部分。


但这只是间接影响。


-

有一个门口有一个壁" / Master Humpries Clock /


Hewlett-Packard Limited注册号:

注册办事处:Cain Road,Bracknell,Berks RG12 1HN 690597英国

(fx:nitpick) False, since the P & A determines what''s an operand of
what, and in general operands have to be evaluated before the operation
of which they are a part.

But that''s only an indirect effect.

--
"There''s a doorway where there was a wall" /Master Humpries Clock/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England


Chris Dollin说:
Chris Dollin said:

Richard Heathfield写道:
Richard Heathfield wrote:

>我不能强调这一点:优先级和结合性都没有任何*与评估顺序有关。
>I cannot stress this enough: neither precedence nor associativity has
*anything* to do with order of evaluation.



(fx:nitpick)False,


(fx:nitpick) False,



错误。

Wrong.


A确定什么是什么的操作数,
since the P & A determines what''s an operand of what,



True。

True.


和一般操作数有在他们所属的

操作之前进行评估。
and in general operands have to be evaluated before the
operation of which they are a part.



这与我的观点无关。例如,考虑表达式:


a = b()* c()+ d();


这是一种合法的可能性生成的汇编语言代码:


JSR _d

MOV R20,R10;复制结果d()到reg 20

JSR _c

MOV R21,R10;将c()的结果复制到第21页

JSR _b

MOV R22,R10;将b()的结果复制到第22页

MOV R24,0; reg 24将保留整个expr的结果

ADD R24,R20

MUL R21,R22

ADD R24,R21;计算现已完成

MOV _a,R24


这里,评价的顺序是d(),c(),b(),加,

乘法,而优先级和关联性排序将是b / b
表示几乎相反。

That doesn''t matter for my point. For example, consider the expression:

a = b() * c() + d();

Here is one legal possibility for the generated assembly language code:

JSR _d
MOV R20, R10 ; copy result of d() to reg 20
JSR _c
MOV R21, R10 ; copy result of c() to reg 21
JSR _b
MOV R22, R10 ; copy result of b() to reg 22
MOV R24, 0 ; reg 24 will hold the result of the entire expr
ADD R24, R20
MUL R21, R22
ADD R24, R21 ; the calculation is now complete
MOV _a, R24

Here, the order of evaluation is d(), c(), b(), addition,
multiplication, whereas the precedence and associativity ordering would
suggest pretty much the opposite.


但是那个'只是间接影响。
But that''s only an indirect effect.



这是一个误导性的。


我不能强调这一点:优先级和结合性都没有

*任何*与评估顺序有关。


-

理查德希思菲尔德

Usenet是一个奇怪的放置" - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

And a misleading one.

I cannot stress this enough: neither precedence nor associativity has
*anything* to do with order of evaluation.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


这篇关于表达式和评估顺序和优先级规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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