📄 scanner.java
字号:
package scanner;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import javax.swing.JOptionPane;
import structureWord.Keywords;
import structureWord.KeywordsStruct;
import structureWord.Symbols;
import structureWord.Token;
import llanguage.BuildWindow;
public class Scanner {
String name = new String(); // 标识符名字
int radix; // 数字的进制
public void startScanner() {
BuildWindow bw = BuildWindow.getBuildWindow();
Keywords keyWords = new Keywords();
Symbols symbols = Symbols.getSymbols();
Token token = Token.getToken();
try {
File f = new File(bw.getFilePath() + bw.getFileName());
FileReader fr = new FileReader(f);
BufferedReader bfReader = new BufferedReader(fr); // 文件读取器
String strLine;
String strWord;
int lineNum = 0; // 行号
int wordNum = 0; // 本行单词号
boolean isKeyword; // 关键字否
int strId = 0; // 序号
while (true) {
strLine = new String();
strWord = new String();
char[] charWord; // 字符数组,存储一行字符
strLine = bfReader.readLine(); // 读取一行
if (strLine != null) {
strLine.trim(); // 略去前后空白符
lineNum++; // 行号
wordNum = 0; // 本行单词号
}
if (strLine == null) {
JOptionPane.showMessageDialog(null, "This File is End");
break;
}
int bp = 0; // 字符数组的下标
charWord = strLine.toCharArray();
while (bp < charWord.length) {
strWord = "";
isKeyword = false;
while (bp < charWord.length
&& (charWord[bp] == ' ' || charWord[bp] == '\t')) {
bp++;
}
while (bp < charWord.length && charWord[bp] != ' '
&& charWord[bp] != '\t') {
strWord = strWord + charWord[bp++];
}
if (strWord == "")
continue;
wordNum++; // 单词号自增
strId++; // 序号自增
for (Iterator it = keyWords.getArrayKeyWords().iterator(); it
.hasNext();) {// 这个地方调试了一个晚上,花的时间很长,却意识到了Iterator的用法和细节
KeywordsStruct ks = (KeywordsStruct) it.next();
if (strWord.equals((ks.getName()))) {// 对比一致
Token token1 = new Token();// 辛苦
token1.getTs().setLabel(strId);
token1.getTs().setName(ks.getName());
token1.getTs().setType(ks.getType());
token1.getTs().setCode(ks.getCode());
token.getTokenStruct().add(token1.getTs());
isKeyword = true;
break;
}
}
if (isKeyword == true) {
continue;
} else {// 检查是否是标识符或数字
boolean hasErrorChar = false;// 非法字符
char[] charW;
charW = strWord.toCharArray();
strWord = "";
for (int i = 0; i < charW.length; i++) {
if (!('a' <= charW[i] && charW[i] <= 'z'
|| 'A' <= charW[i] && charW[i] <= 'Z'
|| '0' <= charW[i] && charW[i] <= '9' || charW[i] == '.')) {
charW[i] = ' ';
hasErrorChar = true;
}
strWord = strWord + charW[i];
if (hasErrorChar) {
bw.gettaInfo().append(
"There is a illegal char at Line:"
+ lineNum + " Word:" + wordNum
+ "\n");
bw.gettaInfo().append(
"ErrorNumber: 1 illegal character\n");
bw.gettaInfo().append(
"And the compiler has deleted it\n\n");
}
}
strWord = strWord.replace(" ", "");
// System.out.println(strWord);
if (strWord == "") {
wordNum--;
strId--;
continue;
}// 非法字符结束
char[] charWo;
boolean hasCharInNumber = false;
charWo = strWord.toCharArray();
if ('a' <= charWo[0] && charWo[0] <= 'z'
|| 'A' <= charWo[0] && charWo[0] <= 'Z') {// 标识符,写入符号表
for (int i = 1; i < charWo.length; i++) {
if (charWo[i] == '.')
charWo[i] = '-';
}
Symbols symbols1 = new Symbols();// 辛苦
symbols1.getSb().setLabel(strId);
symbols1.getSb().setName(strWord);
symbols1.getSb().setType(2);
symbols1.getSb().setCode(18);
symbols.getSymbolStruct().add(symbols1.getSb());
} else if ('0' <= charWo[0] && charWo[0] <= '9') {// 整数或实数,数字开头
int pointNum = 0;
int bp2 = charWo.length - 1; // 异常下标
for (int i = 0; i < charWo.length; i++) {
if (charWo[i] == '.') {
pointNum++;
if (pointNum == 2) {
bp2 = i;
break;
}
} else if ('a' <= charWo[i] && charWo[i] <= 'z'
|| 'A' <= charWo[i] && charWo[i] <= 'Z') {
hasCharInNumber = true;
bp2 = i;
break;
}
}
strWord = "";
for (int i = 0; i < bp2; i++) {
strWord = strWord + charWo[i];
}
if (pointNum == 0) {// 整数
Symbols symbols1 = new Symbols();// 辛苦
symbols1.getSb().setLabel(strId);
symbols1.getSb().setName(strWord);
symbols1.getSb().setType(3);
symbols1.getSb().setCode(19);
symbols.getSymbolStruct().add(symbols1.getSb());
} else if (pointNum == 1) {// 实数
Symbols symbols1 = new Symbols();
symbols1.getSb().setLabel(strId);
symbols1.getSb().setName(strWord);
symbols1.getSb().setType(3);
symbols1.getSb().setCode(20);
symbols.getSymbolStruct().add(symbols1.getSb());
} else {
Symbols symbols1 = new Symbols();
symbols1.getSb().setLabel(strId);
symbols1.getSb().setName(strWord);
symbols1.getSb().setType(3);
symbols1.getSb().setCode(20);
symbols.getSymbolStruct().add(symbols1.getSb());
}
if (pointNum >= 2) {
bw.gettaInfo().append(
"There is a illegal char at Line:"
+ lineNum + " Word:" + wordNum
+ "\n");
bw
.gettaInfo()
.append(
"ErrorNumber: 2 illegal number--there is more than one radix point\n");
bw
.gettaInfo()
.append(
"And the compiler has deleted it\n\n");
} else if (hasCharInNumber) {
bw.gettaInfo().append(
"There is a illegal char at Line:"
+ lineNum + " Word:" + wordNum
+ "\n");
bw
.gettaInfo()
.append(
"ErrorNumber: 3 illegal number--has characters in number\n");
bw
.gettaInfo()
.append(
"And the compiler has deleted it\n\n");
}
}
}
}
}
} catch (FileNotFoundException fnfe) {
JOptionPane.showMessageDialog(null,
"This file is not found Exception");
} catch (IOException e) {
// TODO Auto-generated catch block
JOptionPane.showMessageDialog(null, "There is a IOException");
}
ShowTokenSymbols show = new ShowTokenSymbols();
show.showTS();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -