詞法分析程序的設計與實現

 詞法分析程序(Lexical Analyzer)要求:

- 從左至右掃描構成源程序的字符流

-  識別出有詞法意義的單詞(Lexemes

-  返回單詞記錄(單詞類別,單詞本身)

-  濾掉空格

-  跳過註釋

-  發現詞法錯誤

 

程序結構:

輸入:字符流(什麼輸入方式,什麼數據結構保存)

處理:

–遍歷(什麼遍歷方式)

–詞法規則

輸出:單詞流(什麼輸出形式)

–二元組

 

單詞類別:

1.標識符(10)

2.無符號數(11)

3.保留字(一詞一碼)

4.運算符(一詞一碼)

5.界符(一詞一碼)

單詞符號

種別碼

單詞符號

種別碼

begin

1

:

17

if

2

:=

18

then

3

<

20

while

4

<=

21

do

5

<>

22

end

6

>

23

l(l|d)*

10

>=

24

dd*

11

=

25

+

13

;

26

-

14

(

27

*

15

)

28

/

16

#

0

本案例使用Java編寫解析器

package com.lzh.springbootstudytestcache;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author lzh
 * create 2019-10-09-21:13
 */
public class homework3 {

    public static HashMap<Integer, String> hashMap = new HashMap<Integer, String>();

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("請輸入字符串");
        String str = sc.nextLine();
        parse(str);

    }

    public static void parse(String str) {

        hashMap.put(0, "#");
        hashMap.put(1, "begin");
        hashMap.put(2, "if");
        hashMap.put(3, "then");
        hashMap.put(4, "while");
        hashMap.put(5, "do");
        hashMap.put(6, "end");
        hashMap.put(10, "l(l|d)*");
        hashMap.put(11, "dd*");
        hashMap.put(13, "+");
        hashMap.put(14, "-");
        hashMap.put(15, "*");
        hashMap.put(16, "/");
        hashMap.put(17, ":");
        hashMap.put(18, ":=");
        hashMap.put(20, "<");
        hashMap.put(21, "<=");
        hashMap.put(22, "<>");
        hashMap.put(23, ">");
        hashMap.put(24, ">=");
        hashMap.put(25, "=");
        hashMap.put(26, ";");
        hashMap.put(27, "(");
        hashMap.put(28, ")");

        //測試數據
        //dsf 324 df(sdf if ad<= fds >sdf
        //while( 324 > f) do if( 22 <= a) asf

        //初始化
        String stmp = "" + str.toCharArray()[0];

        for (int i = 1; i < str.length(); i++) {

            //讀取到空格輸出
            if (str.toCharArray()[i] == ' ') {
//                System.out.println("stmp = "+ stmp);
                String regex = "\\d\\d*";
                boolean matches = stmp.matches(regex);
                //是數字
                if (matches) {
                    System.out.println("(11," + stmp + ")");
                }

                //判斷是否是關鍵詞
                for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
                    if (stmp.equals(entry.getValue())) {
                        System.out.println("(" + entry.getKey() + "," + entry.getValue() + ")");
                        stmp = "";
                        break;
                    }
                }

                stmp = "";
            } else {
                //讀取到非空格
                stmp += str.toCharArray()[i];

                for (Map.Entry<Integer, String> entry1 : hashMap.entrySet()) {
                    //讀取一個字符,如果是關鍵詞則輸出
                    if (("" + str.toCharArray()[i]).equals(entry1.getValue())) {
                        int flag = 0;

                        for (Map.Entry<Integer, String> entry11 : hashMap.entrySet()) {
                            //當前字符是關鍵詞,並且下一個也是關鍵詞
                            if (i < str.length() && ("" + str.toCharArray()[i + 1]).equals(entry11.getValue())) {

                                System.out.println("(" + hashMapGetKey("" + str.toCharArray()[i] + str.toCharArray()[i + 1]) + "," + str.toCharArray()[i] + str.toCharArray()[i + 1] + ")");
                                stmp = "";
                                i += 1;
                                flag = 1;
                                break;
                            }
                        }

                        //不是兩個連接在一起的關鍵詞
                        /*if (flag == 0) {
                            System.out.println("(" + entry1.getKey() + "," + entry1.getValue() + ")");
                        }*/
                        break;
                    } else {
                        //不是關鍵詞,追加
                        //判斷追加後是否爲關鍵詞
                        for (Map.Entry<Integer, String> entry2 : hashMap.entrySet()) {
                            if (stmp.equals(entry2.getValue())) {
                                System.out.println("(" + entry2.getKey() + "," + entry2.getValue() + ")");
                                stmp = "";
                                break;
                            }
                        }
                    }
                }
            }
        }
    }

    //通過鍵獲取值
    public static int hashMapGetKey(String value) {
        for (Map.Entry<Integer, String> integerStringEntry : hashMap.entrySet()) {
            if ((integerStringEntry.getValue()).equals(value)) {
                return integerStringEntry.getKey();
            }
        }
        return -1;
    }

}

 

 

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