📄 parser.java
字号:
symType = Lexer.getSym();
} else {
ErrorManager.error(47);
pSkip(47);
}
int tp = exprs();
if (symType == ConstSet.RIGHT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(42);
pSkip(42);
}
if (symType == ConstSet.LEFT_CURLY) {
crtLevel++;
symType = Lexer.getSym();
} else {
ErrorManager.error(16);
pSkip(16);
}
value = Lexer.getValue();
if (value.equals("case")) {
caseLst(tp);
} else {
ErrorManager.error(49);
pSkip(49);
}
value = Lexer.getValue();
if (value.equals("default")) {
symType = Lexer.getSym();
if (symType == ConstSet.COLON) {
symType = Lexer.getSym();
} else {
ErrorManager.error(12);
pSkip(12);
}
stnc();
CodeGen.gen(ConstSet.CRC_INSTR, 0, 0);
}
if (symType == ConstSet.RIGHT_CURLY) {
crtLevel--;
symType = Lexer.getSym();
} else {
ErrorManager.error(48);
pSkip(48);
}
return;
}
/**
* 情况表分析方法
*
* @param type
* 待匹配表达式类型
* @return 情况语句个数
*/
public static int caseLst(int type) {
InfoPane.write("caseLst(int type) is called!" + '\n');
int caseCount = 0;
do {
symType = Lexer.getSym();
if (symType == ConstSet.INTEGER || symType == ConstSet.REAL
|| symType == ConstSet.CHARACTER) {
value = Lexer.getValue();
CodeGen.gen(ConstSet.LST_INSTR, 0, 0);
if (symType > type)
symType = type;
if (symType == ConstSet.REAL)
CodeGen.gen(ConstSet.LIT_INSTR, ConstSet.REAL, value);
else
CodeGen.gen(ConstSet.LIT_INSTR, ConstSet.INTEGER, value);
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_FLTSUB
- symType % 2);
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_EAQ);
CodeGen.gen(ConstSet.CRJ_INSTR, 0, 0);
int tmp = CodeGen.px;
CodeGen.gen(ConstSet.JPC_INSTR, 0, 0);
symType = Lexer.getSym();
if (symType == ConstSet.COLON) {
symType = Lexer.getSym();
} else {
ErrorManager.error(12);
pSkip(12);
}
stnc();
int cPx = CodeGen.px;
CodeGen.reset(tmp, ConstSet.JPC_INSTR, 0, cPx);
value = Lexer.getValue();
} else {
ErrorManager.error(13);
pSkip(13);
}
caseCount++;
} while (value.equals("case"));
return caseCount;
}
/**
* 返回语句分析方法
*
* @return 实际返回值的类型
*/
public static int rtStnc() {
InfoPane.write("rtStnc() is called!" + '\n');
int tmp;
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
symType = Lexer.getSym();
tmp = exprs();
if (symType == ConstSet.RIGHT_PRENTHESES) {
hasValReturned = true;
symType = Lexer.getSym();
} else {
ErrorManager.error(42);
pSkip(42);
}
// 带值返回
if (tmp == crtRetType)
CodeGen.gen(ConstSet.STR_INSTR, crtRetType, 0);// 将栈顶加载到返回值寄存器
// 返回类型不匹配
else {
ErrorManager.error(10);
pSkip(10);
}
} else {
hasNVReturned = true;
tmp = ConstSet.VOIDTYPE;
}
CodeGen.gen(ConstSet.RET_INSTR, 0, 0);
return tmp;
}
/**
* 编译过程正常结束方法
*/
@SuppressWarnings("deprecation")
public static void halt() {
InfoPane.write("halt() is called!" + '\n');
Lexer.setDebug(false);
ProblemPane.write(ErrorManager.errCount + " errors, "
+ ErrorManager.warCount + " warnings totally!" + '\n');
if (ErrorManager.errCount == 0)
HandoutFrame.tb9.setEnabled(true);
ErrorManager.errCount = 0;
ErrorManager.warCount = 0;
ProblemPane.write(ConstSet.COMPCOMPLETE);
ConsolePane.write(ConstSet.COMPCOMPLETE);
InfoPane.write(ConstSet.COMPCOMPLETE);
HandoutFrame.tb8.setEnabled(false);
HandoutFrame.tb11.setEnabled(false);
}
/**
* 编译过程异常结束方法
*/
@SuppressWarnings("deprecation")
public static void excHalt() {
InfoPane.write("excHalt() is called!" + '\n');
ProblemPane.write(ConstSet.COMPINTRUPT);
ConsolePane.write(ConstSet.COMPINTRUPT);
InfoPane.write(ConstSet.COMPINTRUPT);
HandoutFrame.tb8.setEnabled(false);
HandoutFrame.tb11.setEnabled(false);
thr.stop();
}
/**
* 语法错误跳读方法
*
* @param errNum
* 错误号
*/
private static void pSkip(int errNum) {
if (ErrorManager.symErrSet[errNum].length == 0)
return;
while (!found(symType, ErrorManager.symErrSet[errNum])) {
symType = Lexer.getSym();
if (symType == ConstSet.STOP) {
ErrorManager.error(1);
excHalt();
}
}
return;
}
/**
* 用于错误跳读的包含检索方法
*
* @param sym
* 带检索的类型
* @param charSet
* 检索集合
* @return sym是否在charSet中
*/
private static boolean found(int sym, int[] charSet) {
int len = charSet.length;
for (int idx = 0; idx < len; idx++) {
if (sym == charSet[idx])
return true;
}
return false;
}
/**
* 函数返回时的符号表整理方法
*/
private static void fQuit() {
SymTable.quit();
}
/**
* 常量填入方法
*
* @param name
* 常量名
* @param type
* 常量类型
* @param value
* 常量值
* @return
*/
private static int constInsert(String name, String type, String value) {
int tmp = SymTable.search(name, crtLevel);
// 未找到或在外层找到
if (tmp == ConstSet.NOTFOUND || tmp == ConstSet.FINDINOLV)
return SymTable.addRow(new SymTableItem(name, "const", type, value,
"", crtLevel));
// 在本层找到
else {
ErrorManager.error(51);
pSkip(51);
return 0;
}
}
/**
* 变量填入方法
*
* @param name
* 变量名
* @param type
* 变量类型
* @param add
* 变量相对地址
* @return
*/
private static int varInsert(String name, String type, int add) {
int tmp = SymTable.search(name, crtLevel);
// 未找到或在外层找到
if (tmp == ConstSet.NOTFOUND || tmp == ConstSet.FINDINOLV)
return SymTable.addRow(new SymTableItem(name, "var", type, "0",
add + "", crtLevel));
// 在本层找到
else {
ErrorManager.error(51);
pSkip(51);
return 0;
}
}
/**
* 有返回值函数填入方法
*
* @param name
* 函数名
* @param type
* 返回值类型
* @param add
* 函数初始指令地址
* @return 当前符号表地址,若失败返回0
*/
private static int rFuncInsert(String name, String type, int add) {
int tmp = SymTable.search(name, crtLevel);
// 未找到或在外层找到
if (tmp == ConstSet.NOTFOUND || tmp == ConstSet.FINDINOLV)
return SymTable.addRow(new SymTableItem(name, "func", type, "", add
+ "", crtLevel));
// 在本层找到
else {
ErrorManager.error(51);
pSkip(51);
return 0;
}
}
/**
* 无返回值函数填入方法
*
* @param name
* 函数名
* @param add
* 函数初始指令地址
* @return 当前符号表地址,若失败返回0
*/
private static int nrFuncInsert(String name, int add) {
int tmp = SymTable.search(name, crtLevel);
// 未找到或在外层找到
if (tmp == ConstSet.NOTFOUND || tmp == ConstSet.FINDINOLV)
return SymTable.addRow(new SymTableItem(name, "func", "void", "",
add + "", crtLevel));
// 在本层找到
else {
ErrorManager.error(51);
pSkip(51);
return 0;
}
}
/**
* 参数填入方法
*
* @param name
* 参数名
* @param type
* 参数类型
* @param add
* 参数相对地址
* @return 当前符号表地址,若失败返回0
*/
private static int paraInsert(String name, String type, int add) {
int tmp = SymTable.search(name, crtLevel);
if (tmp == ConstSet.NOTFOUND || tmp == ConstSet.FINDINOLV)
return SymTable.addRow(new SymTableItem(name, "para", type, "", ""
+ add, crtLevel));
else {
ErrorManager.error(51);
pSkip(51);
return 0;
}
}
/**
* 线程初始化方法
*/
private static void init() {
try {
SymTable.clear();
CodePane.clear();
CorePane.clear();
ProblemPane.write(ConstSet.COMPSTART);
ConsolePane.write(ConstSet.COMPSTART);
InfoPane.write(ConstSet.COMPSTART);
Lexer.init();
dx = 3;
crtLevel = 0;
varCount = 0;
paraCount = 0;
crtRetType = ConstSet.VOIDTYPE;
symType = ConstSet.VOIDTYPE;
hasJumped = false;
hasValReturned = false;
hasNVReturned = false;
nameBuf = new String();
idenType = new String();
value = new String();
CodeGen.init();
} catch (RuntimeException e) {
// TODO Auto-generated catch block
ProblemPane.write(e.getMessage());
}
}
/**
* @param t
* 已经初始化的线程
*/
public static void setThread(Thread t) {
thr = t;
}
public static void setToOpen(File f) {
toOpen = f;
}
/**
* 本类的静态线程变量,初始化时被赋值,用于中止和休眠
*/
static Thread thr;
/**
* 为最外层预留的三个栈单元,本版本中不使用
*/
private static int dx;
/**
* 用于探测主函数跳转的指令地址
*/
private static int tmpPx;
/**
* 参数表返回的参数个数
*/
private static int paraCount;
/**
* 变量声明中的变量个数
*/
private static int varCount;
/**
* 当前层次数
*/
private static int crtLevel;
/**
* 用于暂时存放Lexer.getValue()方法返回的标识符名字
*/
private static String nameBuf;
/**
* 用于接受Lexer.getSym()方法返回的符号类型
*/
private static int symType;
/**
* 当前函数的声明返回值类型
*/
private static int crtRetType;
/**
* 探测主函数是否跳转的标志变量
*/
private static boolean hasJumped;
/**
* 探测带值返回函数是否返回的标志变量
*/
private static boolean hasValReturned;
/**
* 探测无值返回函数是否返回的标志变量
*/
private static boolean hasNVReturned;
/**
* 用于暂时存放变量类型的类型变量
*/
private static String idenType;
/**
* 用于接受Lexer.getValue()方法返回的名字
*/
private static String value;
private static File toOpen = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -