/* 作者:龚奇源
* 单位:东南大学
* 时间:2008-11-16下午04:49:19
* 功能:
*
*/
package JAccidenceAnalyse;
import JAccidenceAnalyse.Buffer.*;
public class Scaner {
public ScanBuffer scanBuffer; // 扫描缓冲区--共享
private String finalAccidence;
private AccidenceAnalyser aa;
private int BUFFER_SIZE = 100;
private String toDelString;
private int senLength = 0;
private char[] sentenceChar = new char[1000];
private String TOKEN;
private char CHAR;
private int index = 0;
private String IDENTITY = "identity";
private String DIGIT = "digit";
private String WORD_ERROR_INF = "在此行发现不能识别的单词,此行分析终止!";
private boolean ASTATE = true;
/**
* @roseuid 3D9BB9370213
*/
public Scaner(AccidenceAnalyser aa) {
this.aa = aa;
initBuffer();
this.finalAccidence = "";
System.out.println("[INFOR]扫描处理器已经创建!");
}
/**
* @roseuid 3D9BB2860329
*/
public String readFromBuffer(char[] Data) {
String toDelString = String.valueOf(Data);
return toDelString;
}
/**
* @param tmpString
* @return String
* @roseuid 3D9BB2D5008D
*/
public String scan(String toDelString) {
sentenceChar = toDelString.toCharArray();
this.senLength = sentenceChar.length;
int i = 0;
// 分析单词
while (this.index <= this.senLength) {
// state0:
this.TOKEN = "";
this.CHAR = GETBC(sentenceChar);
if (this.CHAR == ';') {
break; // ';'表示这一行结束
}
// 进入状态判断
switch (this.CHAR) {
// judge letter
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
// do
this.TOKEN = this.CONTACT(TOKEN, CHAR);
// state1
CHAR = this.GETCHAR(sentenceChar);
while (this.ISLETTER(CHAR) || this.ISDIGIT(CHAR)) {
this.TOKEN = this.CONTACT(this.TOKEN, CHAR);
CHAR = this.GETCHAR(sentenceChar);
}
this.RETRACT();
// state2
if (aa.keyWordTable.isKeyWord(TOKEN)) {
this.finalAccidence = this.finalAccidence + "[保留字] "
+ this.returnAWord(TOKEN) + "\n";
} else {
this.finalAccidence = this.finalAccidence
+ "[标识符] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String
.valueOf(aa.classIdentity.findKey(IDENTITY))
+ "\n";
}
// clear up token
this.TOKEN = "";
break;
// judge ditital
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
// do
this.TOKEN = this.CONTACT(TOKEN, CHAR);
// state3
CHAR = this.GETCHAR(sentenceChar);
while (this.ISDIGIT(CHAR)) {
this.TOKEN = this.CONTACT(TOKEN, CHAR);
CHAR = this.GETCHAR(sentenceChar);
}
this.RETRACT();
// state4
this.finalAccidence = this.finalAccidence + "[数字] "
+ this.returnAWord(TOKEN) + "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(DIGIT))
+ "\n";
// clear up token
this.TOKEN = "";
break;
case '=':
// state5
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[等号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '+':
// state6
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[加号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '*':
// state7
this.TOKEN = this.CONTACT(TOKEN, CHAR);
CHAR = this.GETCHAR(sentenceChar);
// state8
if (CHAR == '*') {
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[乘方] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
}
// state9
else {
this.finalAccidence = this.finalAccidence
+ "[乘号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
}
// clear up token
this.TOKEN = "";
break;
case ',':
// state10
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[逗号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '(':
// state11
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[左括号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case ')':
// state12
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[右括号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '{':
// state13
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[左大括号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '}':
// state14
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[右大括号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '[':
// state15
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[左中括号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case ']':
// state16
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[右中括号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
case '.':
// state17
this.TOKEN = this.CONTACT(TOKEN, CHAR);
this.finalAccidence = this.finalAccidence
+ "[点号] "
+ this.returnAWord(TOKEN)
+ "[种别码] "
+ String.valueOf(aa.classIdentity.findKey(String
.valueOf(CHAR))) + "\n";
// clear up token
this.TOKEN = "";
break;
default:
// state18
this.TOKEN = this.CONTACT(this
JAVA版的JAVA词法分析器
5星 · 超过95%的资源 需积分: 10 89 浏览量
2008-11-21
19:14:07
上传
评论 5
收藏 26KB RAR 举报
darkfreedom
- 粉丝: 1
- 资源: 7
最新资源
- 使用C语言与python分别实现文件的读写功能
- zephyr sdk package 2
- zephyr sdk package 1
- optimization.ipynb
- 数据库标识码BSM重排序工具、重构标识码工具
- 基于C语言的校园导航系统报告.doc
- __init__.py
- tensorflow-gpu-2.6.5-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-rocm-2.13.1.600-cp38-cp38-manylinux2014-x86-64.whl
- tensorflow-rocm-2.13.0.570-cp311-cp311-manylinux2014-x86-64.whl
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