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

📄 compiler.java

📁 类c语言编译器
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		attriStack=new Stack();
		groupVector=new Vector();
	}
	//===测试用
	void displayAttriStack()
	{
		System.out.println("=============attriStack============\n");
		for(int i=0;i<attriStack.size();i++)
		{
			attribution attri=new attribution();
			attri=(attribution)attriStack.elementAt(i);
			System.out.println("(place:"+attri.place+","+"trueList:"+attri.trueList+","+"falseList:"+attri.falseList+","+"nextList:"+attri.nextList+"\n");
		}
	}
	//===测试用
	void displayGroup()
	{
		System.out.println("============group==============\n");
		for(int i=0;i<groupVector.size();i++)
		{
			fourGroup fGroup=new fourGroup();
			fGroup=(fourGroup)groupVector.elementAt(i);
			System.out.println(fGroup.label+":("+fGroup.operate+","+fGroup.opFirst+","+fGroup.opSecond+","+fGroup.result+")");
		}
		
	}
	//===产生新的标号
	int newlabel()
	{
		return(newLabel++);
	}
	//===产生新的变量
	String newtemp()
	{
		return ("temp"+Integer.toString(newtempLabel++));
	}
	//===语法分析及语义分析
	void analyse(boolean isFormulaStep,boolean isGroupStep)throws ArrayIndexOutOfBoundsException 
	{
		int staIndex=0;	
		int tokIndex=0;
		int tokTop=0;
		int staTop=0;
		int actionTemp=0;	//===存放从表中查到的状态编号
		int gotoTemp=0;		//===产生式编号
		int genLength=0;	//===产生式长度
		int gotoIndex=0;	//===goto表索引
		isStep=isGroupStep;
		
		while(true)
		{
			staTop=Integer.parseInt((String)staStack.lastElement());
			tokTop=Integer.parseInt((String)symTable.tokenCode.elementAt(tokIndex));
			actionTemp=lrTable.lrTable[staTop][tokTop];
			//================================================================================
			//=======================在action表中查到0表示出错[语法错误]======================
			//================================================================================
			if(actionTemp==0)
			{
				errorLine=Integer.parseInt( (String)symTable.tokenLine.elementAt(tokIndex));
				errorColumn=Integer.parseInt ((String)symTable.tokenColumn.elementAt(tokIndex));
				isError=true;
				return;
			}
			switch(actionTemp/100)
			{
			case 0:
				break;
			case 1://===在action表中查到的是s则进栈
				symStack.push((String)symTable.tokenSym.elementAt(tokIndex)); 
				staStack.push(Integer.toString(actionTemp-100));
				tokIndex++;
				break;
			case 2://===在action表中查到的是r则归约
				genLength=((String)genFormu.genOut.elementAt(actionTemp-200)).length();
				//===输出完成产生式
				if(isFormulaStep)
				{
					int dlgResult =MessageBox.show((String)genFormu.genAll.elementAt(actionTemp-200),"产生式",1);
					if (dlgResult == DialogResult.CANCEL)
					{
						isFormulaStep=false;
					}
				}
				formulaVector.addElement((String)genFormu.genAll.elementAt(actionTemp-200));
				switch(actionTemp%200)
				{
				case 1:s1();
					break;
				case 2:c2();
					break;
				case 3:c3();
					break;
				case 4:d4();
					break;
				case 5:d5();
					break;
				case 6:f6();
					break;
				case 7:f7();
					break;
				case 8:s8();
					break;
				case 9:v9();
					break;
				case 10:v10();
					break;
				case 11:s11();
					break;
				case 12:e12();
					break;
				case 13:e13();
					break;
				case 14:h14();
					break;
				case 15:h15();
					break;
				case 16:i16();
					break;
				case 17:i17();
					break;
				case 18:j18();
					break;
				case 19:j19();
					break;
				case 20:j20();
					break;
				case 21:j21();
					break;
				case 22:j22();
					break;
				case 23:j23();
					break;
				case 24:s24();
					break;
				case 25:k25();
					break;
				case 26:k26();
					break;
				case 27:a27();
					break;
				case 28:b28();
					break;
				case 29:m29();
					break;
				case 30:n30();
					break;
				case 31:o31();
					break;
				case 32:r32();
					break;
				case 33:p33();
					break;
				default:
					break;
				}
				//如果是空产生式不弹栈
				if(!((String)genFormu.genOut.elementAt(actionTemp-200)).startsWith("z"))
				{
					for(int i=0;i<genLength;i++)
					{
						symStack.pop();
						staStack.pop();
					}
				}
				symStack.push((String)genFormu.genSymbol.elementAt(actionTemp-200));
				staTop=Integer.parseInt((String)staStack.lastElement());
				gotoIndex=Integer.parseInt((String)genFormu.genCode.elementAt(actionTemp-200))+25;
				gotoTemp=lrTable.lrTable[staTop][gotoIndex];
				staStack.push(Integer.toString(gotoTemp));
				break;
			case 3://===如果在action表中查到的是acc则成功
				//===将最后的出口填上标号
				int intLabel=newlabel();
				for(int i=0;i<groupVector.size();i++)
				{
					if(((fourGroup)groupVector.elementAt(i)).notFinished)
					{
						((fourGroup)groupVector.elementAt(i)).result=Integer.toString(intLabel);
					}
				}
				return;
			default:
				break;
			}
		}
	}
	//================================================================================
	//===================================连接两个表===================================
	//================================================================================
	//==参数说明[firList---先归约的非终结符的list,secList---后归约的非终结符的list]==
	int merge(int firList,int secList)
	{
		boolean out=false;
		if(secList==0)
			return firList;
		else
		{
			String strTemp=new String();
			int indexTemp=firList-100;
			if(((fourGroup)groupVector.elementAt(indexTemp)).notFinished)
			{	
				strTemp=((fourGroup)groupVector.elementAt(indexTemp)).result;
			}
			else
			{
				out=true;
			}
			while(strTemp!="#" && !out)
			{
				if(((fourGroup)groupVector.elementAt(indexTemp)).notFinished)
				{	
					indexTemp=Integer.parseInt(strTemp);
					strTemp=((fourGroup)groupVector.elementAt(indexTemp)).result;
				}
				else
				{
					out=true;
				}
			}
			((fourGroup)groupVector.elementAt(indexTemp)).result=Integer.toString(secList-100);
			return firList;
		}
	}
	//===============================================================================
	//=====================================回填出口==================================
	//===============================================================================
	
	//==============参数说明[list---四元组的标号,label---result域的标号]============
	void backpatch(int list,String label)
	{
		String strTemp=new String();
		int indexTemp=list-100;
		boolean out=false;
		
		if(list!=0 && ((fourGroup)groupVector.elementAt(indexTemp)).notFinished)
		{
			strTemp=((fourGroup)groupVector.elementAt(indexTemp)).result;
			((fourGroup)groupVector.elementAt(indexTemp)).result=label;
		}
		else
		{
			out=true;
		}
		while(strTemp!="#" && !out)
		{			
			if(((fourGroup)groupVector.elementAt(indexTemp)).notFinished)
			{
				((fourGroup)groupVector.elementAt(indexTemp)).result=label;
				((fourGroup)groupVector.elementAt(indexTemp)).notFinished=false;
				indexTemp=Integer.parseInt(strTemp);
				strTemp=((fourGroup)groupVector.elementAt(indexTemp)).result;
			}
			else
			{
				out=true;
			}
		}
		if(list!=0 && ((fourGroup)groupVector.elementAt(indexTemp)).notFinished)
		{
			((fourGroup)groupVector.elementAt(indexTemp)).result=label;
			((fourGroup)groupVector.elementAt(indexTemp)).notFinished=false;
		}
	}
	//===============================================================================
	//=================================各文法对应的语义==============================
	//===============================================================================
	void s1()
	{
		attribution attri=new attribution();
		attribution newAttri=new attribution();
		attri=(attribution)attriStack.pop();
		fourGroup group=new fourGroup();
		group.label=newlabel();
		group.operate="=";
		group.opFirst=attri.place;
		group.opSecond=" ";
		group.result=(String)symStack.elementAt(symStack.size()-3);
		group.notFinished=false;
		groupVector.addElement(group);
		fourGroup group2=new fourGroup();
		group2.label=newlabel();
		group2.operate="j";
		groupVector.addElement(group2);
		newAttri.nextList=group2.label;
		attriStack.push(newAttri);
		if(isStep)
		{
			String strTemp=new String();
			for(int i=0;i<groupVector.size();i++)
			{
				strTemp=strTemp+((fourGroup)groupVector.elementAt(i)).label+":"+"("+((fourGroup)groupVector.elementAt(i)).operate+","+((fourGroup)groupVector.elementAt(i)).opFirst+","+((fourGroup)groupVector.elementAt(i)).opSecond+","+((fourGroup)groupVector.elementAt(i)).result+")"+"\n";
			}
			int dlgResult=MessageBox.show(strTemp,"四元组",1);
			if (dlgResult == DialogResult.CANCEL)
			{
				isStep=false;
			}
		}
	}
	void c2()
	{
		attribution attri=new attribution();
		attribution newAttri=new attribution();
		fourGroup group=new fourGroup();
		group.label=newlabel();
		group.operate="+";
		attri=(attribution)attriStack.pop();
		group.opSecond=attri.place;
		attri=(attribution)attriStack.pop();
		group.opFirst=attri.place;
		group.result=newtemp();
		group.notFinished=false;
		groupVector.addElement(group);
		newAttri.place=group.result;
		attriStack.push(newAttri);
		if(isStep)
		{
			String strTemp=new String();
			for(int i=0;i<groupVector.size();i++)
			{
				strTemp=strTemp+((fourGroup)groupVector.elementAt(i)).label+":"+"("+((fourGroup)groupVector.elementAt(i)).operate+","+((fourGroup)groupVector.elementAt(i)).opFirst+","+((fourGroup)groupVector.elementAt(i)).opSecond+","+((fourGroup)groupVector.elementAt(i)).result+")"+"\n";
			}
			int dlgResult=MessageBox.show(strTemp,"四元组",1);
			if (dlgResult == DialogResult.CANCEL)
			{
				isStep=false;
			}
		}
	}
	void c3()
	{
		/**/
	}
	void d4()
	{
		attribution attri=new attribution();
		attribution newAttri=new attribution();
		fourGroup group=new fourGroup();
		group.label=newlabel();
		group.operate="*";
		attri=(attribution)attriStack.pop();
		group.opSecond=attri.place;
		attri=(attribution)attriStack.pop();
		group.opFirst=attri.place;
		group.result=newtemp();
		group.notFinished=false;
		groupVector.addElement(group);
		newAttri.place=group.result;
		attriStack.push(newAttri);
		if(isStep)
		{
			String strTemp=new String();
			for(int i=0;i<groupVector.size();i++)
			{
				strTemp=strTemp+((fourGroup)groupVector.elementAt(i)).label+":"+"("+((fourGroup)groupVector.elementAt(i)).operate+","+((fourGroup)groupVector.elementAt(i)).opFirst+","+((fourGroup)groupVector.elementAt(i)).opSecond+","+((fourGroup)groupVector.elementAt(i)).result+")"+"\n";
			}
			int dlgResult=MessageBox.show(strTemp,"四元组",1);
			if (dlgResult == DialogResult.CANCEL)
			{
				isStep=false;
			}
		}
	}
	void d5()
	{
		/**/
	}
	void f6()
	{
		/* */
	}
	void f7()
	{
		attribution attri=new attribution();
		attri.place=(String)symStack.lastElement();
		attriStack.push(attri);
	}
	void s8()
	{
		attribution attriV=new attribution();
		attribution attriS=new attribution();
		attribution attriA=new attribution();
		attribution attriE=new attribution();
		attriV=(attribution)attriStack.pop();
		attriS=(attribution)attriStack.pop();
		attriA=(attribution)attriStack.pop();
		attriE=(attribution)attriStack.pop();
		attribution newAttri=new attribution();
		backpatch(attriE.trueList,Integer.toString( attriA.nextList));			
		backpatch(attriE.falseList,Integer.toString(attriV.falseList));
		backpatch(attriS.nextList,Integer.toString(attriV.nextList));
		//======================merge============================
		newAttri.nextList=attriV.nextList;
		attriStack.push(newAttri);
	}
	void v9()
	{
		attribution attriS=new attribution();
		attribution attriB=new attribution();
		attriS=(attribution)attriStack.pop();
		attriB=(attribution)attriStack.pop();
		attribution newAttri=new attribution();
		newAttri.nextList=attriS.nextList;
		newAttri.falseList=attriB.nextList;
		attriStack.push(newAttri);
	}
	void v10()
	{
		attribution attri=new attribution();
		attri.nextList=newlabel();
		newLabel--;
		attri.falseList=attri.nextList;
		attriStack.push(attri);
	}
	void s11()
	{
		attribution attriS=new attribution();
		attribution attriN=new attribution();
		attribution attriE=new attribution();
		attribution attriM=new attribution();
		attribution newAttri=new attribution();
		attriS=(attribution)attriStack.pop();
		attriN=(attribution)attriStack.pop();
		attriE=(attribution)attriStack.pop();
		attriM=(attribution)attriStack.pop();
		backpatch(attriS.nextList,Integer.toString(attriM.nextList));
		backpatch(attriE.trueList,Integer.toString(attriN.nextList));
		newAttri.nextList=attriE.falseList;
		attriStack.push(newAttri);
	}
	void e12()
	{
		attribution attriH=new attribution();
		attribution attriO=new attribution();
		attribution attriE=new attribution();
		attriH=(attribution)attriStack.pop();
		attriO=(attribution)attriStack.pop();
		attriE=(attribution)attriStack.pop();
		attribution newAttri=new attribution();
		backpatch(attriE.falseList,Integer.toString(attriO.nextList));
		newAttri.trueList=merge(attriE.trueList,attriH.trueList);
		newAttri.falseList=attriH.falseList;
		attriStack.push(newAttri);
	}
	void e13()
	{
		/* */
		 
	}
	void h14()
	{
		attribution attriI=new attribution();
		attribution attriR=new attribution();
		attribution attriH=new attribution();
		attriI=(attribution)attriStack.pop();
		attriR=(attribution)attriStack.pop();
		attriH=(attribution)attriStack.pop();
		attribution newAttri=new attribution();
		backpatch(attriH.trueList,Integer.toString(attriR.nextList));
		newAttri.trueList=attriI.trueList;
		newAttri.falseList=merge(attriH.falseList,attriI.falseList);
		attriStack.push(newAttri);
	}
	void h15()
	{
		/* */
	}
	void i16()
	{
		attribution attriJ=new attribution();
		attribution newAttri=new attribution();
		attriJ=(attribution)attriStack.pop();
		newAttri.falseList=attriJ.trueList;
		newAttri.trueList=attriJ.falseList;
		attriStack.push(newAttri);
	}
	void i17()
	{

⌨️ 快捷键说明

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