一元运算符的运算符优先级或最大Munch规则排在第一位 [英] Operator precedence or Maximal Munch Rule comes first for Unary Operators

查看:123
本文介绍了一元运算符的运算符优先级或最大Munch规则排在第一位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这里有以下代码:

int a,b,x;
a=b=1;
x=a+++b;

现在,x的值将为2,因为先将a进行后递增,然后将其添加到b.

Now the value of x will be 2 as a is first being post incremented and then it is being added to b.

下面是编译后的字节码:

Following is the compiled byte code :

 0  iconst_1
 1  dup
 2  istore_2 [b]
 3  istore_1 [a]
 4  iload_1 [a]
 5  iinc 1 1 [a]
 8  iload_2 [b]
 9  iadd
10  istore_3 [x]

因此表达式将等同于x = (a++) + b.

现在,另一个表达式x=a++++b由于最大munch规则而无法编译.它将变为x = (a++) ++ b,因此会出现编译错误.

Now the other expression x=a++++b, won't compile because of the maximal munch rule. It will become x = (a++) ++ b and hence compilation error.

x=a+++b的上述行为是由于优先级 ++还是由于最大限制规则?

Is the above behavior of x=a+++b because of the precedence of the operator ++ or because of maximal munch rule?

推荐答案

即使在 结果最终并不能做出正确的程序,而另一个 词汇翻译就可以.

The longest possible translation is used at each step, even if the result does not ultimately make a correct program while another lexical translation would.

因此,输入字符 a--b 被标记了(

Thus, the input characters a--b are tokenized (§3.5) as a, --, b, which is not part of any grammatically correct program, even though the tokenization a, -, -, b could be part of a grammatically correct program.

这将解释原因

x=a+++b

解析为

x=(a++)+b

另一方面,a++++b被标记为 a ++ ++ b ,这会导致错误.

On the other hand, a++++b is tokenized as a++, ++, b which causes an error.

这篇关于一元运算符的运算符优先级或最大Munch规则排在第一位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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