(1)编码
词法分析将Java源文件的字符流转变为对应的Token流。一些规范的Token定义在Token枚举类中,如下:
其实在形成一个Token流时,在词法分析的过程中,可以归纳为三类,分别是:
/** An interface that defines codes for Java source tokens * returned from lexical analysis. */ public enum Token implements Formattable { EOF, ERROR, IDENTIFIER, // 如类名、包名、变量名、方法名等 ABSTRACT("abstract"), ASSERT("assert"), BOOLEAN("boolean"), BREAK("break"), BYTE("byte"), CASE("case"), CATCH("catch"), CHAR("char"), CLASS("class"), CONST("const"), CONTINUE("continue"), DEFAULT("default"), DO("do"), DOUBLE("double"), ELSE("else"), ENUM("enum"), EXTENDS("extends"), FINAL("final"), FINALLY("finally"), FLOAT("float"), FOR("for"), GOTO("goto"), IF("if"), IMPLEMENTS("implements"), IMPORT("import"), INSTANCEOF("instanceof"), INT("int"), INTERFACE("interface"), LONG("long"), NATIVE("native"), NEW("new"), PACKAGE("package"), PRIVATE("private"), PROTECTED("protected"), PUBLIC("public"), RETURN("return"), SHORT("short"), STATIC("static"), STRICTFP("strictfp"), SUPER("super"), SWITCH("switch"), SYNCHRONIZED("synchronized"), THIS("this"), THROW("throw"), THROWS("throws"), TRANSIENT("transient"), TRY("try"), VOID("void"), VOLATILE("volatile"), WHILE("while"), INTLITERAL, LONGLITERAL, FLOATLITERAL, DOUBLELITERAL, CHARLITERAL, STRINGLITERAL, TRUE("true"), FALSE("false"), NULL("null"), LPAREN("("), RPAREN(")"), LBRACE("{"), RBRACE("}"), LBRACKET("["), RBRACKET("]"), SEMI(";"), COMMA(","), DOT("."), ELLIPSIS("..."), EQ("="), GT(">"), LT("<"), BANG("!"), TILDE("~"), QUES("?"), COLON(":"), EQEQ("=="), LTEQ("<="), GTEQ(">="), BANGEQ("!="), AMPAMP("&&"), BARBAR("||"), PLUSPLUS("++"), SUBSUB("--"), PLUS("+"), SUB("-"), STAR("*"), SLASH("/"), AMP("&"), BAR("|"), CARET("^"), PERCENT("%"), LTLT("<<"), GTGT(">>"), GTGTGT(">>>"), PLUSEQ("+="), SUBEQ("-="), STAREQ("*="), SLASHEQ("/="), AMPEQ("&="), BAREQ("|="), CARETEQ("^="), PERCENTEQ("%="), LTLTEQ("<<="), GTGTEQ(">>="), GTGTGTEQ(">>>="), MONKEYS_AT("@"), CUSTOM; // 省略相关的方法 }
(1)标识符号:如Token.PLUS、Token.EQ、Token.LBRACE、Token.RBRACE等
(2)Java的保留关键字:
数据类型:
Boolean\int\long\short\byte\float\double\char\class\interface
流程控制:
if\else\do\while\for\switch\case\default\break\continue\return\try\catch\finally
修饰符:
public
protected
private
final
void
static
strictfp
abstract
transient
synchronized
volatile
native
动作:
package
import
throw
throws
extends
implements
this
Super
instanceof
new
保留字:
true\false\null\goto\const
(3)Token.IDENTIFIER
用来表示用户自定义的类名、包名、变量包、方法名等
(1)词法元素(关键字、符号和自定义单词)
(2)Java的Name与QualifiedName
一个字符一个字符的读取源代码,形成规范化的Token流。规范化的Token包含:
- java关键词:package、import、public、class、int等
- 自定义单词:包名、类名、变量名、方法名
- 符号:=、;、+、-、*、/、%、{、}等
原文地址:https://www.cnblogs.com/extjs4/p/9695362.html
时间: 2024-11-08 23:43:34