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

📄 cfjframe.java

📁 用java语言实现的简单词法分析器 实现了界面操作
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				//this.finalAccidence = "";
			}
			try{
				out.close();
				tofile.close();
				in.close();
				file_reader.close();
			}
			catch(IOException exp){
			}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
	public String toWord(String ss) {
		//找出注释
        int temp = ss.indexOf("/*");
        int temp1 = 0;
        if(temp != -1)
        {
          if((temp1 = ss.indexOf("*/")) != -1)
          {
          	String ss1 = ss.substring(temp+2,temp1);
          	this.finalAccidence = this.finalAccidence + "\n[注释]: " + ss1.toUpperCase();
          	return this.finalAccidence;
          }
        }
        
		this.finalAccidence = "";
		this.sChar = ss.toCharArray();
		this.senLength = sChar.length;
		int i;
		index = 0;
		while(this.index<=this.senLength) {
			this.word = "";
			this.CHAR = this.GETBC(sChar);
    		if(this.CHAR == ';'){
    			System.out.println("ttttttttttt");
    			break;
    		}
    		//进入状态判断
      		switch (this.CHAR) {
        		//judge letter
				case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':
				case 'l':case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case 'v':case 'w':case 'x':case 'y':
				case 'z':case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G':case 'H':case 'I':case 'J':case 'K':case 'L':case 'M':
				case 'N':case 'O':case 'P':case 'Q':case 'R':case 'S':case 'T':case 'U':case 'V':case 'W':case 'X':case 'Y':case 'Z':
				this.word = this.CONTACT(this.word, CHAR);
				if(this.index==this.senLength){
            		this.index++;
            		if(this.kdw.isKedWord(this.word)){
						this.finalAccidence = this.finalAccidence + "[保留字: " + this.word.toUpperCase() + "]";
					}
					else{
						this.finalAccidence = this.finalAccidence + "[标识符: " + this.word + "]";
					}
					this.kdw.flag = false;
					System.out.println(word);
					this.word = "";
            		break;
            	}
				this.CHAR = this.GETCHAR(this.sChar);
				while (this.ISLETTER(this.CHAR) || this.ISDIGIT(this.CHAR)) {
            		this.word = this.CONTACT(this.word, this.CHAR);
            		if(this.index==this.senLength){
            			this.index++;
            			break;
            		}	
            		this.CHAR = this.GETCHAR(this.sChar);            		            		
          		}
          		this.RETRACT();
				if(this.kdw.isKedWord(this.word)){
					this.finalAccidence = this.finalAccidence + "[保留字: " + this.word.toUpperCase() + "]";
				}
				else{
					this.finalAccidence = this.finalAccidence + "[标识符: " + this.word + "]";
				}
				this.kdw.flag = false;
				System.out.println(word);
				this.word = "";
				break;
				case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8': case '9':
          		this.word = this.CONTACT(word, CHAR);
          		System.out.println(word);
          		if(this.index==this.senLength){
            		this.index++;
            		this.finalAccidence = this.finalAccidence + "[数字常量: " +
              			this.word + "]";
          			this.word = "";
            		break;
            	}
          		CHAR = this.GETCHAR(sChar);
          		while (this.ISDIGIT(CHAR) || this.ISDOT(CHAR)) {
            		this.word = this.CONTACT(word, CHAR);
            		if(this.index==this.senLength){
            			this.index++;
            			break;
            		}            		
            		CHAR = this.GETCHAR(sChar);            		
          		}
          		this.RETRACT();
          		this.finalAccidence = this.finalAccidence + "[数字常量: " +
              		this.word + "]";
          		this.word = "";
          		break;
        		case '=':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[等号: " +
              		this.word + "]";
          		this.word = "";
          		break;
        		case '+':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[加号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case '-':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[减号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case '*':
          		this.word = this.CONTACT(word, CHAR);
          		CHAR = this.GETCHAR(sChar);
          		if (CHAR == '*') {
            		this.word = this.CONTACT(word, CHAR);
            		this.finalAccidence = this.finalAccidence + "[乘方:" +
                		this.word + "] ";
          		}
         		else {
            		this.finalAccidence = this.finalAccidence + "[乘号:" +
                		this.word + "] ";
          		}
          		this.word = "";
          		break;
        		case '/':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[除号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case ',':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[逗号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case '(':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[左括号:" +
              		this.word + "] ";
          		this.word = "";
         	 	break;
        		case ')':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[右括号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case '{':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[左大括号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case '}':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[右大括号:" +
              		this.word + "] ";
          		this.word = "";
         		break;
        		case '[':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[左中括号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case ']':
          		this.word = this.CONTACT(word, CHAR);
          		this.finalAccidence = this.finalAccidence + "[右中括号:" +
              		this.word + "] ";
          		this.word = "";
          		break;
        		case '#':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[警号:" + 
          	  		this.word + "] ";
          		this.word = "";
          		break;
         		case '"':
          		CHAR = this.GETCHAR(sChar);
          		while(CHAR != '"'){
          			this.word = this.CONTACT(word, CHAR);
            		CHAR = this.GETCHAR(sChar);
          		} 
          		this.finalAccidence = this.finalAccidence + "[字符串常量:" +
              		this.word + "] ";
          		this.word = "";
          		break;          
        		case ':':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[冒号:" + 
          	  		this.word + "] ";
          		this.word = "";
          		break;
        		case '<':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[左尖扩号:" + 
          	  		this.word + "] ";
          		this.word = "";
          		break;
        		case '>':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[右尖括号:" + 
          	  		this.word + "] ";
          		this.word = "";
          		break;
        		case '%':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[百分号:" + 
          	  		this.word + "] ";
          		this.word = "";
          		break;
        		case '&':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[与/取值符:" + 
          	  		this.word + "] ";
          		this.word = "";
          		break;
        		case '\n':
          		this.word = this.CONTACT(word,CHAR);
          		this.finalAccidence = this.finalAccidence + "[换行:" + 
          	  		this.word + "] ";
          		this.word = "";
         		break;
        		case 39:
          		CHAR = this.GETCHAR(sChar);
          		while(CHAR != 39){
          			this.word = this.CONTACT(word, CHAR);
            		CHAR = this.GETCHAR(sChar);
          		} 
          		this.finalAccidence = this.finalAccidence + "[字符常量:" +
              		this.word + "] ";
          		this.word = "";          		
          		break;
          		default:
          		this.word = "";
          		flag = true;
          		break;
			}
		}
		return this.finalAccidence;
	}
	public char GETBC(char[] sChar) {
    	try {
      		while ( (sChar[this.index]) == ' ') {
        	this.index++;
      		}
      		this.index++;
    	}
    	catch (java.lang.ArrayIndexOutOfBoundsException e) {
      		return ';'; //表示此行已经结束
    	}
    	return sChar[index - 1];
    }
    public char GETCHAR(char[] sChar) {
    	this.next();
    	return sChar[this.index - 1];
  	}
  	public void next() {
    	this.index++;
  	}
  	public boolean ISLETTER(char letter) {
    	return java.lang.Character.isLetter(letter);
  	}
  	public boolean ISDIGIT(char letter) {
    	return java.lang.Character.isDigit(letter);
  	}
  	public void RETRACT() {
    	this.index--;
  	}
  	public String CONTACT(String word, char CHAR) {
    	String tmpS = this.word + String.valueOf(CHAR);
    	word = tmpS;
    	return word;
  	}
  	public boolean ISDOT(char letter) {
  		if(letter == '.'){
  			return true;
  		}
  		else{
  			return false;
  		}
  	}
}

//保留字表类
class KedWord {
	boolean flag = false;
	//保留字表
	String[] kedWord = {
		"int","char","double","auto","struct","break","else","long","switch","volatile","do",
		"case","enum","register","typedef","extern","void","default","goto","if","while","static",
		"return","union","const","float","short","unsigned","continue","for","signed","sizeof"
	};
	public boolean isKedWord(String word){
		int num = (int)kedWord.length;
		for(int i=0;i<num;i++){
			if(word.equals(kedWord[i])){
				this.flag = true;
				System.out.print("OK");
			}
		}
		return this.flag;
	}
}

⌨️ 快捷键说明

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