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

📄 cifa2.java

📁 刚做的,希望对大家有帮助,做的不好,请原谅,
💻 JAVA
字号:
//ronghong 2005.5.12
import java.io.*;

class CiFa2
{
	private String[] word;//保留字表
	private String[] wsym;	
	private char[] sword;//运算符表
	private String[] ssym;
	
	private int CC;
	private char CH;
	private char CalMark;
	
	public  String BuffStr;//缓冲区
	private int BuffLength;
	
	private  String SYM;
	private  String ID;
	private  String NUM;
	
	private String SourceLocat; //源文件位置
	private String Sources[]; //源文件,以行为单位存储
	private String inLine;
	
	private String TargetLocat; //
	private String WrTwoFormule; //二元式
	
	CiFa2(){
			word=new String[13];
			word[0]="begin";
			word[1]="call";
			word[2]="const";
			word[3]="do";
			word[4]="end";
			word[5]="if";			
			word[6]="odd";
			word[7]="procedure";
			word[8]="read";
			word[9]="then";
			word[10]="var";
			word[11]="while";
			word[12]="write";
					
			wsym=new String[13];
			wsym[0]="beginsym";
			wsym[1]="callsym";
			wsym[2]="constsym";
			wsym[3]="dosym";
			wsym[4]="endsym";
			wsym[5]="ifsym";			
			wsym[6]="oddsym";
			wsym[7]="proceduresym";
			wsym[8]="readsym";
			wsym[9]="thensym";
			wsym[10]="varsym";
			wsym[11]="whilesym";
			wsym[12]="writesym";	
				
			
			sword=new char[8];
			sword[0]='+';
			sword[1]='-';
			sword[2]='*';
			sword[3]='/';
			sword[4]='(';
			sword[5]=')';
			//sword[6]=',';
			sword[6]='=';
			//sword[8]=';';
			sword[7]='#';			
			ssym=new String[8];			
			ssym[0]="plus";
			ssym[1]="minus";
			ssym[2]="times";
			ssym[3]="slash";
			ssym[4]="lparen";
			ssym[5]="rparen";
			//ssym[6]="comma";
			ssym[6]="eql";
			//ssym[8]="semicolon";
			ssym[7]="end";
			
			CC=0;
			
			//BuffStr=TempStr;//缓冲区
			//BuffLength=BuffStr.length();
			
			SYM="";
			ID="";
			NUM="";
			
			SourceLocat="Source.txt";
			Sources=new String[10]; //源文件不超过10行
			
			TargetLocat="Target.txt";
			WrTwoFormule="";
	}
	

	
	public void writeTarget() //写入Target.txt
	
		{			
			String A="";
			boolean condition=false;
			
			if(CC==0){
					do{
						Getch();
					}
					while(IsBorder());
					};
												
			if(!IsOver()){
			condition=( ( CH>=65 && CH<=90 )||( CH>=97 && CH<=122 ) );//CH是字符
			if(condition){
				int k=0;	
				do{
					A=A+CH;	
					k++;
					Getch();
					condition=(CH>=65 && CH<=90)||(CH>=97 && CH<=122)||(CH>=65 && CH<=90)||(CH>=97 && CH<=122);
					}
				while(condition);
				
				while(IsBorder()){
					Getch();
				}
				
				ID=A;
					
				SYM="";
							
				for(int temp2=0;temp2<word.length;temp2++){
					if(word[temp2].equals(ID)){ //是保留字
						SYM=wsym[temp2];
						break;
						};
					}
					
				if(SYM.equals("")){
					SYM="ident";//不是保留字					
					};

				//System.out.println("("+ID+","+SYM+")");
				WrTwoFormule="("+ID+","+SYM+")";
				try{WriteFile(WrTwoFormule);}catch(IOException ioe){};
				}
				
				else{
					condition=(CH>=48 && CH<=57);
					if(condition){ //CH是数字
						NUM="";
						SYM="";
						while(CH>=48 && CH<=57){
							NUM=NUM+CH;
							Getch();
							}
							
						while(IsBorder()){
							Getch();
						}
						
						SYM="number";
						//System.out.println("("+NUM+","+SYM+")");
						WrTwoFormule="("+NUM+","+SYM+")";
						try{WriteFile(WrTwoFormule);}catch(IOException ioe){};
						
						}
						else{
							if(!IsOver()){ //CH是算符或界符
							for(int temp3=0;temp3<sword.length;temp3++){
								if(sword[temp3]==CH){
									SYM=ssym[temp3];
									CalMark=sword[temp3];
									break;
									}
								} //off for

							//System.out.println("("+CalMark+","+SYM+")");
							WrTwoFormule="("+CalMark+","+SYM+")";
							try{WriteFile(WrTwoFormule);}catch(IOException ioe){};
							do{
								Getch();
							}
							while(IsBorder());

							};				
						};
					}; //off else
					
				}; //off if
		} //off writeTarget()
		
		
	public char Getch(){
		if(CC==0){
			try{ReadFile();}catch(IOException ioe){};
		}
			String TempStr2="";
			if(CC+1<=BuffStr.length()){
					TempStr2=BuffStr.substring(CC,CC+1);
					CC++;
					CH=TempStr2.charAt(0);
					return CH;
				};
				CH='#';
				return CH;
		} //off Getch()
		
		
	private boolean IsOver(){ //判断是否结束
		if(CH=='#'){
			return true;
		}
		return false;
	}
	
