컴 파일 원리 - 변수 성명 문 (4)

3088 단어 Java
문법 분석.
코드:
Syntax.java
package per.eyuan.compile;

import per.eyuan.util.Action;
import per.eyuan.util.GoTo;
import per.eyuan.util.Id;
import per.eyuan.util.IdTable;
import per.eyuan.util.MyStack;
import per.eyuan.util.TwoItem;

public class Syntax {
//	Id idt[];//         Id  ,   type  
	IdTable idt;
	TwoItem ti[];//   
	int ti_flag;//         ,
	MyStack msStatu=new MyStack();//   ,    
	MyStack msSymble=new MyStack();//   ,    
	MyStack msSmtm=new MyStack();//   
	Action act=new Action();//Action 
	GoTo gt=new GoTo();//GoTo 
	String action;//  
	boolean succ=false;//    ,    
	String statu="";//           
	String symble="";//     
	String state="";//  
	String semanteme="";//  
	String syntax[]={"S`->S","S->D;","D->int id","D->float id","D->D,id"};
	String synL[]={"X","S","D","D","D"};
	String synR[][]={{"S"},{"D",";"},{"int","id"},{"float","id"},{"D",",","id"}};
	String vnType;//     “  ”
	public Syntax() {
		super();
		init();
	}

	public void init(){
		//          
		msStatu.push("0");
		msSymble.push("#");
		msSmtm.push("_");
		ti_flag=0;
	}
	public void analyse(){
		System.out.println("   \t\t   \t\t  \t\t   \t\t\t    ");
		while(!succ){
			//           ,  Action 
			act.setTable(msStatu.getTop(), ti[ti_flag].getItem1());
			//  Action      ,    
			//action=act.getAction();
			action=act.getAction();
			perform(action);
		}
		
	}
	public void perform(String action){
		//  
		if(action.equals("err")){
			error();
		}
			
		//  
		else if(action.equals("acc")){
			this.succ=true;
			output(-1);
		}
		//  
		//   ,    ,    ,   “  ”
		else if(action.charAt(0)=='s'){
			int x=Integer.parseInt(action.charAt(1)+"");
			output(x);//       
			msStatu.push(x+"");
			msSymble.push(ti[ti_flag].getItem1());
			if(ti[ti_flag].getItem1().equals("id"))
				msSmtm.push(ti[ti_flag].getItem2());
			//  msSmtm.push(id[Integer.parseInt(ti[ti_flag].getItem2())].getName());
			else
				msSmtm.push("_");
			ti_flag++;//   0  ,    ,   “  ”
			
		}
		//  
		else if(action.charAt(0)=='r'){
			int y=Integer.parseInt(action.charAt(1)+"");
			output(y);//       

			int ind=-1;
			if(y==2||y==3||y==4)
				ind=Integer.parseInt(ti[ti_flag-1].getItem2());
			
			for(int j=0;j

좋은 웹페이지 즐겨찾기