📄 compile.java
字号:
import java.util.LinkedList;/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.util.Vector;/** * @author lilac */public class Compile { //错误类型定义 public static final int ERR_OVERFLOW = 1; //溢出错误 public static final int ERR_ABNORMAL = 2; //非正常结束 public static final int ERR_SEMILACK = 3; //缺分号 public static final int ERR_PARALACK = 4; //缺参数 public static final int ERR_RBRALACK = 5; //缺右大括号 public static final int ERR_SENTLACK = 6; //没有语句 public static final int ERR_NOTBE = 7; //不是 public static final int ERR_SAMEFUNC = 8; //函数同名 public static final int ERR_SAMETOP = 9; //全局常量或变量同名 public static final int ERR_SAMECVP = 10; //局部常量或变量或参数同名 public static final int ERR_RETLACK = 11; //缺return public static final int ERR_NOTDEFINE = 12; //未定义使用变量或函数名 //四元式操作符 public static final int ADD = 1; public static final int SUB = 2; public static final int MUL = 3; public static final int DIV = 4; public static final int MOV = 5; public static final int STAMP = 6; public static final int J = 7; public static final int JFALSE = 8; public static final int CONDITION = 9; public static final int CMP = 10; public static final int PUSHPARA = 11; public static final int CALL = 12; public static final int PRINTF = 13; public static final int SCANF = 14; public static final int RETURN = 15; //字段定义 private LinkedList<Lexoutput> Llist; //词法输出表 private Lexoutput lop; //当前词法输出元素 private int lpos = 0; //词法输出表当前位置 private Lexoutput flag; //标志寄存 private int lev = 0; //当前层数 public Vector<Constpair> topconst = null; //全局常量数组 public Vector<String> topvar = null; //全局变量数组 public LinkedList<Function> funclist = null; //函数表 private Function func = null; //当前函数 private Four four = null; //当前四元式 private int flab = 0; //当前函数标签个数 private int ti = 1; //四元式中间结果符号角标 private String intime = null; //暂存器 public int error = 0; //错误类型 private int checkret = 0; //是否return标志 /** * 构造函数 */ public Compile(LinkedList<Lexoutput> Llist) { this.Llist = new LinkedList<Lexoutput>(Llist); this.topconst = new Vector<Constpair>(); this.topvar = new Vector<String>(); this.funclist = new LinkedList<Function>(); } /************************************************************************************************/// 取词法输出链中的下一个元素的方法 private int next() { if (++lpos < Llist.size()) { lop = new Lexoutput(Llist.get(lpos)); error = 0; return 0; } else { lop = new Lexoutput(); error = ERR_OVERFLOW; return error; } } //构造新四元式并把它加入到四元式链中去 private void addfour(int op, String first, String last, String result) { four = new Four(op, first, last, result); func.fourlist.add(four); } //检查函数名是否相同 private int checkfname(String fname) { int i = 0; while (i < funclist.size()) { if (fname.equals(funclist.get(i++).name)) { error = ERR_SAMEFUNC; return error; } } error = 0; return 0; } //检查全局常量及变量是否同名 private int checktop(String topname) { for (Constpair topcon : this.topconst) { if (topname.equals(topcon.name)) { error = ERR_SAMETOP; return error; } } for (String topv : this.topvar) { if (topname.equals(topv)) { error = ERR_SAMETOP; return error; } } error = 0; return 0; } //检查局部常量变量及参数是否同名 private int checkcvp(String name) { for (String p : func.para) { if (name.equals(p)) { error = ERR_SAMETOP; return error; } } for (Constpair c : func.con) { if (name.equals(c.name)) { error = ERR_SAMETOP; return error; } } for (String v : func.var) { if (name.equals(v)) { error = ERR_SAMETOP; return error; } } error = 0; return 0; }//错误类型输出 public String err_report(int error) { switch (error) { case 1: return "溢出错误"; case 2: return "非正常结束"; case 3: return "缺分号"; case 4: return "缺参数"; case 5: return "缺右大括号"; case 6: return "没有语句"; case 7: return "完全不是"; case 8: return "函数同名"; case 9: return "全局常量或变量同名"; case 10: return "局部常量或变量或参数同名"; case 11: return "缺return"; case 12: return "未定义使用变量或函数名"; } return "没错"; } /****************************************************************************************************///<程序>递归总程序 public int Prog() { lop = new Lexoutput(Llist.get(lpos)); flag = new Lexoutput(); if (lop.type == Lex.CONST) { //若是const开头,进入ConstI的子程序 if (ConstI() != 0) { return error; } } do { if (lop.type == Lex.INT) { //当以int 开头时 if (next() == 0 && lop.type == Lex.IDSY) { //读入下一个不为空的词,若为标识符时 flag = lop; //用flag存下当前的标识符 if (next() == 0 && (lop.type == Lex.COMMA || lop.type == Lex.SEMICOLON)) { //若成功读入下一个词且为逗号或分好时交给VarI子程序 if (VarI() == 0) { flag.type = -1; //确实为变量后清空flag,进入下一循环 flag.word = null; continue; } else { //不是变量说明时报错 return error; } } else { if (FuncD() == 0) { //当确定为函数定义时 if (checkret != 0) { error = ERR_RETLACK; return error; } flag.type = -1; flag.word = null; continue; } else { return error; } } } else { error = ERR_ABNORMAL; return error; } } else if (lop.type == Lex.VOID) { //当以void开头时 flag = lop; if (next() == 0 && lop.type == Lex.MAIN) { //成功读入下一词,且为main时,交给Main子程序 if (next() == ERR_OVERFLOW || Main() != 0) { return error; } error = 0; return 0; } else { //不是main时,交给FuncD子程序 if (FuncD() == 0) { flag.type = -1; flag.word = null; continue; } else { return error; } } } error = ERR_NOTBE; return error; } while (true); } //<常量说明部分>递归子程序 public int ConstI() { if (next() == ERR_OVERFLOW || ConstD() != 0) { //const 后面不是<ConstD>,错误 return error; } while (lop.type != Lex.SEMICOLON) { if (lop.type == Lex.COMMA) { if (next() == ERR_OVERFLOW || ConstD() != 0) { //逗号后面不是<ConstD>,错误 return error; } } else { error = ERR_SEMILACK; return error; } } //遇到分号前一直没有错误,可以接受 next(); error = 0; return 0; }// <常量定义>递归子程序 public int ConstD() { if (lop.type == Lex.IDSY) { String topname = new String(); String conname = new String(); if (lev == 0) { if (checktop(lop.word) != 0) { error = ERR_SAMETOP; return error; } topname = lop.word; //把全局常量名存入全局常量表 } else { //局部常量处理 if (checkcvp(lop.word) != 0) { error = ERR_SAMECVP; return error; } conname = lop.word; } if (next() == 0 && lop.type == Lex.EQUAL) { if (next() == 0 && lop.type == Lex.INTEGER) { if (lev == 0) { topconst.add(new Constpair(topname, lop.word)); //把全局常量值存入全局常量表 } else { //局部常量处理 func.con.add(new Constpair(conname, lop.word)); } next(); error = 0; return 0; } else { error = ERR_ABNORMAL; return error; } } } error = ERR_NOTBE; return error; }//变量说明部分递归子程序 public int VarI() { if (lev == 0) { //第零层为全局变量 if (checktop(flag.word) != 0) { error = ERR_SAMETOP; return error; } topvar.add(flag.word); while (lop.type != Lex.SEMICOLON) { if (next() == ERR_OVERFLOW || lop.type != Lex.IDSY) { error = ERR_ABNORMAL; return error; } if (checktop(lop.word) != 0) { error = ERR_SAMETOP; return error; } topvar.add(lop.word); if (next() == ERR_OVERFLOW || (lop.type != Lex.COMMA && lop.type != Lex.SEMICOLON)) { error = ERR_SEMILACK; return error; } } next(); error = 0; return 0; } //第一层为局部变量 if (lop.type != Lex.INT) { error = ERR_NOTBE; return error; } while (lop.type != Lex.SEMICOLON) { if (next() == ERR_OVERFLOW || lop.type != Lex.IDSY) { error = ERR_ABNORMAL; return error; } //局部变量处理 if (checkcvp(lop.word) != 0) { error = ERR_SAMECVP; return error; } func.var.add(lop.word); if (next() == ERR_OVERFLOW || (lop.type != Lex.COMMA && lop.type != Lex.SEMICOLON)) { error = ERR_SEMILACK; return error; } } next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -