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

📄 pretreatment.java

📁 实现算法与界面设计的完全分离。很好的封装自己的词法分析程序
💻 JAVA
字号:


import java.io.File;
import java.io.FileNotFoundException;
public class Pretreatment {
 private String tmpString;
 private String outputString;
 private int BUFFER_SIZE = 100;
 private AccidenceAnalyser aa;
 public InputBuffer inputBuffer; //输入缓冲区--共享
 private java.io.File SourceFile; //文件对象
 private java.io.RandomAccessFile randomAFile; //随机文件对象
 public static int fileRow = 0;
 
 int cmtIndex0 = -1;  //用来标记 字符串引号 "
 int cmtIndex4 = -1;
  
 int cmtIndex1 = -1;  //用来标记 注释符号 //
 int cmtIndex2 = -1;  //用来标记 注释符号 /* */
  
 boolean isCmt1 = false;  //用来标记 注释符号 //
 boolean isCmt2 = false;  //用来标记 注释符号 /* */

 public Pretreatment(File SourceFile, AccidenceAnalyser aa) {
 try {
 this.SourceFile = SourceFile;
 this.randomAFile = new java.io.RandomAccessFile(this.SourceFile, "r");
 }
 catch (FileNotFoundException e) {
 e.printStackTrace(System.err);
 }
 this.aa = aa;
 inputBuffer = aa.csbFactory.createInputBuffer(BUFFER_SIZE);
 System.out.println("预处理器已经创建!");
 }

 public void putSourceToINBuffer(String tmpString) {
 this.inputBuffer.Data = tmpString.toCharArray();
 }

 public void putFinToSCBuffer(String filtratedString) {
 aa.scaner.scanBuffer.Data = filtratedString.toCharArray();
 }

 public void controlThread() {
 //int intLength;
 //int resCounter = 0;
 String tmpString;
 String filtratedString;
 System.out.println("------------------开始单词分析------------------");
 try {
 if (SourceFile.exists()) { //文件存在
 //读文件内容到缓冲区
 while ( (tmpString = this.randomAFile.readLine()) != null) {
 ++fileRow;
 //分割符
 System.out.println("------------------begin row " + this.fileRow +
 "------------------");
 //开始这一行分析
 System.out.println("正在处理行: " + tmpString);
 //放入输入缓冲区
 this.putSourceToINBuffer(tmpString);
 //处理字符串
 //filtratedString = this.filtrateSource(this.inputBuffer.Data);
 filtratedString = String.valueOf(this.inputBuffer.Data).trim();
 
 cmtIndex2 = filtratedString.indexOf("*/");
 
 if((cmtIndex2 != -1) && isCmt2){
  isCmt2 = false;
  filtratedString = filtratedString.substring(cmtIndex2+2);
  System.out.println("已过滤句子: " + filtratedString); 
  this.putFinToSCBuffer(filtratedString);
  aa.controlThread();
  continue;
 }
 
 if(isCmt2) continue;
 
 cmtIndex0  = filtratedString.indexOf("\"");  //出现左引号"的位置
 
 cmtIndex1 = filtratedString.indexOf("//"); 
 cmtIndex2 = filtratedString.indexOf("/*"); 
 
 cmtIndex4 = filtratedString.indexOf("\"",cmtIndex0+1);  //出现右引号"的位置
 
 if( !((cmtIndex0 < cmtIndex1)  &&   (cmtIndex1 < cmtIndex4))  ){ 
  //  "//" 不是在两个引号之间
  
  if(cmtIndex1 != -1){
   if( (cmtIndex1 < cmtIndex2) || (cmtIndex2 == -1)) 
    if(cmtIndex1 > 0){
    filtratedString = filtratedString.substring(0,cmtIndex1);
	   System.out.println("已过滤句子: " + filtratedString); 
	   this.putFinToSCBuffer(filtratedString);
	   aa.controlThread();
	   continue;
    }
  }
  }
  if( !((cmtIndex0 < cmtIndex2)  &&   (cmtIndex2 < cmtIndex4))  ){  
   //  "/*" 不是在两个引号之间
   
  if(cmtIndex2 != -1){
   if(cmtIndex2 > 0){
	  filtratedString = filtratedString.substring(0,cmtIndex2);
	   System.out.println("已过滤句子: " + filtratedString); 
	   this.putFinToSCBuffer(filtratedString);
	   aa.controlThread();
   }
   isCmt2 = true;
   continue;
  }
 } 
 
 System.out.println("已过滤句子: " + filtratedString);
 //放入扫描缓冲区
 this.putFinToSCBuffer(filtratedString);
 aa.controlThread();
 }
 System.out.println("------------------分析完毕------------------");
 }
 else { //文件不存在
 System.err.println("[ERROR]源文件不存在!");
 }
 }
 catch (Exception e) {
 e.printStackTrace(System.err);
 }
 }

 public void startPretreatment() {
 this.controlThread();
 }
}

⌨️ 快捷键说明

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