gpt4 book ai didi

java - 为什么 javac 不接受 `x = x+++++y` ?

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:46:50 24 4
gpt4 key购买 nike

从编译器理论的角度来看,为什么javac编译器不接受x = x+++++y形式的语句,而是接受x = x+++++y ?

最佳答案

因为 ++ 是 java 词法分析器的有效标记,语句 x+++++y 将被解析为标记:

(x)(++)(+)( )(++)(y)

x+++++y 将被标记为无效的:

(x)(++)(++)(+)(y)

以上是无效的 java,因为 ++ 运算符只能应用于数字变量,而 (x++) 的结果不是。这种类型的标记化是称为 maximal munch 的概念的一个很好的例子。 .

来自JLS section 3.2

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.

关于java - 为什么 javac 不接受 `x = x+++++y` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16477768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com