编程语言
首页 > 编程语言> > java – 运算符优先级或最大蒙克规则首先出现在一元运算符中

java – 运算符优先级或最大蒙克规则首先出现在一元运算符中

作者:互联网

在这里,我有以下代码:

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

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

以下是编译的字节代码:

 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,因此编译错误.

上述行为是x = a b,因为运算符的优先级还是因为最大的munch规则?

解决方法:

引自Lexical Translations

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.

Thus, the input characters a–b are tokenized (07001) 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

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

标签:java,operator-precedence,unary-operator
来源: https://codeday.me/bug/20190612/1226204.html