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

📄 compile.java

📁 北航编译原理课程设计成果——一个扩充的C0文法编译器
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		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++;		if(next()==ERR_OVERFLOW||Sentence()!=0)		{			return error;		}		addfour(J,start,null,null);		addfour(STAMP,end,null,null);						//加标签(循环结束处)		flab++;		error=0;		return 0;	}	//赋值语句递归子程序	private int EvaluateS()	{		String presult=intime;		if(Exp()!=0)		{			if(error==ERR_NOTBE)			{				error=ERR_ABNORMAL;				return error;			}			else			{				return error;			}		}		addfour(MOV,intime,null,presult);								 //把当前表达式的值赋给前一个表达式的结果		error=0;		return 0;	}	//调用语句递归子程序	private int Call()	{		String funcname=intime;		if(checkFuncName(funcname)==0)		{	//未定义的函数名			error=ERR_NOTDEFINE;			return error;		}		if(VparaT()!=0)		{			return error;		}		if(lop.type!=Lex.Rpar)		{			error=ERR_ABNORMAL;			return error;		}		addfour(CALL,funcname,null,null);		intime="@teax";		next();		error=0;		return 0;	}	//返回语句递归子程序	private int ReturnS()	{		if(lop.type!=Lex.RETURN)		{			error=ERR_NOTBE;			return error;		}		if(next()==0&&lop.type==Lex.Lpar)		{			if(next()==ERR_OVERFLOW||Exp()!=0)			{				return error;			}			if(lop.type!=Lex.Rpar)			{				error=ERR_ABNORMAL;				return error;			}			addfour(RETURN,intime,null,null);			next();			error=0;			return 0;		}		addfour(RETURN,null,null,null);		error=0;		return 0;	}	//读语句递归子程序	private int ReadS()	{		if(lop.type!=Lex.SCANF)		{			error=ERR_NOTBE;			return error;		}		if(next()==ERR_OVERFLOW||lop.type!=Lex.Lpar)		{			error=ERR_ABNORMAL;			return error;		}		if(next()==ERR_OVERFLOW||lop.type!=Lex.IDSY)		{			error=ERR_ABNORMAL;			return error;		}		String idsy=new String(lop.word);		if(next()==ERR_OVERFLOW||lop.type!=Lex.Rpar)		{			error=ERR_ABNORMAL;			return error;		}		addfour(SCANF,idsy,null,null);		next();		error=0;		return 0;	}	//写语句递归子程序	private int WriteS()	{		if(lop.type!=Lex.PRINTF)		{			error=ERR_NOTBE;			return error;		}		if(next()==ERR_OVERFLOW||lop.type!=Lex.Lpar)		{			error=ERR_ABNORMAL;			return error;		}		if(next()==ERR_OVERFLOW)		{			error=ERR_ABNORMAL;			return error;		}		if(lop.type==Lex.STRING)		{			String string=lop.word;			if(next()==ERR_OVERFLOW||lop.type!=Lex.COMMA)			{				addfour(PRINTF,string,null,"0");  //只有string,设为0类型				if(lop.type!=Lex.Rpar)				{					error=ERR_ABNORMAL;					return error;				}				next();				error=0;				return 0;			}			if(next()==ERR_OVERFLOW||Exp()!=0)			{				return error;			}			addfour(PRINTF,string,intime,"2");	//string,exp都有,设为2类型			if(lop.type!=Lex.Rpar)			{				error=ERR_ABNORMAL;				return error;			}			next();			error=0;			return 0;		}		if(Exp()!=0)		{			return error;		}		addfour(PRINTF,null,intime,"1");	 //只有exp,设为1类型		if(lop.type!=Lex.Rpar)		{			error=ERR_ABNORMAL;			return error;		}		next();		error=0;		return 0;	}	//条件递归子程序	private int Condition()	{		if(Exp()!=0)		{			return error;		}		if(lop.type < Lex.G)		{											 //当不是关系运算符时			addfour(CMP,intime,"0",null);					   //若条件是表达式,则让表达式结果与0比较			addfour(CONDITION,"28",null,null);				  //需满足的条件是不等于零,Lex.NE=28			error=0;			return 0;		}		String presult=intime;		String relate=new Integer(lop.type).toString();					//取关系运算符		if(next()==ERR_OVERFLOW||Exp()!=0)		{			return error;		}		addfour(CMP,presult,intime,null);					   //比较两表达式		addfour(CONDITION,relate,null,null);						  //需满足的条件是relate存的条件		error=0;		return 0;	}	//表达式递归子程序	private int Exp()	{		int symbel=0;		int op=1;		if(lop.type==Lex.MINUS||lop.type==Lex.ADD)		{			symbel++;								//当有符号时op对应改变			op=lop.type-12;			next();		}		if(Item()!=0)		{			return error;		}		if(symbel>0)		{			addfour(op,"0",intime,"@t"+ti);					   //第一个项的结果与零做加或减			 			intime="@t"+ti;			ti++;		}		while(lop.type==Lex.ADD||lop.type==Lex.MINUS)		{			String presult=intime;						  //存前一项的结果			op=lop.type-12;			if(next()==ERR_OVERFLOW||Item()!=0)			{				if(error==ERR_NOTBE)				{					error=ERR_ABNORMAL;					return error;				}				else				{					return error;				}			}			addfour(op,presult,intime,"@t"+ti);					   //前一项的结果与该项结果做加或减 			intime="@t"+ti;			ti++;		}		if(intime.equals("@teax"))		{			addfour(Compile.MOV,"@teax",null,"@t"+ti);			intime="@t"+ti;			ti++;		}		error=0;		return 0;	}	//项递归子程序	private int Item()	{		int op;		if(Factor()!=0)		{			return error;		}		String presult=intime;			//把前一个因子的结果存为presult		while (lop.type==Lex.MUL||lop.type==Lex.DIV)		{			op=lop.type-12;			  //把元算符号存入op			if(next()==ERR_OVERFLOW)			{				error=ERR_ABNORMAL;				return error;			}			if(Factor()!=0)			{				if(error==ERR_NOTBE)				{					error=ERR_ABNORMAL;					return error;				}				else				{					return error;				}			}			addfour(op,presult,intime,"@t"+ti);			intime="@t"+ti;			ti++;			presult=intime;		}		error=0;		return 0;	}	//因子递归子程序	private int Factor()	{		int yon=0;   //是否有常量的标志位		if(lop.type==Lex.IDSY)		{			   //以标识符开头的要把标识符存入intime,再预读一个,区分出是函数调用还是单个标识符。			Lexoutput idsy=lop;			if(next()==ERR_OVERFLOW||lop.type!=Lex.Lpar)			{   //预读的一个不是括号就返回标识符,是括号就判断是否为call				//先判断是否有const名,若有则替换为对应的数值				for(Constpair c:func.con)				{					if(c.name.equals(idsy.word))					{						intime=c.value;						yon++;					}				}				for(String para:func.para)				{					if(para.equals(idsy.word))					{						intime=idsy.word;						yon++;					}				}				for(String var:func.var)				{					if(var.equals(idsy.word))					{						intime=idsy.word;						yon++;					}				}				for(Constpair c:this.topconst)				{					if(c.name.equals(idsy.word)&&yon==0)					{						intime=c.value;						yon++;					}				}				for(String var:this.topvar)				{					if(var.equals(idsy.word)&&yon==0)					{						intime=idsy.word;						yon++;					}				}				if(yon==0)				{					 //未定义的变量,报错	 					error=ERR_NOTDEFINE;					return error;				}				error=0;				return 0;			}		   //有(一定是函数调用			intime=idsy.word;			if(next()==ERR_OVERFLOW||Call()!=0)			{				return error;			}			error=0;			return 0;		}		if(lop.type==Lex.Lpar)		{ //当第一个是(时,判断是否是(Exp)形式的			if(next()==ERR_OVERFLOW||Exp()!=0)			{				return error;			}			if(lop.type!=Lex.Rpar)			{				error=ERR_ABNORMAL;				return error;			}			next();			error=0;			return 0;		}		if(lop.type==Lex.INTEGER)		{	 //若为整数就直接返回整数			intime=lop.word;			next();			error=0;			return 0;		}		error=ERR_NOTBE;		return error;	}	//值参数表递归子程序	private int VparaT()	{		String[] vparat=new String[50];		int i=0;		if(Exp()!=0)		{			error=0;			return 0;		}		vparat[i++]=intime;		while (lop.type==Lex.COMMA)		{			if(next()==ERR_OVERFLOW||Exp()!=0)			{				return error;			}			vparat[i++]=intime;		}		for(i=i-1;i>=0;i--)		{			addfour(PUSHPARA,vparat[i],null,null);	  //倒着推参数		}		error=0;		return 0;	}}

⌨️ 快捷键说明

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