📄 parser.java
字号:
} else if (tmpVal.equals(">=")) {
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_NLESS);// 栈顶<0为假
// <0,=0为真
} else {
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_NEQU);
}
return;
}
/**
* 表达式分析方法
*
* @return 表达式值类型
*/
public static int exprs() {
InfoPane.write("exprs() is called!" + '\n');
int type1;
if (symType == ConstSet.SUBSYM) {
symType = Lexer.getSym();
type1 = item();
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_MUNZERO);// 进行栈顶取负
} else if (symType == ConstSet.ADDSYM) {
symType = Lexer.getSym();
type1 = item();
} else
type1 = item();
// 进行隐式的类型转换
while (symType == ConstSet.ADDSYM || symType == ConstSet.SUBSYM) {
int tmpType = symType;
symType = Lexer.getSym();
int type2 = item();
if (type2 < type1)
type1 = type2;
// 如果是加号
if (tmpType == ConstSet.ADDSYM) {
if (type1 == ConstSet.REAL)
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_FLTADD);
else
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_INTADD);
}
// 如果是减号
else {
if (type1 == ConstSet.REAL)
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_FLTSUB);
else
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_INTSUB);
}
}
return type1;
}
/**
* 项分析方法
*
* @return 项值类型
*/
public static int item() {
InfoPane.write("item() is called!" + '\n');
int type1 = factor();
while (symType == ConstSet.MULSYM || symType == ConstSet.DIVSYM) {
int tmpType = symType;
symType = Lexer.getSym();
int type2 = factor();
if (type2 < type1)
type1 = type2;
if (tmpType == ConstSet.MULSYM) {
if (type1 == ConstSet.REAL)
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_FLTMUL);
else
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_INTMUL);
} else {
if (type1 == ConstSet.REAL)
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_FLTDIV);// 须判断除以零,由虚机进行
else
CodeGen.gen(ConstSet.OPR_INSTR, 0, ConstSet.OPR_INTDIV);// 须判断除以零,由虚机进行
}
}
return type1;
}
/**
* 因子分析方法
*
* @return 因子值类型
*/
public static int factor() {
InfoPane.write("factor() is called!" + '\n');
int type = ConstSet.INTEGER;
switch (symType) {
// 如果是变量、常量或带返回值的函数名
case ConstSet.IDENTIFIER:
value = Lexer.getValue();
String name = value;
// 如果是变量或常量
if (SymTable.search(name, crtLevel) != ConstSet.NOTFOUND) {
ItemInfo tmpInfo = SymTable.getEleInfo();
// 在符号表中找到
if (tmpInfo != null) {
if (tmpInfo.kind.equals("const")) {
CodeGen.gen(ConstSet.LIT_INSTR, tmpInfo.type,
tmpInfo.value);
} else {
// 已被赋值
CodeGen.gen(ConstSet.LOD_INSTR, tmpInfo.lvm,
tmpInfo.address);// 取变量值到栈顶
}
type = tmpInfo.type;
symType = Lexer.getSym();
value = Lexer.getValue();
} else {
ErrorManager.error(53);
pSkip(53);
}
}
// 如果是有返回值函数调用
else if (SymTable.searchFunc(name) == ConstSet.RFRETURN) {
ItemInfo tmpInfo = SymTable.getFuncInfo();
type = tmpInfo.type;
rFcStnc();
} else {
ErrorManager.error(52);
pSkip(52);
}
break;
// 如果是表达式
case ConstSet.LEFT_PRENTHESES:
symType = Lexer.getSym();
type = exprs();
if (symType == ConstSet.RIGHT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(42);
pSkip(42);
}
break;
case ConstSet.INTEGER:
value = Lexer.getValue();
type = ConstSet.INTEGER;
CodeGen.gen(ConstSet.LIT_INSTR, ConstSet.INTEGER, value);
symType = Lexer.getSym();
break;
case ConstSet.REAL:
value = Lexer.getValue();
CodeGen.gen(ConstSet.LIT_INSTR, ConstSet.REAL, value);
type = ConstSet.REAL;
symType = Lexer.getSym();
break;
case ConstSet.CHARACTER:
value = Lexer.getValue();
CodeGen.gen(ConstSet.LIT_INSTR, ConstSet.CHARACTER, value);
type = ConstSet.CHARACTER;
symType = Lexer.getSym();
break;
default:
type = ConstSet.INTEGER;
ErrorManager.error(43);
pSkip(43);
}
return type;
}
/**
* 循环语句分析方法
*/
public static void loopStnc() {
InfoPane.write("loopStnc() is called!" + '\n');
int tmp1 = CodeGen.px;
int tmp2;
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(44);
pSkip(44);
}
cndit();
if (symType == ConstSet.RIGHT_PRENTHESES) {
tmp2 = CodeGen.px;
CodeGen.gen(ConstSet.JPC_INSTR, 0, 0);
symType = Lexer.getSym();
} else {
ErrorManager.error(41);
pSkip(41);
return;
}
stnc();
CodeGen.gen(ConstSet.JMP_INSTR, 0, tmp1);
CodeGen.reset(tmp2, ConstSet.JPC_INSTR, 0, CodeGen.px);
return;
}
/**
* 有返回值函数调用语句分析方法
*
* @return 返回类型
*/
public static int rFcStnc() {
InfoPane.write("rFcStnc() is called!" + '\n');
value = Lexer.getValue();
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(31);
pSkip(31);
}
CodeGen.gen(ConstSet.INT_INSTR, 0, 3);// 为控制区分配空间
int paras = valParaLst();
CodeGen.gen(ConstSet.STC_INSTR, 0, paras);
ItemInfo tmpInfo = SymTable.getFuncInfo();// 进入前已经查过表
if (tmpInfo.lvm == paras) {
CodeGen.gen(ConstSet.CAL_INSTR, 0, tmpInfo.address);
CodeGen.gen(ConstSet.LDR_INSTR, 0, 0);
} else {
ErrorManager.error(7);
pSkip(7);
return tmpInfo.type;
}
if (symType == ConstSet.RIGHT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(32);
pSkip(32);
}
return tmpInfo.type;
}
/**
* 值参数表分析方法
*
* @return 参数个数
*/
public static int valParaLst() {
InfoPane.write("valParaLst() is called!" + '\n');
int pCount = 0;
if (symType == ConstSet.RIGHT_PRENTHESES
|| symType == ConstSet.SEMICOLON)
return pCount;
exprs();
pCount++;
while (symType == ConstSet.COMMA) {
symType = Lexer.getSym();
exprs();
pCount++;
}
return pCount;
}
/**
* 无返回值函数调用语句分析方法
*/
public static void nrFcStnc() {
InfoPane.write("nrFcStnc() is called!" + '\n');
value = Lexer.getValue();
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(31);
pSkip(31);
}
CodeGen.gen(ConstSet.INT_INSTR, 0, 3);// 为控制区分配空间
int paras = valParaLst();
CodeGen.gen(ConstSet.STC_INSTR, 0, paras);
ItemInfo tmpInfo = SymTable.getFuncInfo();// 进入前已经查过表
// 参数个数相等
if (paras == tmpInfo.lvm)
CodeGen.gen(ConstSet.CAL_INSTR, 0, tmpInfo.address);
else {
ErrorManager.error(7);
pSkip(7);
return;
}
if (symType == ConstSet.RIGHT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(32);
pSkip(32);
}
}
/**
* 赋值语句分析方法
*
* @return 实际赋值类型,若查找变量失败或类型不匹配返回ConstSet.VOIDTYPE
*/
public static int evlStnc() {
InfoPane.write("evlStnc() is called!" + '\n');
ItemInfo tmpInfo;
// 查找变量,若找到合法的(本函数变量或参数或全局变量),类型检查,填入;否则错误
if (SymTable.search(value, crtLevel) != ConstSet.NOTFOUND) {
tmpInfo = SymTable.getEleInfo();
symType = Lexer.getSym();
symType = Lexer.getSym();
int tmpTp = exprs();
// 类型检查,如果类型相同
if (tmpInfo.type == tmpTp || tmpInfo.type != ConstSet.REAL
&& tmpTp != ConstSet.REAL) {
CodeGen.gen(ConstSet.STO_INSTR, tmpInfo.lvm, tmpInfo.address);
SymTable.setValue("1", tmpInfo.id - 1, 4);
}
// 如果待赋值变量类型更高,做隐式的类型转化
else if (tmpInfo.type < tmpTp) {
CodeGen.gen(ConstSet.TPC_INSTR, tmpInfo.type, 0);
CodeGen.gen(ConstSet.STO_INSTR, tmpInfo.lvm, tmpInfo.address);
SymTable.setValue("1", tmpInfo.id - 1, 4);
}
// 否则报错
else {
ErrorManager.error(5);
pSkip(5);
return ConstSet.VOIDTYPE;
}
} else {
ErrorManager.error(53);
pSkip(53);
return ConstSet.VOIDTYPE;
}
return tmpInfo.type;
}
/**
* 读语句分析方法
*
* @return 读入变量个数
*/
public static int readStnc() {
InfoPane.write("readStnc() is called!" + '\n');
int redCount = 0;
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
symType = Lexer.getSym();
if (symType == ConstSet.IDENTIFIER) {
value = Lexer.getValue();
int flg1 = SymTable.search(value, crtLevel);
// 在符号表中找到该名字
if (flg1 != ConstSet.NOTFOUND) {
ItemInfo tmpInfo = SymTable.getEleInfo();
// 是常量,错误
if (tmpInfo.kind.equals("const")) {
ErrorManager.error(60);
pSkip(60);
return redCount;
} else {
redCount++;
CodeGen.gen(ConstSet.RED_INSTR, tmpInfo.type, 0);
CodeGen.gen(ConstSet.STO_INSTR, tmpInfo.lvm,
tmpInfo.address);
SymTable.setValue("1", tmpInfo.id - 1, 4);
}
}
symType = Lexer.getSym();
while (symType == ConstSet.COMMA) {
symType = Lexer.getSym();
if (symType == ConstSet.IDENTIFIER) {
value = Lexer.getValue();
int flg2 = SymTable.search(value, crtLevel);
// 在符号表中找到该名字
if (flg2 != ConstSet.NOTFOUND) {
ItemInfo tmpInfo = SymTable.getEleInfo();
// 如果是常量
if (tmpInfo.kind.equals("const")) {
ErrorManager.error(60);
pSkip(60);
return redCount;
} else {
redCount++;
CodeGen
.gen(ConstSet.RED_INSTR, tmpInfo.type,
0);
CodeGen.gen(ConstSet.STO_INSTR, tmpInfo.lvm,
tmpInfo.address);
SymTable.setValue("1", tmpInfo.id - 1, 4);
}
}
symType = Lexer.getSym();
} else {
ErrorManager.error(6);
pSkip(6);
return redCount;
}
}
if (symType == ConstSet.RIGHT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(32);
pSkip(32);
}
;
} else {
ErrorManager.error(37);
pSkip(37);
}
} else {
ErrorManager.error(38);
pSkip(38);
}
return redCount;
}
/**
* 写语句分析方法
*
* @return 输出exprs类型,若只输出字符串则返回ConstSet.STRING
*/
public static int wrtStnc() {
InfoPane.write("wrtStnc() is called!" + '\n');
int wrtTp = ConstSet.VOIDTYPE;
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(45);
pSkip(45);
}
if (symType == ConstSet.STRING) {
value = Lexer.getValue();
CodeGen.gen(ConstSet.WRT_INSTR, 0, value);// 直接输出
symType = Lexer.getSym();
if (symType == ConstSet.COMMA) {
symType = Lexer.getSym();
wrtTp = exprs();
CodeGen.gen(ConstSet.WRT_INSTR, 1, 0);// 输出栈顶
} else if (symType == ConstSet.RIGHT_PRENTHESES) {
wrtTp = ConstSet.STRING;
} else {
ErrorManager.error(46);
pSkip(46);
}
} else {
wrtTp = exprs();
CodeGen.gen(ConstSet.WRT_INSTR, 1, 0);// 输出栈顶
}
if (symType == ConstSet.RIGHT_PRENTHESES) {
symType = Lexer.getSym();
} else {
ErrorManager.error(42);
pSkip(42);
}
return wrtTp;
}
/**
* 情况语句分析方法
*/
public static void csStnc() {
InfoPane.write("csStnc() is called!" + '\n');
symType = Lexer.getSym();
if (symType == ConstSet.LEFT_PRENTHESES) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -