java編寫詞法分析器


詞法分析器就是通過掃描一段程序判斷是否是關鍵字、標識符、常數、分界符、運算符。一般分為一符一種和經典五中;

這里我用的是經典五中,此詞法分析器是用java編寫的;

/*
保留字|關鍵字:1
操作符|運算符:2
分界符:3
標識符:4
常數:5
無識別:6
*/

主要代碼為:

/**
 * 此程序是通過將文件的字符讀取到字符數組中去,然后遍歷數組,將讀取的字符進行
 * 分類並輸出
 * @author
 *
 */
public class WordAnalyze {
    private String keyWord[] = {"break","include","begin","end","if","else","while","switch"};
    private char ch;
    //判斷是否是關鍵字
    boolean isKey(String str)
    {
        for(int i = 0;i < keyWord.length;i++)
        {
            if(keyWord[i].equals(str))
                return true;
        }
        return false;
    }
    //判斷是否是字母
    boolean isLetter(char letter)
    {
        if((letter >= 'a' && letter <= 'z')||(letter >= 'A' && letter <= 'Z'))
            return true;
        else
            return false;
    }
    //判斷是否是數字
    boolean isDigit(char digit)
    {
        if(digit >= '0' && digit <= '9')
            return true;
        else
            return false;
    }
    //詞法分析
    void analyze(char[] chars)
    {
        String arr = "";
        for(int i = 0;i< chars.length;i++) {
            ch = chars[i];
            arr = "";
            if(ch == ' '||ch == '\t'||ch == '\n'||ch == '\r'){}
            else if(isLetter(ch)){
                while(isLetter(ch)||isDigit(ch)){    
                    arr += ch;
                    ch = chars[++i];
                }
                //回退一個字符
                i--;
                if(isKey(arr)){
                    //關鍵字
                    System.out.println(arr+"\t4"+"\t關鍵字");
                }
                else{
                    //標識符
                    System.out.println(arr+"\t4"+"\t標識符");
                }
            }
            else if(isDigit(ch)||(ch == '.'))
            {
                while(isDigit(ch)||(ch == '.'&&isDigit(chars[++i])))
                {
                    if(ch == '.') i--;
                    arr = arr + ch;
                    ch = chars[++i];
                }
                //屬於無符號常數
                System.out.println(arr+"\t5"+"\t常數");
            }
            else switch(ch){
                //運算符
                case '+':System.out.println(ch+"\t2"+"\t運算符");break;
                case '-':System.out.println(ch+"\t2"+"\t運算符");break;
                case '*':System.out.println(ch+"\t2"+"\t運算符");break;
                case '/':System.out.println(ch+"\t2"+"\t運算符");break;
                //分界符
                case '(':System.out.println(ch+"\t3"+"\t分界符");break;
                case ')':System.out.println(ch+"\t3"+"\t分界符");break;
                case '[':System.out.println(ch+"\t3"+"\t分界符");break;
                case ']':System.out.println(ch+"\t3"+"\t分界符");break;
                case ';':System.out.println(ch+"\t3"+"\t分界符");break;
                case '{':System.out.println(ch+"\t3"+"\t分界符");break;
                case '}':System.out.println(ch+"\t3"+"\t分界符");break;
                //運算符
                case '=':{
                            ch = chars[++i];
                            if(ch == '=')System.out.println("=="+"\t2"+"\t運算符");
                            else {
                                System.out.println("="+"\t2"+"\t運算符");
                                i--;
                            }
                         }break;
                case ':':{
                            ch = chars[++i];
                            if(ch == '=')System.out.println(":="+"\t2"+"\t運算符");
                            else {
                                System.out.println(":"+"\t2"+"\t運算符");
                                i--;
                            }
                         }break;
                case '>':{
                            ch = chars[++i];
                            if(ch == '=')System.out.println(">="+"\t2"+"\t運算符");
                            else {
                                System.out.println(">"+"\t2"+"\t運算符");
                                i--;
                            }
                         }break;
                case '<':{
                            ch = chars[++i];
                            if(ch == '=')System.out.println("<="+"\t2"+"\t運算符");
                            else {
                                System.out.println("<"+"\t2"+"\t運算符");
                                i--;
                            }
                         }break;
                //無識別
                default: System.out.println(ch+"\t6"+"\t無識別符");
            }
        }
    }
public static void main(String[] args) throws Exception {
    File file = new File("E:\\data.txt");//定義一個file對象,用來初始化FileReader
    FileReader reader = new FileReader(file);//定義一個fileReader對象,用來初始化BufferedReader
    int length = (int) file.length();
    //這里定義字符數組的時候需要多定義一個,因為詞法分析器會遇到超前讀取一個字符的時候,如果是最后一個
    //字符被讀取,如果在讀取下一個字符就會出現越界的異常
    char buf[] = new char[length+1];
    reader.read(buf);
    reader.close();
    new WordAnalyze().analyze(buf);
    
}
}

 運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM