Java- 运营商订单

Java- Operators order

为什么 a=b+++c 被解析为 a=b++,+,c? 它可以是 a= b,+,++c 吗? lex工具有没有遵循什么顺序或规则?

 int a = 20, b = 10, c = 0;

         // a=b+++c is compiled as
         // b++ +c
         // a=b+c then b=b+1
         a = b+++c;
         System.out.println("Value of a(b+c),b(b+1),c = " + a + "," + b + "," + c);

输出:

Value of a(b+c),b(b+1),c = 10,11,0

是的,词法分析器很贪婪。当它找到一个新的 + 时,它的首要任务是检查它是否可以用构成多字符运算符的字符来完成,例如 ++ 或 +=

从a++1和a++1的区别就可以看出,一个无效,一个虽然白痴,但有效

当 Java 代码被标记化时,它将使用 Java Language Specification Section 3.2 中规定的最长可能的标记:

3.2. Lexical Translations

A raw Unicode character stream is translated into a sequence of tokens, using the following three lexical translation steps, which are applied in turn:

  1. A translation of Unicode escapes (§3.3) in the raw stream of Unicode characters to the corresponding Unicode character. A Unicode escape of the form \uxxxx, where xxxx is a hexadecimal value, represents the UTF-16 code unit whose encoding is xxxx. This translation step allows any program to be expressed using only ASCII characters.

  2. A translation of the Unicode stream resulting from step 1 into a stream of input characters and line terminators (§3.4).

  3. A translation of the stream of input characters and line terminators resulting from step 2 into a sequence of input elements (§3.5) which, after white space (§3.6) and comments (§3.7) are discarded, comprise the tokens (§3.5) that are the terminal symbols of the syntactic grammar (§2.3).

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.