[英]Operator precedence or Maximal Munch Rule comes first for Unary Operators
Here I am having the following piece of code: 在这里,我有以下代码:
int a,b,x;
a=b=1;
x=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
. 现在x
的值将为2,因为a
首先是后递增 ,然后将其添加到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]
So the expression will be equivalent to x = (a++) + b
. 因此表达式将等效于x = (a++) + b
。
Now the other expression x=a++++b
, won't compile because of the maximal munch rule . 现在另一个表达式x=a++++b
,由于最大的munch规则而无法编译。 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 ? 上述x=a+++b
行为是因为运算符 ++的优先级还是因为最大的munch规则 ?
Quoting from 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 ( §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. 因此,输入字符a-b被标记化( §3.5 )为a , - , b ,它不是任何语法正确程序的一部分,即使标记化a , - , - , b可能是语法的一部分正确的程序。
This would explain why 这可以解释原因
x=a+++b
is parsed as 被解析为
x=(a++)+b
On the other hand, a++++b
is tokenized as a++ , ++ , b which causes an error. 另一方面, a++++b
被标记为++ , ++ , b ,这会导致错误。
The unary operator "++" is recognized only when there is a variable to the left of the "++". 仅当“++”左侧有变量时,才会识别一元运算符“++”。 When you write a+++b, the third plus is the binary operator "add", while the first operator (++) is "increment variable by 1". 当你写一个+++ b时,第三个加号是二进制运算符“add”,而第一个运算符(++)是“递增变量1”。 When you write "a++++" things fail because this is like writing a<unary increment variable by 1> <add> <add>
and there is a missing argument for the first operator. 当你写“a ++++”时,事情会失败,因为这就像写a<unary increment variable by 1> <add> <add>
并且第一个运算符缺少一个参数。 The second pair of plus signs is not recognized as an "increment variable" because (a++) is not a variable. 第二对加号不被识别为“增量变量”,因为(a ++)不是变量。
Now interestingly, the Java compiler does currently requires white space to properly recognize 现在有趣的是,Java编译器当前需要空格才能正确识别
z = a++ + ++b; // this works
z = a+++++b; // this fails
As an old compiler writer, I would expect that both constructs should be syntactically evaluated as the same (recognizing the two unary operators ++ and ++ 作为一个旧的编译器编写器,我希望这两个结构应该在语法上被评估为相同(识别两个一元运算符++和++
Maximal munch is a rule that is used in the lexer, operator precedence in the parser, and the lexer runs conceptually before the parser. 最大munch是在词法分析器中使用的规则,解析器中的运算符优先级,并且词法分析器在解析器之前在概念上运行。 Hence, x=a+++b
is turned into x=(a++)+b
because of the maximal munch rule, not operator precedence: 因此, x=a+++b
变成x=(a++)+b
因为最大的munch规则,而不是运算符优先级:
When the lexer sees a+++b
is will turn this into tokens [identifier a
] [double plus] [plus] [identifier b
]. 当词法分析器看到a+++b
会将其转换为标记[标识符a
] [双加] [加号] [标识符b
]。 The [double plus] token is due to maximal munch (take the longest match, and ++
is longer than +
). [double plus]令牌是由于最大咬合(采用最长匹配,而++
长于+
)。 The parser can then only turn this into (a++)+b regardless of operator precedence. 然后,无论运算符优先级如何,解析器都只能将其转换为(a ++)+ b。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.