編譯原理詞法分析 java

 

輸出結果

 

實驗報告

  • 實驗目的
      1. 學會針對DFA轉換圖實現相應的高級語言源程序。
      2. 深刻領會狀態轉換圖的含義,逐步理解有限自動機。
      3. 掌握手工生成詞法分析器的方法,瞭解詞法分析器的內部工作原理。
  • 實驗內容

(自己設計的某種計算機語言)的編譯程序的詞法分析部分實現。

從左到右掃描每行該語言源程序的符號,拼成單詞,換成統一的內部表示(token)送給語法分析程序。

爲了簡化程序的編寫,有具體的要求如下:

  1. 數包含整型和浮點型。
  2. 空白符僅僅是空格、回車符、製表符。
  3. 代碼是自由格式。
  4. 註釋應放在花括號之內,並且不允許嵌套

給出自己的記號定義。舉例如下:

 

定義保留字以及特殊符號

 

static String keyWord[] = {"else", "end", "if", "read", "repeat", "then", "until", "write"};
static String symbol[] = {"%", "*", "+", "-", "/", ":", ";", "<", "=", ">"};

  • 實驗要求

要求實現編譯器的以下功能:

    1. 按規則拼單詞,並轉換成二元式形式
    2. 刪除註釋行

else  if (contents.get(i)=='{'){      //刪除註釋行
   
for(int i2 =i;i2<contents.size();i2++){
        if(contents.get(i2)=='}'){
            i=i2;
            break;
        }
    }

 

    1. 刪除空白符 (空格、回車符、製表符)

 

    1. 列表打印源程序,按照源程序的行打印,在每行的前面加上行號,並且打印出每行包含的記號的二元形式

 

//讀取文件

file = new File("D:\\data.txt");

StringBuffer sbf = new StringBuffer();



try {

    BufferedReader reader = new BufferedReader(new FileReader(file));

    String tempStr;

    int n = 1;

    while ((tempStr = reader.readLine()) != null) {

        System.out.print("" + n + "\t");

        System.out.println(tempStr);

        n++;

    }

} catch (FileNotFoundException e) {

    e.printStackTrace();

} catch (IOException e) {

    e.printStackTrace();

}

 

    1. 發現並定位錯誤

給出詞法分析程序使用的狀態圖(DFA)

 

自己設計的詞法分析進行具體的要求:

程序源碼

public class WordAnalysis {

    static File file;

    static String keyWord[] = {"else", "end", "if", "read", "repeat", "then", "until", "write"};

    static String symbol[] = {"%", "*", "+", "-", "/", ":", ";", "<", "=", ">"};

    private static void getToken() {

        file = new File("D:\\data.txt");

        ArrayList<Character> contents = new ArrayList<>();

        int n = 1;

        try {

            Reader reader  = new InputStreamReader(new FileInputStream(file));

            int tempchar;

            while ((tempchar = reader.read()) != -1) {

                if (((char) tempchar) != '\r') {

                    contents.add((char) tempchar);

                }

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.print(""+n+"\t");

        for (int i = 0; i < contents.size(); i++) {

            if(contents.get(i)=='\n'&&i!=contents.size()-1){          //判斷第幾行

                System.out.print("\n"+(++n)+"\t");

            }else if (isCharacter(contents.get(i))){   //判斷是否爲標識符

                String tok="";

                tok = tok+contents.get(i);

                while (isCharacter(contents.get(++i))){

                    tok = tok+contents.get(i);

                }

                i--;

                if(!iskeyword(tok)){

                    System.out.print("(標識符,"+tok+")\t");

                }

            }else if(isNumber(contents.get(i))){    //判斷是否爲數字

                String tok="";

                tok = tok+contents.get(i);

                while (isNumber(contents.get(++i))){

                    tok = tok+contents.get(i);

                }

                i--;

                System.out.print("(數字,"+tok+")\t");

            }else  if (contents.get(i)=='{'){      //刪除註釋行

                for(int i2 =i;i2<contents.size();i2++){

                    if(contents.get(i2)=='}'){

                        i=i2;

                        break;

                    }

                }

            }else {

                isSymbol(contents.get(i));

            }

        }



    }



    //是否爲符號

    private static boolean isSymbol(Character character) {

        for (int i1 = 0; i1 < symbol.length; i1++) {

            if(symbol[i1].equals(character.toString())){

                System.out.print("(符號,"+character+")\t");

            }

        }

        return false;

    }



    //是否爲關鍵字

    private static boolean iskeyword(String tok) {

        for (int i1 = 0; i1 < keyWord.length; i1++) {

            if(keyWord[i1].equals(tok)){

                System.out.print("(關鍵字,"+tok+")\t");

                return true;

            }

        }

        return false;

    }

    private static void printSource() {

        //讀取文件

        file = new File("D:\\data.txt");

        StringBuffer sbf = new StringBuffer();

        try {

            BufferedReader reader = new BufferedReader(new FileReader(file));

            String tempStr;

            int n = 1;

            while ((tempStr = reader.readLine()) != null) {

                System.out.print("" + n + "\t");

                System.out.println(tempStr);

                n++;

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }



    }



    // 判斷是否爲數字

    private static boolean isNumber(char c) {

        if (c <= '9' && c >= '0') {

            return true;

        } else {

            return false;

        }

    }



    //判斷是否爲字符

    private static boolean isCharacter(char c) {

        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {

            return true;

        } else {

            return false;

        }

    }

    public static void main(String[] args) {

        printSource();    //打印源程序

        getToken();       //源程序處理

    }



}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章