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

📄 yufafenxi.java

📁 编译原理 实现了词法分析 语法分析等基本功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

/**
 * 
 * 
 
〈程序〉→〈分程序〉。  Chengxu()-->Fenchegnxu();
〈分程序〉→ [<常量说明部分>][<变量说明部分>][<过程说明部分>]〈语句〉 Fenchengxu()-->CASE:const,var,procedure 
           Changliangshuoming();bianliangshuoming();guochengshuoming();yuju();
 <常量说明部分> → CONST<常量定义>{ ,<常量定义>};Changliangshuoming()-->if(const)  while(,) constdefine(); end ";"
 <常量定义> → <标识符>=<无符号整数> changliangdingyi()-->  IDENT = NUMBER  else error
 <无符号整数> → <数字>{<数字>} NUMBER 常量、变量和过程说明 TABLE
 <变量说明部分> → VAR<标识符>{ ,<标识符>};常量、变量和过程说明  TABLE
 <标识符> → <字母>{<字母>|<数字>} IF
 <过程说明部分> → <过程首部><分程度>;{<过程说明部分>}while   guochengshuoming()-->常量、变量和过程说明  TABLE
 <过程首部> → procedure<标识符>;if
 <语句> → <赋值语句>|<条件语句>|<当型循环语句>|<过程调用语句>|<读语句>|<写语句>|<复合语句>|<空>
 yuju()-->if(ident,if````)fuzhiyuju();tiaojianyuju();dangxingxunhuan();call ident;duyuju();xieyuju();fuheyuju();
 <赋值语句> → <标识符>:=<表达式>if : if =(区别关系运算!!!!!)
 <复合语句> → begin<语句>{ ;<语句>}<end>fuheyuju()-->begin yuju(); while() yuju();end
 <条件> → <表达式><关系运算符><表达式>|odd<表达式>       tiaojian()-->biaodashi(),yunsuanfu,biaodashi()
 <表达式> → [+|-]<项>{<加减运算符><项>} biaodashi()-->xiang(); while (+-)
 <项> → <因子>{<乘除运算符><因子>}xiang()-->yinzi();while(*or/)
 <因子> → <标识符>|<无符号整数>|(<表达式>)yinzi()-->ident num biaodashi();
 <加减运符> → +|-
 <乘除运算符> → *|/
 <关系运算符> → =|#|<|<=|>|>=
 <条件语句> → if<条件>then<语句> tiaojianyuju()--> if condition then statement
 <过程调用语句> → call<标识符>  if 下同
 <当型循环语句> → while<条件>do<语句>
 <读语句> → read(<标识符>{ ,<标识符>})
 <写语句> → write(<标识符>{,<标识符>})
 <字母> → a|b|c…x|y|z
 <数字> → 0|1|2…7|8|9**/













/**
 * 
 */



package Yufa;
import java.awt.*;
import java.io.*;
import java.awt.event.*;

import javax.swing.*;
public class Yufafenxi extends JFrame implements ActionListener{

	static JButton Chufa;//触发器,直接打开文件!
	BufferedReader in;
	FileReader reader;
	public PrintWriter outputStream=null;
	
	String mys="";
    Ci CurrentWord;
	Tabdingyi define;
	Biao table=new Biao();
	int level=0;//level
	int adr=0;//DXdizhi 
	
	
	public static void main(String args[]){
		Yufafenxi gra=new Yufafenxi();
			//gra.setVisible(true);		
			
			
			Chufa.doClick();
	}
	public Yufafenxi(){
		try{
			outputStream=new PrintWriter(new FileOutputStream(
	        ("D:\\"	+"表格.txt")),true);
			}
			catch(FileNotFoundException e2){
				System.out.println("Error opening!");
			}
		
		
		Chufa=new JButton("打开文件");Chufa.addActionListener(
				
		
				new   ActionListener(){   
					  public   void   actionPerformed(ActionEvent   e)   
					  {   
					      //实现事件   
						  
							if(e.getSource()==Chufa){	
								//load.setVisible(true);
								
								//mys="";
								String help;
								{
									try{
										File file=new File("D:\\","test.txt");
										//System.out.println(load.getDirectory());System.out.println(load.getFile());
										reader=new FileReader(file);
										in=new BufferedReader(reader);
										while((help=in.readLine())!=null)
											mys+=help;
										in.close();
										reader.close();
										
									}catch(IOException e2){}
								}
								Chengxu();			
						
								outputStream.close();
							}
						  
						  
						  
						  
					  }   
					  });
		
		

		
		
	
		

	
	}

