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

📄 getsym.java

📁 可以实现.txt和.pas文件的词法分析,提供给大家参考和学习
💻 JAVA
字号:
package PL0;

import java.util.*;
import PL0.*;
import java.io.*;

public class GetSym{
	static final int MAX=15;
	public static String csymbol;
	static String s;
	static char[] word=new char[MAX];
	public static GetChar c;
	static Symbol sym=new Symbol();
	public static PrintWriter out;   
	public GetSym(String fp,String wfp) throws IOException{
		c=new GetChar(fp);
		out=new PrintWriter(new BufferedWriter(new FileWriter(wfp)));
	}
	public static PrintWriter getout(){
		return out;
	}
	public static int getsym() throws IOException{
		while(c.ch==' ' || c.ch==10 || c.ch==9){
			if(c.getchar()==-1){
				return -1;
			}
		}
		if((c.ch>='a' && c.ch<='z') || (c.ch>='A' && c.ch<='Z')){
			int i=0;
			do{
				word[i]=c.ch;
				i++;
				if(c.getchar()==-1){
					return -1;
				}
			}while(c.ch>='a' && c.ch<='z' || c.ch>='A' && c.ch<='Z' || c.ch>'0' && c.ch<'9');
			s=new String(word);
			s=s.substring(0,i);
			if(sym.KeyWord.contains(s)){
				csymbol=s;
				out.write("keyword             "+s+"\r\n");
			}
			else
			{
				csymbol="ident";
				out.write("ident               "+s+"\r\n");
			}
			
		}
		else{
			if(c.ch>='0' && c.ch<='9'){
				int num=0;
				csymbol="number";
				do{
					num=num*10+c.ch-'0';
					if(c.getchar()==-1){
						return -1;
					}
				}while(c.ch>='0' && c.ch<='9');
				out.write("number              "+num+"\r\n");
			}
			else{
				if(c.ch==':'){
					if(c.getchar()==-1){
						return -1;
					}
					if(c.ch=='='){
						csymbol="becomes";
						out.write(":=                  "+csymbol+"\r\n");
						if(c.getchar()==-1){
							return -1;
						}
					}
					else{
						csymbol="nul";
						out.write("undefined symbol    "+csymbol+"\r\n");
					}
				}
				else{
					if(c.ch=='<'){
						if(c.getchar()==-1){
							return -1;
						}
						if(c.ch=='='){
							csymbol="leq";
							out.write("<=                  "+csymbol+"\r\n");
							if(c.getchar()==-1){
								return -1;
							}
						}
						else{
							csymbol="lss";
							out.write("<                   "+csymbol+"\r\n");
						}
					}
					else{
						if(c.ch=='>'){
							if(c.getchar()==-1){
								return -1;
							}
							if(c.ch=='='){
								csymbol="geq";
								out.write(">=                  "+csymbol+"\r\n");
								if(c.getchar()==-1){
									return -1;
								}
							}
							else{
								csymbol="gtr";
								out.write(">                   "+csymbol+"\r\n");
							}
						}
						else{
							csymbol=sym.simpleword[c.ch];
							out.write("simple word         "+csymbol+"\r\n");
							if(csymbol!="period"){
								if(c.getchar()==-1){
									return -1;
									}
								}
							}
						}
					}
				}
		}
		return -1;
	}
    public static void dod() throws IOException{
     	GetSym getsym=new GetSym("c:/testpas.pl0","c:/out.txt");
     	try{
	while(true){
			GetSym.getsym();
			if(GetSym.csymbol=="period"){
				break;
			}
		}
	//		GetSym getsym=new GetSym(filename.getText(),"c:/out.txt");
	//		getsym.dod();
	//		filename.setText(filename.getText()+"处理完毕");
	//		if(getsym.c.err.error.isEmpty()){
	//			errorinfor.setText("错误序号:"+getsym.c.err.error.get(0));
	//		}
			}catch(IOException ef){
				PL0.EventDo.errorinfor.setText("文件操作错误");
		}
     //	while(true){
     	//	 if(getsym.csymbol=="period"){
     	//	 	break;
     	//	 }
     //	}
   //  getsym.getsym();
    	out.close();
    }
}

⌨️ 快捷键说明

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