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

📄 identifieranalyse.java

📁 词法分析器 可以识别c++的源码,自动生成词法分析表, 在分析结束后会指出源代码中可能的错误 待分析的源代码要以 *源代码.txt的格式保存
💻 JAVA
字号:
package accidenceAnalyse;

import java.io.*;
import java.util.regex.*;
import java.util.*;

public class IdentifierAnalyse implements TokenAnalyse
{
	class IdentifierTable {
		private int ID ;
		private String name ;
		public IdentifierTable(int ID , String name) {
			this.ID = ID ;
			this.name = name ;
		}
		public int getID() {
			return this.ID ;
		}
		public String getname() {
			return this.name ;
		}
	}
	
	public IdentifierTable getIdentifierTable(int ID , String name) {
		return new IdentifierTable(ID , name) ;
	}
	
	public IdentifierAnalyse (){
		h_keyAnalyse = new KeyAnalyse();
	}
	/*   标识符的词法分析*/
	public void analyse(String str ,ArrayList<TokenTable> list , ArrayList<String> errorList , int textLine) {
		boolean isKey = h_keyAnalyse.keyAnalyse(str, list, errorList, textLine);
		if (isKey)
			return;
		else
		{
			Pattern  pattern  = Pattern.compile(STRING);
			Matcher matcher = pattern.matcher(str);
			if(matcher.find())
				if(str.substring (  matcher.start(),matcher.end() ).equals(str)){
					this.name = str;
					this.ID = 2;
					list.add(new TokenTable(this.ID,this.name));
					insertIdentifierTable (this.ID , this.name);
				}
				else{
					String errorStr = "第"+textLine+"行标识符: "+str+"不合法";
					errorList.add(errorStr);
				}
			else{
				String errorStr = "第"+textLine+"行标识符: "+str+"不合法";
				errorList.add(errorStr);
			}
		}
	}
	
	private void insertIdentifierTable (int ID , String name) {
		identifierTable.add(getIdentifierTable(ID , name));
	}
	
	public void getArrayList (String filename) throws FileNotFoundException {
		try{
			DataOutputStream out = new DataOutputStream(new FileOutputStream("accidenceAnalyse\\accidenceAnalyseTable\\"+filename+"_IdentifierTable.txt"));
			for (IdentifierTable t : identifierTable){
				try {
					out.writeUTF(""+t.getID());
					out.writeUTF(t.getname());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			out.close() ;
		}catch(Exception e){}
	}
	/* 标识符的正则表达式  */ 
	static final String STRING = "[a-zA-Z][_\\w]*|_[_\\w]*[\\w]";
	private KeyAnalyse h_keyAnalyse;
	/*  标识符的名字*/
	protected int ID = 0;
	protected String name = "false";
	private static ArrayList <IdentifierTable> identifierTable = new ArrayList<IdentifierTable>() ;
}

⌨️ 快捷键说明

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