    public void show(Tabdingyi def){
    	
    	outputStream.append("NAME    "+def.NAME+":     KIND= "+def.KIND+"     VAL="+def.VAL+"     LEVEL="+def.LEVEL+"     ADR=(DX内容)"+def.ADR);
		outputStream.println();
    }
	public int Guanjianzi(String s){//关键字识别!!!!
		
		if(s.equals("const")){
			return 1;
		}
		else if(s.equals("var")){
			return 2;
		}
		else if(s.equals("procedure")){
			return 3;
		}
		else if(s.equals("begin")){
			return 4;
		}
		else if(s.equals("end")){
			return 5;
		}
		else if(s.equals("read")){
			return 6;
		}
		else if(s.equals("write")){
			return 7;
		}
		else if(s.equals("call")){
			return 8;
		}
		else if(s.equals("while")){
			return 9;
		}		
		else if(s.equals("do")){
			return 10;
		}
		else if(s.equals("if")){
			return 11;
		}
		else if(s.equals("then")){
			return 12;
		}
		else if(s.equals("odd")){
			return 13;
		}
		return 0;
	}
	
	
	
	
	
	
	
//	----------------------------------------词法分析--------------------------------------------------------------------
	public Ci getSym(String s){//即返回token的分程序
		
		
		
		if(s.length()==0){
			System.out.println("分析完毕!");
			Ci word=new Ci("acc","acc",null,-1);//sym,name=acc			
			return word;			
		}
		else{
			Ci word=new Ci();
			char[] a=s.toCharArray();
			char[] b=new char[30];
			char[] c;
			int index=1;//index记录已分析了几个字符,a[0]到a[index-1]已分析
			//display(a);
			if(Character.isLetter(a[0])){//首字符为字母
				b[0]=a[0];
				int i;
				for(i=1;i<a.length;i++){
					if(Character.isLetter(a[i])||Character.isDigit(a[i])){//concat
						b[index++]=a[i];					
					}
					else
						break;
				}
				///获得该单词放入name
				char[]d =new char[index];
				for(int j=0;j<index;j++){
					d[j]=a[j];
				}
				String name=new String (d);
				
				word.setNAME(name);
				//把char数组b中多余的元素去掉
				c=new char[index];
				for(int j=0;j<index;j++){
					c[j]=b[j];
				}
				
				//display(c);
				
				String help=new String(c);
				int code=Guanjianzi(help);
				if(code==0){
					//System.out.println("读到一个标识符");
					//field1.setText();
					word.ID=help;
					word.SYM="IDENT";
				}
				else{
					//System.out.println("读到一个关键字");
					word.SYM=help+"SYM";
				}
				//word.show("word1");
				word.show(word.NAME);
				 //count加一,同时保存到数组				
				//return index;					
			}
			else if(Character.isDigit(a[0])){
				b[0]=a[0];
				int i;
				for(i=1;i<a.length;i++){
					if(Character.isDigit(a[i])){//concat
						b[index++]=a[i];					
					}
					else
						break;
				}
	            ///获得该单词放入name
				char[]d =new char[index];
				for(int j=0;j<index;j++){
					d[j]=a[j];
				}
				String name=new String (d);
				word.setNAME(name);
				
	            //把char数组b中多余的元素去掉放入c
				c=new char[index];
				for(int j=0;j<index;j++){
					c[j]=b[j];
				}
				
				//display(c);	
				
				//System.out.println(c.length);
				//System.out.println(index);
				
				String help=new String(c);
				//int code=Guanjianzi(help);
				//System.out.println("读到一个数字");
				word.SYM="NUMBER";
				word.NUM=Integer.parseInt(help);
				//word.show("word1");			
					
				word.show(word.NAME);
				 //count加一,同时保存到数组
				//return index;
			}
			else if(a[0]=='>'){
				
				if(a[1]=='='){
					index=2;
					word.SYM=">=";
					word.setNAME(">=");
					//System.out.println("读到 >=");
				}
				else{
					index=1;
					word.SYM=">";
					word.setNAME(">");
					//System.out.println("读到 >");
				}	
				
				
				word.show(word.NAME);
				 //count加一,同时保存到数组
				//return index;
				//return null;
			}
			else if(a[0]=='<'){
				
				if(a[1]=='='){
					index=2;
					word.SYM="<=";
					word.setNAME("<=");
					//System.out.println("读到 <=");
				}
				else{
					index=1;
					word.SYM="<";
					word.setNAME("<");
					//System.out.println("读到 <");
				}	
				word.show(word.NAME);
				 //count加一,同时保存到数组
				//return index;
				//return null;
			}
			else if(a[0]=='*'){
				
				if(a[1]=='*'){
					index=2;
					word.SYM="**";
					word.setNAME("**");
					//System.out.println("读到 **");
				}
				else{
					index=1;
					word.SYM="*";
					word.setNAME("*");
					//System.out.println("读到 *");
				}	
				word.show(word.NAME);
				 //count加一,同时保存到数组
				//return index;
				//return null;
			}
			else if(a[0]=='+'||a[0]=='-'||a[0]=='/'||a[0]=='='||a[0]=='#'){
				index=1;
				String help=new String(a,0,1);//new String(char [] a,int startindex,int count)
				word.SYM=help;
				word.setNAME(help);
				//System.out.println("读到 "+help);
				word.show(word.NAME);
				 //count加一,同时保存到数组
				//return index;
			}
			else if(a[0]=='\n'||a[0]==';'||a[0]==')'||a[0]=='('||a[0]=='{'||a[0]=='}'||a[0]==','
				||a[0]==':'||a[0]=='.'){
				index=1;			
				String help=new String(a,0,1);//new String(char [] a,int startindex,int count)
				word.SYM=help;
				word.setNAME(help);
				//System.out.println("读到 "+help);
				word.show(word.NAME);
				// //count加一,同时保存到数组
				//return index;
			}
			
			else{
			
				System.out.println("您输入了非法字符,请重输");
				return null;
				//field.setText("您输入了非法字符,请重输");
				//return 0;
			}		
			
	   char[] help2=new char[a.length-index];
	   for(int ii=index,j=0;ii<a.length;ii++,j++){
	      help2[j]=a[ii];
	   }
			mys=new String(help2).trim();
			//System.out.println();
			//System.out.println(mys);
			//getSym(mys);
			return word;
			
	}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	//----------------------------------------语法分析--------------------------------------------------------------------
	void Chengxu(){//程序
		CurrentWord=getSym(mys);/////在此得到第一个单词		
		Fenchengxu();
		if(CurrentWord.SYM.equals(".")){
			System.out.println("分析完毕!!!");System.out.println("成功!!!");
			return;
		}
		else{
			CurrentWord.show(CurrentWord.SYM);
			Cuowu err=new Cuowu(9);
			err.showError();
			System.exit(0);
		}
	}
	void Fenchengxu(){//分程序
		if(CurrentWord.SYM.equalsIgnoreCase("const"+"SYM"))
		{
			Changliangshuoming();//常量说明部分,需要往下读一个			
		}
		if(CurrentWord.SYM.equalsIgnoreCase("var"+"SYM"))
		{
			bianliangshuoming();//变量说明部分,需要往下读一个
		}
		if(CurrentWord.SYM.equalsIgnoreCase("procedure"+"SYM"))
		{
			guochengshuoming();//过程说明部分 ,需要往下读一个
		}		
		yuju();//语句
		
	}
	void Changliangshuoming(){//常量说明部分
		
		do{
			CurrentWord=getSym(mys);
		    changliangdingyi();//常量定义	
		//CurrentWord=getSym(mys);
		}while(CurrentWord.SYM.equals(","));
		if(CurrentWord.SYM.equals(";")){
			CurrentWord=getSym(mys);//说明结束,读下一个	
			System.out.println("语法:");	
			System.out.println("分析了一个常量说明的部分");			
			
			
		}
		else{

⌨️ 快捷键说明

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