📄 compile.java
字号:
error = 0; return 0; } //函数定义递归子程序 private int FuncD() { if (flag.type == Lex.IDSY) { if (checkfname(flag.word) != 0) { return error; } func = new Function(flag.word, Function.R_INT); lev++; } else { if (checkfname(flag.word) != 0) { return error; } func = new Function(lop.word, Function.R_VOID); lev++; next(); } funclist.add(func); if (Para() != 0) { error = ERR_PARALACK; return error; } //有参数时填map int i = 0; for (String a : func.para) { func.pmap.put(a, (i + 2) * 4); i++; } if (Complex() != 0) { return error; } //四元式成功生成后填map i = 1; for (String v : func.var) { func.vmap.put(v, i * 4); i++; } for (Four f : func.fourlist) { if (f.result != null && f.result.startsWith("@")) { func.vmap.put(f.result, i * 4); i++; } } checkret = 0; flab = 0; error = 0; return 0; } //主函数递归子程序 private int Main() { func = new Function("main", Function.R_VOID); lev++; funclist.add(func); if (lop.type != Lex.Lpar) { error = ERR_PARALACK; return error; } if (next() == ERR_OVERFLOW || lop.type != Lex.Rpar) { error = ERR_ABNORMAL; return error; } if (next() == ERR_OVERFLOW || Complex() != 0) { return error; } //四元式成功生成后填map int i = 1; for (String v : func.var) { func.vmap.put(v, i * 4); i++; } for (Four f : func.fourlist) { if (f.result != null && f.result.startsWith("@")) { func.vmap.put(f.result, i * 4); i++; } } flab = 0; error = 0; return 0; } //复合语句递归子程序 private int Complex() { if (lop.type != Lex.Lbra) { error = ERR_NOTBE; return error; } if (next() == ERR_OVERFLOW) { error = ERR_ABNORMAL; return error; } if (lop.type == Lex.CONST && ConstI() != 0) { return error; } if (VarI() == ERR_SAMECVP) { return error; } if (SentenceQ() != 0) { return error; } if (lop.type != Lex.Rbra) { error = ERR_RBRALACK; return error; } next(); error = 0; return 0; } //参数递归子程序 private int Para() { if (lop.type != Lex.Lpar) { error = ERR_NOTBE; return error; } if (next() == ERR_OVERFLOW || ParaT() != 0) { error = ERR_ABNORMAL; return error; } if (lop.type != Lex.Rpar) { error = ERR_ABNORMAL; return error; } next(); error = 0; return 0; } //参数表递归子程序 private int ParaT() { if (lop.type != Lex.INT) { error = 0; return 0; } do { if (next() == ERR_OVERFLOW || lop.type != Lex.IDSY) { error = ERR_ABNORMAL; return error; } func.para.add(lop.word); if (next() == ERR_OVERFLOW || lop.type != Lex.COMMA) { error = 0; return 0; } if (next() == ERR_OVERFLOW || lop.type != Lex.INT) { error = ERR_ABNORMAL; return error; } continue; } while (true); } //语句序列递归子程序 private int SentenceQ() { int f = 0; while (Sentence() == 0) { f++; } if (f == 0) { if (error == ERR_NOTBE) { error = ERR_SENTLACK; return error; } else { return error; } } if (error == ERR_NOTBE) { error = 0; return 0; } else { return error; } } //语句递归子程序 private int Sentence() { if (ConditionS() == 0) { error = 0; return 0; } if (error != ERR_NOTBE) { return error; } if (LoopS() == 0) { error = 0; return 0; } if (error != ERR_NOTBE) { return error; } if (lop.type == Lex.Lbra) { if (next() == ERR_OVERFLOW || SentenceQ() != 0) { return error; } if (lop.type != Lex.Rbra) { error = ERR_RBRALACK; return error; } next(); error = 0; return 0; } if (lop.type == Lex.IDSY) { //当以标识符开头时,可能是赋值语句也可能是函数调用,需要把标识符存入intime,再预读一个来判断 intime = lop.word; if (next() == ERR_OVERFLOW) { error = ERR_ABNORMAL; return error; } if (lop.type == Lex.EQUAL) { if (next() == ERR_OVERFLOW || EvaluateS() != 0) { return error; } if (lop.type != Lex.SEMICOLON) { error = ERR_SEMILACK; return error; } next(); error = 0; return 0; } if (lop.type == Lex.Lpar) { if (next() == ERR_OVERFLOW || Call() != 0) { return error; } if (lop.type != Lex.SEMICOLON) { error = ERR_SEMILACK; return error; } next(); error = 0; return 0; } error = ERR_ABNORMAL; return error; } if (ReturnS() == 0) { checkret++; if (lop.type != Lex.SEMICOLON) { error = ERR_SEMILACK; return error; } next(); error = 0; return 0; } if (error != ERR_NOTBE) { return error; } if (ReadS() == 0) { if (lop.type != Lex.SEMICOLON) { error = ERR_SEMILACK; return error; } next(); error = 0; return 0; } if (error != ERR_NOTBE) { return error; } if (WriteS() == 0) { if (lop.type != Lex.SEMICOLON) { error = ERR_SEMILACK; return error; } next(); error = 0; return 0; } if (error != ERR_NOTBE) { return error; } error = ERR_NOTBE; return error; } //条件语句递归子程序 private int ConditionS() { if (lop.type != Lex.IF) { error = ERR_NOTBE; return error; } if (next() == ERR_OVERFLOW || lop.type != Lex.Lpar) { error = ERR_ABNORMAL; return error; } if (next() == ERR_OVERFLOW || Condition() != 0) { return error; } addfour(JFALSE, func.name + "label" + flab, null, null); //不满足条件时跳转至本函数的第flab个标签处 String elsestart = func.name + "label" + flab; flab++; if (lop.type != Lex.Rpar) { error = ERR_ABNORMAL; return error; } if (next() == ERR_OVERFLOW || Sentence() != 0) { return error; } addfour(J, func.name + "label" + flab, null, null); String end = func.name + "label" + flab; flab++; addfour(STAMP, elsestart, null, null); //给函数fanc.name加第flab个标签 flab++; if (lop.type == Lex.ELSE) { if (next() == ERR_OVERFLOW || Sentence() != 0) { return error; } } addfour(STAMP, end, null, null); error = 0; return 0; } //循环语句递归子程序 private int LoopS() { if (lop.type != Lex.WHILE) { error = ERR_NOTBE; return error; } String start = func.name + "label" + flab; flab++; addfour(STAMP, start, null, null); //加标签(循环起始处) if (next() == ERR_OVERFLOW || lop.type != Lex.Lpar) { error = ERR_ABNORMAL; return error; } if (next() == ERR_OVERFLOW || Condition() != 0) { return error; } if (lop.type != Lex.Rpar) { error = ERR_ABNORMAL; return error; } addfour(JFALSE, func.name + "label" + flab, null, null); //当不满足条件是越过后面的语句到标签处 String end = func.name + "label" + flab; flab++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -