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

📄 yufa_insidecifa.java

📁 java语言 编写 的 语法分析器可以自动 生成中间代码 四元式
💻 JAVA
字号:
/*
 * Created on 2005-5-22
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import java.io.*;
public class YuFa_insideCiFa{
	int singleXFlag=1;
	private int counterObj;
	private String expressFour="",returnObj;
	private String expression="",errMsg="error详情:";
	private boolean flagOver;
	private boolean yufaFlag=true;
	private String type,preType;
	private String typeValue="",preTypeValue;
	private String inputFile;
	private int expressionValue;
	FileReader fr;
	BufferedReader br;
	CiFa2 cf=new CiFa2();
	
	YuFa_insideCiFa() //读入文件
		throws java.io.IOException{
		inputFile="Target.txt";
		int counter=0;
			
		fr= new FileReader(inputFile);
		br=new BufferedReader(fr);
	}
	protected void finalize()
		throws java.io.IOException{
		br.close();
	}
	

	private boolean readNext()
	throws java.io.IOException{
		String tempStr=cf.getSym();
		System.out.println("YuFa.readNext()中的tempStr:"+tempStr);
		if(tempStr!=null){
			
			int i=tempStr.indexOf(',');
			String tempStr1=tempStr.substring(1,i);
			typeValue=tempStr1;
			String tempStr2=tempStr.substring(i+1,tempStr.length()-1);
			type=tempStr2;
			System.out.println("YuFa.readNext()方法调用CiFa2.getSym(),返回一行内容:"+tempStr);
			System.out.print("已经分解成 Type:"+type+"  TypeValue:"+typeValue+"  ");
		}
		else{
			type="#";
			typeValue="#";
			return false;
		}
		
		//System.out.println(type);
		//System.out.println(typeValue);
		//System.exit(1);
		return true;
	}
	private int bds() throws java.io.IOException{//表达式
		System.out.println("inside bds()");
		int bdsValue=0;
		String a,b;
		//System.out.println(type.equals("plus"));
		//System.out.println(type);
		if(type.equals("plus")&&!flagOver){
			
			advance();
			bdsValue=x();
			a=returnObj;
			
		}
		else if(type.equals("minus")&&!flagOver){
			advance();
			bdsValue=x();
			bdsValue=0-bdsValue;
			a=returnObj;
		}else {
			bdsValue=x();
			a=returnObj;
		}
		
		while(true){
			if(!type.equals("plus")&&!type.equals("minus"))
				break;
			singleXFlag=1;
			if(type.equals("plus")&&!flagOver){
				
				jfysf();
				bdsValue=bdsValue+x();
				b=returnObj;
				emit("+",a,b,newobj(1));
			}
			else if(type.equals("minus")&&!flagOver){
				jfysf();
				bdsValue=bdsValue-x();
				b=returnObj;
				emit("-",a,b,newobj(1));
			}
			a=newobj(0);
		}
		returnObj=newobj(0);

		return bdsValue;
	}
	private void bsf() throws java.io.IOException{//标示符
		System.out.println("inside bsf()");
		if(!flagOver){
			advance();
		
			while(type.equals("ident") || type.equals("number")){
				advance();
			}
		}
	}

	private int x() throws java.io.IOException{//项
		System.out.println("inside x()");
		
		int xValue=0;
		String a,b;
		xValue=yz(1);
		a=returnObj;
		while(true){
			//System.out.println(!type.equals("times"));
			//System.exit(1);
			if(!type.equals("slash")&&!type.equals("times")){
				break;
			}
			singleXFlag=0;
			if(type.equals("slash")){
				cfysf();
				xValue=xValue/yz(0);
				b=returnObj;
				emit("/",a,b,newobj(1));
			}else if(type.equals("times")){
				cfysf();
				xValue=xValue*yz(0);
				b=returnObj;
				emit("*",a,b,newobj(1));				
			}

			
		}
		if(singleXFlag==1){
			//System.out.println("sssssss1");
			returnObj=String.valueOf(xValue);
		}else{
			returnObj=newobj(0);
			//System.out.println("sssssss2");
		}
		
		return xValue;
	}
	
	private int yz(int xFlag) throws java.io.IOException{//因子
		System.out.println("inside yz()");
		int yzValue=1;
		if(type.equals("ident")&&!flagOver){
			bsf();
		}
		else if(type.equals("number")&&!flagOver){
			returnObj=typeValue;
			yzValue=Integer.parseInt(typeValue); 
			advance();
			
		}
		else{
			if(type.equals("lparen")&&!flagOver){
				advance();
				yzValue=bds();
				//returnObj=newobj(1);
				if(type.equals("rparen")&&!flagOver){
					advance();
					singleXFlag=0;
				}else{
					error("在符号'"+typeValue+"'处有错误  可能期盼一个左括号')'");
				}
			}else if(xFlag==1){
				error("在符号'"+typeValue+"'之前应该跟项 或者 期盼一个左括号'('");
				//System.exit(1);
			}
			else{
				error("在符号'"+typeValue+"'之前应该跟因子 或者 期盼一个左括号'('");
			}
		}
		
		return yzValue;
	}
	private void jfysf()//加法运算符
		throws java.io.IOException{
		System.out.println("inside jfysf()");
		if(type.equals("plus")&&!flagOver){
			advance();
		}
		else if(type.equals("minus")&&!flagOver){
			advance();
		}
		
	}

	private void cfysf() throws java.io.IOException{//乘法运算符
		System.out.println("inside cfysf()");
		if(type.equals("times")&&!flagOver){
			advance();
		}
		else if(type.equals("slash")&&!flagOver){
			advance();
		}
		
	}
	
	private void wfhzs() throws java.io.IOException{//无符号整数
		System.out.println("inside wfhzs()");
		sz();
	}

	private void sz()//数字
		throws java.io.IOException{
		System.out.println("inside sz()");
		if(type.equals("number")&&!flagOver){
			advance();
		} 
		
	}
	
	private void emit(String op,String a,String b,String obj){//生成4元式
		expressFour+="("+op+","+a+","+b+","+obj+")"+"\n";
	}
	
	private String newobj(int flag){
		String temp="";
		if(flag==1){
			counterObj++;
			temp="T" +counterObj;
			//System.out.println("YuFa.newobj(1)创建了目标变量"+temp);
		}else if(flag==0) {
			temp="T"+counterObj;
			//System.out.println("YuFa.newobj(0)使用了一次当前目标变量"+temp);
		}

		return temp;
	}
	
	private void advance()
		throws java.io.IOException{//进一步
		expression+=typeValue;
		boolean condition=readNext();
		//System.out.println(condition);
		if(condition){
			System.out.println("继续执行advance(),现在指向"+typeValue);
		}
		else{
			flagOver=true;//表示结束
			System.out.println(inputFile+"文件读完");
		}
	} 
	
	private void error(String errmsg) throws java.io.IOException{
		yufaFlag=false;
		System.out.println(errmsg);
		errMsg+=errmsg;
		expression+="  <此处error> ";
		while(!flagOver){
			advance();
		}
	}
	
	public String analays() throws java.io.IOException{
		//表达式部分语法分析过程开始
		System.out.println("\n########## 下面输出语法调用词法的调试信息 #########\n");
		advance();
		expressionValue=bds();
		if(!type.equals("#")){
			error("错误,在符号"+typeValue+"附近,可能没有跟一个项");
		}
		
		System.out.println("\n##########       调试信息输出结束       #########\n\n\n\n\n########## 以下为表达式语法分析结果 #########\n");
		if(yufaFlag){
			System.out.println("表达式为: "+expression+"#");
			System.out.println("分析结果:此表达式语法正确!");
			System.out.println("表达式值为:"+expressionValue);
			System.out.println("\n4元式为:\n"+expressFour);
			System.out.println("Expression:="+newobj(0)+" Over!");
		}

		else {
			System.out.println(expression+"#");
			System.out.println("分析结果:此表达式语法有错误!");
			System.out.println(errMsg);
		}
		System.out.println("\n########## 表达式语法分析结果结束 ###########\n\n\n\n\n");	
		//分析过程结束
		System.out.println("方法Yufa.analays() 成功执行!");
		return "1";
	}
}

⌨️ 快捷键说明

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