	private boolean IsBorder(){ //判断是否为界符
		boolean IsBorder=false;
		IsBorder=(CH==',') || (CH=='.') || (CH==';') || (CH==' ');
		return IsBorder;
	}
	
	private void ReadFile() //读入文件
		throws java.io.IOException{
			int counter=0;
			BuffStr="";
			
			FileReader fr= new FileReader(SourceLocat);
			BufferedReader br=new BufferedReader(fr);
			
			while( (inLine=br.readLine())!=null){
				BuffStr=BuffStr+inLine;
				//Sources[counter]=inLine+"#";
				//counter++;
			}
			BuffStr=BuffStr+"#";
			br.close();		
	}

	private void WriteFile(String TempTarg) //写文件
		throws java.io.IOException{
			
			FileWriter fw=new FileWriter(TargetLocat,true);
			BufferedWriter bw=new BufferedWriter(fw);
			PrintWriter pw=new PrintWriter(bw,true);

			pw.println(TempTarg);			
			pw.close();
		}

	private void TempWriteOut(){ //暂时用来输出二元式
		try{ReadFile();}catch(IOException ioe){};
		int i=0;
		//String TempStr3="";
		while(Sources[i]!=null){
			BuffStr=Sources[i];
			CC=0;
			CH=' ';
			while(CH!='#'){
				writeTarget();
			}
			i++;
		}
	}


public String getSym()
	
		{			
			String A="";
			boolean condition=false;
			String retStr="";
			
			if(CC==0){
					do{
						Getch();
					}
					while(IsBorder());
					};
												
			if(!IsOver()){
			condition=( ( CH>=65 && CH<=90 )||( CH>=97 && CH<=122 ) );//CH是字符
			if(condition){
				int k=0;	
				do{
					A=A+CH;	
					k++;
					Getch();
					condition=(CH>=65 && CH<=90)||(CH>=97 && CH<=122)||(CH>=65 && CH<=90)||(CH>=97 && CH<=122);
					}
				while(condition);
				
				while(IsBorder()){
					Getch();
				}
				
				ID=A;
					
				SYM="";
							
				for(int temp2=0;temp2<word.length;temp2++){
					if(word[temp2].equals(ID)){ //是保留字
						SYM=wsym[temp2];
						break;
						};
					}
					
				if(SYM.equals("")){
					SYM="ident";//不是保留字					
					};

				//System.out.println("("+ID+","+SYM+")");
				retStr="("+ID+","+SYM+")";
				}
				
				else{
					condition=(CH>=48 && CH<=57);
					if(condition){ //CH是数字
						NUM="";
						SYM="";
						while(CH>=48 && CH<=57){
							NUM=NUM+CH;
							Getch();
							}
							
						while(IsBorder()){
							Getch();
						}
						
						SYM="number";
						//System.out.println("("+NUM+","+SYM+")");
						retStr="("+NUM+","+SYM+")";
						}
						else{
							if(!IsOver()){ //CH是算符或界符
							for(int temp3=0;temp3<sword.length;temp3++){
								if(sword[temp3]==CH){
									SYM=ssym[temp3];
									CalMark=sword[temp3];
									break;
									}
								} //off for

							//System.out.println("("+CalMark+","+SYM+")");
							retStr="("+CalMark+","+SYM+")";
							do{
								Getch();
							}
							while(IsBorder());

							};				
						};
					}; //off else
					
				}; //off if
			if(!retStr.equals("")){
				return retStr;
				}	
			else{
				return null;
			}
		} //off getSym()

	/*public static void main(String[] args){
		CiFa a=new CiFa();
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
		System.out.println(a.getSym());
	} //off main()*/
	
}//off class

⌨️ 快捷键说明

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