⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compile.java.svn-base

📁 北航编译原理课程设计成果——一个扩充的C0文法编译器
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
package cn.edu.buaa.scse.liyi.test;import java.util.LinkedList;/* * To change this template,choose Tools | Templates * and open the template in the editor. */import java.util.Vector;/** *  * @author liyi */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 Quadruple quadruple=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)	{		quadruple=new Quadruple(op,first,last,result);		func.fourlist.add(quadruple);	}	//检查函数名是否相同	private int checkFuncName(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 Program()	{		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();		error=0;		return 0;	}	//函数定义递归子程序	private int FuncD()	{		if(flag.type==Lex.IDSY)		{			if(checkFuncName(flag.word)!=0)			{				return error;			}			func=new Function(flag.word,Function.R_INT);			lev++;		}		else		{			if(checkFuncName(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(Quadruple 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(Quadruple 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;			}		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -