3 词法结构

3.1 Unicode

  • Java编程语言的文本是使用UTF-16编码机制。即Java编译器编译的对象就是UTF-16字符流,Java虚拟机运行时字符和字符串都是用的UTF-16编码。

3.2 词法翻译

  • 原始的Unicode字符流会翻译为符号序列,翻译过程有三个过程。
  1. 将Unicode转移字符转换为相应Unicode字符。Unicode转移字符的形式为\uxxxx,其中xxxx是十六进制数。\uxxxx表示的是编码为xxxx的UTF-16码元。
  2. 将步骤1产生的Unicode流翻译成由输入字符(InputCharacter)和行终止符(LineTerminator)构成的流。
  3. 将步骤2产生的流翻译成输入元素构成的序列,输入元素由符号构成,为句法的的终结符。摒弃了空白和注释。
    步骤1中我的理解:
    1.UnicodeInputCharacter流
    2.将UnicodeEscape转换为RawInputCharacte
    3.RawInputCharacter流

3.3 Unicode转义字符

UnicodeInputCharacter:
UnicodeEscape
RawInputCharacter

UnicodeEscape:
UnicodeMarker HexDigit HexDigit HexDigit HexDigit

UnicodeMarker:
 u{u}

HexDigit: one of
 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F

RawInputCharacter:
 any Unicode character

3.4 行终止符

LineTerminator:
 the ASCII LF character, also known as "newline"
 the ASCII CR character, also known as "return"
 the ASCII CR character followed by the ASCII LF character

InputCharacter:
UnicodeInputCharacter but not CR or LF

3.5 输入元素和符号

输入元素(InputElement)去掉注释(Comment)和空白(WhiteSpace)就是符号(Token),符号是句法的终结符。

Input:
 {InputElement} [Sub]

InputElement:
WhiteSpace
Comment
Token

Token:
Identifier
KeyWord
Literal
Separator
Operator

Sub:
 the ASCII SUB character, also known as "control-Z"

你可能感兴趣的:(3 词法结构)