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

📄 yacc.java.svn-base

📁 北航编译原理课程设计成果——一个扩充的C0文法编译器
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
			addQuater(CONDITION,"28",null,null);				//需满足的条件是不等于零,Lex.NE=28
			return true;
		}
		else if(lop.getType()<Lex.G)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		String presult=register;
		String relate=new Integer(lop.getType()).toString();	//取关系运算符

		if(!next()||!parseExpression())
		{
			return false;
		}

		addQuater(CMP,presult,register,null);					//比较两表达式
		addQuater(CONDITION,relate,null,null);					//需满足的条件是relate存的条件

		return true;
	}
	
	/**
	 * <条件语句>
	 * @param jmpstart
	 * @param jmpend
	 * @return
	 */
	private boolean parseConditionStatement(String jmpstart,String jmpend)
	{
		if(!next()||lop.getType()!=Lex.Lpar)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		if(!next()||!parseCondition())
		{
			return false;
		}

		funclab++;
		addQuater(JFALSE,curfunc.getName()+"label"+funclab,null,null);			//不满足条件时跳转至本函数的第funclab个标签处
		String elseStart=curfunc.getName()+"label"+funclab;

		if(lop.getType()!=Lex.Rpar)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		if(!next()||!parseSentence(jmpstart,jmpend))
		{
			return false;
		}
		funclab++;
		addQuater(J,curfunc.getName()+"label"+funclab,null,null);
		String end=curfunc.getName()+"label"+funclab;
		
		addQuater(STAMP,elseStart,null,null);				 //给函数fanc.name加第funclab个标签

		if(lop.getType()==Lex.ELSE)
		{

			if(!next()||!parseSentence(jmpstart,jmpend))
			{
				return false;
			}
		}
		addQuater(STAMP,end,null,null);
		return true;
	}
	
	/**
	 * <循环语句>
	 * @return
	 */
	private boolean parseLoopStatement()
	{
		funclab++;
		String start=curfunc.getName()+"label"+funclab;
		addQuater(STAMP,start,null,null);						//加标签(循环起始处)
		
		
		if(!next()||lop.getType()!=Lex.Lpar)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		if(!next()||!parseCondition())
		{
			return false;
		}
		if(lop.getType()!=Lex.Rpar)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_RBRALACK,lop.getLine(),null));
			return false;
		}

		funclab++;
		addQuater(JFALSE,curfunc.getName()+"label"+funclab,null,null);		//当不满足条件时越过后面的语句到标签处
		String end=curfunc.getName()+"label"+funclab;
		
		if(next()&&parseSentence(start,end))
		{
			addQuater(J,start,null,null);
			addQuater(STAMP,end,null,null);								//加标签(循环结束处)
			
			return true;
		}
		return false;
	}
	
	/**
	 * <赋值语句>
	 * @return
	 */
	private boolean parseEvaluation()
	{
		String result=register;
		boolean def=false;							//是否定义过的标志位
		for(Constant con:global_const)
		{
			if(con.getName().equals(result))
			{
				error=true;
				errorList.add(Error.report(Error.ERR_CONSTASSIGN,lop.getLine(),result));
				return false;
			}
		}
		for(Constant con:curfunc.getConst())
		{
			if(con.getName().equals(result))
			{
				error=true;
				errorList.add(Error.report(Error.ERR_CONSTASSIGN,lop.getLine(),result));
				return false;
			}
		}
		for(String para:curfunc.getPara())
		{
			if(para.equals(result))
			{
				def=true;
				break;
			}
		}
		for(String var:curfunc.getVar())
		{
			if(var.equals(result)||def)
			{
				def=true;
				break;
			}
		}
		for(String var:this.global_var)
		{
			if(var.equals(result)||def)
			{
				def=true;
				break;
			}
		}
		if(!def)
		{					 //未定义的变量,报错	 
			error=true;
			errorList.add(Error.report(Error.ERR_NOTDEFINE,lop.getLine(),result));
			return false;
		}
		if(!parseExpression())
		{
			return false;
		}
		addQuater(MOV,register,null,result);					//把当前表达式的值赋给前一个表达式的结果
		return true;
	}
	
	/**
	 * <子函数调用语句>
	 * @return
	 */
	private boolean parseCall()
	{
		String funcName=register;
		if(!checkFuncDefined(funcName,lop.getLine()))
		{						//未定义的函数名
			return false;
		}
		if(!parseValueParaTable())
		{
			return false;
		}
		if(lop.getType()!=Lex.Rpar)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		addQuater(CALL,funcName,null,null);
		register="@teax";
		next();

		return true;
	}
	
	/**
	 * <读语句>
	 * @return
	 */
	private boolean parseRead()
	{
		if(((!next()||lop.getType()!=Lex.Lpar))||(!next()||lop.getType()!=Lex.IDSY))
		{
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		
		String idsy=new String(lop.getWord());
			
		if(!next()||lop.getType()!=Lex.Rpar)
		{
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		addQuater(SCANF,idsy,null,null);
		
		if(next()&&lop.getType()==Lex.SEMICOLON)
		{
			next();
			return true;
		}
		else
		{
			error=true;
			errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
			return false;
		}
	}
	
	/**
	 * <写语句>
	 * @return
	 */
	private boolean parseWrite()
	{
		if(!next()||lop.getType()!=Lex.Lpar)
		{
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		if(!next())
		{
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		if(lop.getType()==Lex.STRING)
		{
			String str=lop.getWord();
			if(!next()||lop.getType()!=Lex.COMMA)
			{
				addQuater(PRINTF,str,null,"0");					//只有字符串,设为0类型
				if(lop.getType()!=Lex.Rpar)
				{
					errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
					return false;
				}
				if(next()&&lop.getType()==Lex.SEMICOLON)
				{
					next();
					return true;
				}
				else
				{
					error=true;
					errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
					return false;
				}
			}
			if(!next()||!parseExpression())
			{
				return false;
			}
			addQuater(PRINTF,str,register,"2");	//string,exp都有,设为2类型
			if(lop.getType()!=Lex.Rpar)
			{
				errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
				return false;
			}
			if(next()&&lop.getType()==Lex.SEMICOLON)
			{
				next();
				return true;
			}
			else
			{
				error=true;
				errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
				return false;
			}
			
		}
		if(!parseExpression())
		{
			return false;
		}
		addQuater(PRINTF,null,register,"1");	 //只有exp,设为1类型
		if(lop.getType()!=Lex.Rpar)
		{
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			return false;
		}
		if(next()&&lop.getType()==Lex.SEMICOLON)
		{
			next();
			return true;
		}
		else
		{
			error=true;
			errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
			return false;
		}
	}
	
	/**
	 * <返回语句>
	 * @return
	 */
	private boolean parseReturn()
	{
		if(next())
		{
			if(lop.getType()==Lex.Lpar)
			{
				if(!next()||!parseExpression())
				{
					return false;
				}
				if(lop.getType()!=Lex.Rpar)
				{
					error=true;
					errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
					return false;
				}
				addQuater(RETURN,register,null,null);
			
				if(!next()||lop.getType()!=Lex.SEMICOLON)
				{
					error=true;
					errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
					return false;
				}
				next();
			
				return true;
			}
			else if(lop.getType()!=Lex.SEMICOLON)
			{
				if(!parseExpression())
				{
					return false;
				}
				
				addQuater(RETURN,register,null,null);
			
				if(lop.getType()!=Lex.SEMICOLON)
				{
					error=true;
					errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
					return false;
				}
				next();
			
				return true;
			}
		}
		
		if(lop.getType()==Lex.SEMICOLON)
		{
			addQuater(RETURN,null,null,null);
			next();
			return true;
		}
		else
		{
			error=true;
			errorList.add(Error.report(Error.ERR_SEMILACK,lop.getLine(),null));
			return false;
		}
	}
	
/********************************************************************************************/	
	
	/**
	 * <程序>
	 */
	public void parseProgram()
	{
		lop=new Output(lexList.get(lxpos));
		flag=new Output();
		while(lop.getType()==Lex.CONST)
		{													//若是const开头,进入parseConst()的子程序
			if(!parseConst())
			{
				return;
			}
		}
		do
		{
			if(lop.getType()==Lex.INT)
			{												//当以int开头时
				if(next()&&lop.getType()==Lex.IDSY)
				{											//读入下一个不为空的词,若为标识符时
					flag=lop;								//用flag存下当前的标识符
					if(next()&&(lop.getType()==Lex.COMMA||lop.getType()==Lex.SEMICOLON))
					{										//若成功读入下一个词且为逗号或分号时交给parseVar()子程序
						if(parseVar())
						{
							flag.setType(-1);				//确实为变量后清空flag,进入下一循环
							flag.setWord(null);
							flag.setLine(-1);
							continue;
						}
						else
						{
							//不是变量说明时跳出
							return;
						}
					}
					else if(lop.getType()==Lex.Lpar)
					{
						if(parseFunc())
						{									//当确定为函数定义时
							if(!checkret)
							{
								error=true;
								errorList.add(Error.report(Error.ERR_RETLACK,lop.getLine(),null));
								return;
							}
							flag.setType(-1);
							flag.setWord(null);
							flag.setLine(-1);
							continue;
						}
						else
						{
							return;
						}
					}
					else
					{
						error=true;
						errorList.add(Error.report(Error.ERR_NOTBE,lop.getLine(),"全局变量说明"));
						return;
					}
				}
				else
				{
					return;
				}
			}
			else if(lop.getType()==Lex.VOID)
			{				//当以void开头时
				flag=lop;
				if(next()&&lop.getType()==Lex.MAIN)
				{							 				//成功读入下一词,且为main时,调用parseMain()子程序
					if(next()&&parseMain(Function.R_VOID))
					{
						if(next())
						{
							error=true;
							errorList.add(Error.report(Error.ERR_UNRECEIVE,lop.getLine(),null));
						}
						return;
					}
					return;
				}
				else
				{											//不是main时,调用parseFunc()子程序
					if(parseFunc())
					{
						flag.setLine(-1);
						flag.setType(-1);
						flag.setWord(null);
						continue;
					}
					else
					{
						return;
					}
				}
			}
			else if(lop.getType()==Lex.MAIN)
			{
				flag=lop;
				if(next()&&parseMain(-1))
				{
					if(next())
					{
						error=true;
						errorList.add(Error.report(Error.ERR_UNRECEIVE,lop.getLine(),null));
					}
					return;
				}
				return;
			}
			
			error=true;
			errorList.add(Error.report(Error.ERR_ABNORMAL,lop.getLine(),null));
			if(funcList.isEmpty()||!"main".equals(funcList.getLast().getName()))
			{
				errorList.add(Error.report(Error.ERR_MAINLACK,lop.getLine(),null));
			}
			
			return;
		}
		while(true);
	}

}

⌨️ 快捷键说明

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