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

📄 syntaxanalyse.java

📁 操作系统中的语法分析,主要是判断语言是否合乎语言规范
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.io.*;
public class SyntaxAnalyse{
	int linepos=0;
	String cstr=new String();
    String fstr=new String();
    String keystr=new String();
    String errstr=new String();
    String []KeyWord1=new String[36];
    RandomAccessFile file;
    //构造函数
    public SyntaxAnalyse()
    {
    	this.SetTAble();
    }
	//从控制台取得信息放在cstr中
	public  String ReadLineFormConsole()
 {
  try{
  	String temp=new String();
  	BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
  	temp=in.readLine();
    cstr=temp;
  	}
  	catch(IOException e)
  	{
  		e.printStackTrace();
  		}
  		return cstr;	
 }
 //设置对照表
 public void SetTAble()
 {
 	    KeyWord1[0]=null;
        KeyWord1[1]="PROGRAM";
		KeyWord1[2]="CONST";
		KeyWord1[3]="VAR";
		KeyWord1[4]="INTEGER";
		KeyWord1[5]="LONG";
		KeyWord1[6]="PROCEDURE";
		KeyWord1[7]="IF";
		KeyWord1[8]="THEN";
		KeyWord1[9]="WHILE";
		KeyWord1[10]="DO";
		KeyWord1[11]="READ";
		KeyWord1[12]="WRITE";
		KeyWord1[13]="BEGIN";
		KeyWord1[14]="END";
		KeyWord1[15]="ODD";
		KeyWord1[16]="PLUS";
		KeyWord1[17]="MINUS";
		KeyWord1[18]="IMES";
		KeyWord1[19]="SLASH";
		KeyWord1[20]="EQL";
		KeyWord1[21]="NLEQ";
		KeyWord1[22]="LSS";
		KeyWord1[23]="LEQ";
		KeyWord1[24]="GTR";
		KeyWord1[25]="GEQ";
		KeyWord1[26]="PERIOD";
		KeyWord1[27]="COMMA";
		KeyWord1[28]="SEMICOLON";
		KeyWord1[29]="COLON";
		KeyWord1[30]="BECOMES";
		KeyWord1[31]="LPAREN";
		KeyWord1[32]="RPAREN";
		KeyWord1[33]="USNUMBER";
		KeyWord1[34]="IDENT";
		KeyWord1[35]="NEQ";	
 }
 //从词法分析文件中取得一行信息
 public String GetWord()
 {
 	//String temp=new String();
 	try{
 		fstr=file.readLine();
 		//linepos++;
 		}
 	catch(IOException e)
 	{
 		e.printStackTrace();
 	}
 	
 	return fstr;
 }
 //从词法分析文件中取得一行信息中取得关键码信息
 public void GetKeyWord()
 {
 	int start=0,end=0;
 	int key=0;
 	String temp=new String();
 	String temp1=new String();
 	temp=this.GetWord();
 	if(temp==null)
 	{
 		//System.out.println("语法分析结束");
 		return;
 		
 	}
 	start=temp.indexOf('(');
 	end=temp.indexOf(',');
 	if(2<=(end-start))
 	{
 		temp1=temp.substring(0,start);
 		temp=temp.substring(start+1,end);
 		//System.out.println(temp1);
 		try
 		{
 			//System.out.println();
 			linepos=Integer.parseInt(temp1.trim());
 			key=Integer.parseInt(temp.trim());
 			
 			if(key<=35)
 			{
 			 keystr=KeyWord1[key];	
 			}
 			else
 			{
 				System.out.println("词法分析错误");
 				keystr=KeyWord1[0];
 			}
 			
 		}
 		catch(NumberFormatException e)
 		{
 			e.printStackTrace();
 		}
 		
 	}
 	else
 	{
 		keystr=KeyWord1[0];
 	}
 	
 }
 //测试用
 public String GetString()
 {
  return keystr;	
 }
 
//打开要读的文件
public boolean OpenFile(String path)
{
	
	try{
		file=new RandomAccessFile(path,"r");
		return true;
	   }
	catch(FileNotFoundException e)
	{
		e.printStackTrace();
		return false;
	}
	catch(IOException e)
 	{
 		e.printStackTrace();
 		return false;
 	}
	
}
//关闭文件
public void CloseFile()
{
   try{
   	  file.close();
      }
   catch(IOException e)
   {
   	e.printStackTrace();
   }   
	
}
//错误处理程序
public void ErrorDoen(String err)
{
	System.out.println("第"+linepos+"行,发生错误:"+err);
}
//程序
public boolean Program()
{
	if(true==ProgHead())
	{
		if(true==Block())
		{
			if("PERIOD"==keystr)
			{
				GetKeyWord();
				return true;
			}
			else
			{
				errstr="程序应该用\".\"结束";
				this.ErrorDoen(errstr);
				return false;
			}
		}
		else return false;
	}
	else return false;
} 
//程序头部
public boolean ProgHead()
{
	GetKeyWord();
	if("PROGRAM"==keystr)
	{
	 	GetKeyWord();
	 	if("IDENT"==keystr)
	 	{
	 		GetKeyWord();
	 		if("SEMICOLON"==keystr)
	 		{
	 			GetKeyWord();
	 			return true;
	 		}
	 		else
	 		{
	 	     errstr="请你申明程序头要以\";\"结束";
	         this.ErrorDoen(errstr);
	         return false;	
	 		}
	 		
	 	}
	 	else
	 	{
	 		errstr="请你申明正确的程序名";
	        this.ErrorDoen(errstr);
	        return false;	
         
	 	}
	}
	else
	{
         errstr="程序应该用\"PROGRAM\"开始";
	     this.ErrorDoen(errstr);
	     return false;	 			  
	}
}


//常量说明部分r
public boolean ConsExpl()
{
    if("CONST"==keystr)
    {
     GetKeyWord();
     if(true==ConsDefi())
     {
     	if(true==ConsSuff())
     	return true;
     	else return false;	
     }
     else return false;	
    }
    else return true;	
}
//常量定义r
public boolean ConsDefi()
{
    
	if("IDENT"==keystr)
	{
	    GetKeyWord();
		if("EQL"==keystr)
		{
	     
		 GetKeyWord();
		if("USNUMBER"==keystr)
		 {
		 	GetKeyWord();
		 	return true;
		 }
		 else
		 {
		 	errstr="常量定义错误:应是无符号整数";
	        ErrorDoen(errstr);
	        return false;
		 }
		}
		else
		{
			errstr="常量定义错误:应是等号:";
	        ErrorDoen(errstr);
	        GetKeyWord();
	        return false;	
		}
	 	
	}
	else
	{
	 errstr="常量定义错误:应是标识符:";
	 ErrorDoen(errstr);
	 GetKeyWord();
	 return false;	
	}	
}
//常量定义后缀r
public boolean ConsSuff()
{
	
	while(true)
	{
	if("COMMA"==keystr)
	{
		 GetKeyWord();
		if(true==ConsDefi()){}
		else return false;
				
	}
    else 
    {
    	return true;	
	}
	}

}

//变量说明部分r
public boolean VarExpl()
{
	if("VAR"==keystr)
	{
		GetKeyWord();
		if(true==VarDefi())
		{
		if(true==VarSuff())
		 return true;
		else
		 return false;	
		}
		else return false;
		
	}
	else return true;
}
//变量定义r
public boolean VarDefi()
{
    if("IDENT"==keystr)
    {
     GetKeyWord();
     if(true==IdSuff())
     {
     if("COLON"==keystr)
     {
      GetKeyWord();
      if(true==TypeIl())
      {
      	if("SEMICOLON"==keystr)
      	{
      		GetKeyWord();
      		return true;
      	}
      	else
      	{
     	errstr="变量定义错误:应是\";\"";
     	this.ErrorDoen(errstr);
     	return false;      		
      	}
      }	
      else
      {
      	return false;
      }
     }
     else
     {
     	errstr="变量定义错误:应是\":\"";
     	this.ErrorDoen(errstr);
     	return false;
     }	
     }
     else return false;	
    }
    else return false;	
}
//类型r
public boolean TypeIl()
{
	if("INTEGER"==keystr)
	{
		GetKeyWord();
		return true;
	}
	else if("LONG"==keystr)
	{
		GetKeyWord();
		return true;	
	}
	else
	{
		errstr="类型错误:";
		this.ErrorDoen(errstr);
		//GetKeyWord();
		return false;
	}
}
//标识符后缀r
public boolean IdSuff()
{
 while(true)
 {
 	if("COMMA"==keystr)
 	{
 	  GetKeyWord();
 	  if("IDENT"==keystr)
 	  {
 	  	GetKeyWord();
 	  }
 	  else
 	  {
 	  	errstr="标识符后缀-错误:应该是标识符";
 	  	this.ErrorDoen(errstr);
 	  	return false;
 	  }
 	  	
 	}
 	else return true;
 }	
}
//变量定义后缀r
public boolean VarSuff()
{
	while(true)
    {
    	if("IDENT"==keystr)
    	 {
    	 	if(true==VarDefi()){}
    	 	else return false;
    	 }
        else return true;
    }
 	
}
//过程说明部分r
public boolean ProcDefi()
{
 if("PROCEDURE"==keystr)	
 {
 	if(true==ProceDh())
 	{
 		if(true==Block())
 	{
 	 if("SEMICOLON"==keystr)
 	 {
 	 	GetKeyWord();
 	 	if(true==ProcSuff())
 	    {
 	     return true;	
 	    }
 	    else return false;
 	 }
 	 else
 	 {
 	  		errstr="常量定义错误:应该是分号(SEMICOLON)\""+keystr+"\"";
	        ErrorDoen(errstr);
	        //GetKeyWord();
	        return false;	
 	 }
 	 }
 	 else return false;
 	}
 	
 	else return false;
 }
 else return true;
 	
}
//过程首部r
public boolean ProceDh()
{
	if("PROCEDURE"==keystr)
	{
		GetKeyWord();
		if("IDENT"==keystr)
		{
			GetKeyWord();
			if(true==Argument())
			{
			 if("SEMICOLON"==keystr)	
			 {
			  	GetKeyWord();
			  	return true;
			 }
			 else
			 {
			 	errstr="过程首部错误,应该是分号(SEMICOLON)而不是\""+keystr+"\"";
	 	        this.ErrorDoen(errstr);
	 	        //GetKeyWord();
	 	        return false;	
			 }
			}
			else return false;
		}
		else
		{
		 errstr="过程首部错误,应该是标识符(IDENT)而不是\""+keystr+"\"";
	 	 this.ErrorDoen(errstr);
	 	 //GetKeyWord();
	 	 return false;		
		}
	}
	else
	{
	     errstr="过程首部错误,应该是过程头是(PROCEDURE)而不是\""+keystr+"\"";
	 	 this.ErrorDoen(errstr);
	 	 //GetKeyWord();
	 	 return false;		
	}
	
}
//参数部分r
public boolean Argument()
{
	if("LPAREN"==keystr)
	{
	 GetKeyWord();
	 if("IDENT"==keystr)
	 {
	  GetKeyWord();
	  if(true==TypeIl())
	  {
	  	if("RPAREN"==keystr)
	  	{
	  	 GetKeyWord();
	  	 return true;
	  	}
	  	else
	  	{
	  	 errstr="参数部分错误,应该是右括号(RPAREN)而不是\""+keystr+"\"";
	 	 this.ErrorDoen(errstr);
	 	 GetKeyWord();
	 	 return false;	
	  	}
	  }
	  else return false;	
	 }
	 else
	 {
	 	errstr="参数部分错误,应该是标识符而不是\""+keystr+"\"";
	 	this.ErrorDoen(errstr);
	 	GetKeyWord();
	 	return false;
	 }	
	}
	else return true;
	
}
//过程说明部分r
public boolean ProcSuff()
{
	while(true)
	{
	if("PROCEDURE"==keystr)	
    {
    if(true==ProceDh())
    {
    	if(true==Block())
 	 {
 	 if("SEMICOLON"==keystr)
 	 {
 	 	GetKeyWord();
 	 }
 	 else
 	 {
 	  		errstr="常量定义错误:应该是分号(SEMICOLON)\""+keystr+"\"";
	        ErrorDoen(errstr);
	        GetKeyWord();
	        return false;	
 	 }
 	 }
 	  else return false;
      }
      else return false;
    }
 	
      else return true;
 	  }
}
//分程序r
public boolean Block()
{
	if(true==ConsExpl())
	{
		if(true==VarExpl())
		{
			if(true==ProcDefi())
			{
			 if(true==Sentence())
			 {
			 	return true;
			 }
			 else return false;	
			}
			else return false;
		}
		else return false;
	}
	else return false;
}
//语句r
public boolean Sentence()
{
	if("IDENT"==keystr)
	{
	 if(true==AssiPro())
	 {
	 	return true;
	 }
	 else return false;
	}
	else if("IF"==keystr)
	{
	if(true==IfSent())
	 {
	 	return true;
	 }
	 else return false;
	}
	else if("WHILE"==keystr)
	{
	if(true==WhilSent())
	 {
	 	return true;
	 }

⌨️ 快捷键说明

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