編譯原理 實驗二 語法分析 算符優先分析程序JAVA實現(有償獲取全部源代碼)

(1)根據給定文法,先求出FirstVt和LastVt集合,構造算符優先關係表(要求算符優先關係表    輸出到顯示器或者輸出到文件)

(2)根據算法和優先關係表分析給定表達式是否是該文法識別的正確的算術表達式(要求輸出歸約過程)

(3)假如給定表達式文法爲:

G(E’): E’→#E#

          E→E+T | T

          T→T*F |F

          F→(E)|i

(4)分析的句子可爲:(i+i)*i和i+i)*i

有償獲取全部源代碼!

有償獲取全部源代碼!

有償獲取全部源代碼!

添加作者微信,價格私聊(備註學校+年級

每個學校每個年級值出售一次

價格公道,童叟無欺

部分程序截圖

 部分代碼

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

/**
 * Class Grammar, read grammar from file
 */
public class Grammar {
    private List<String> VT;//VT 終結符集合
    private List<String> VN;//VN 非終結符集合
    private String S;//開始符
    private Map<String, List<String>> P;//產生式集合

    public Grammar() {

        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(new File("D:\\IdeaProject\\CompilerTheory\\src\\cn\\wan\\experiment02\\grammar.txt")));
            VT = new ArrayList<>(Arrays.asList(bufferedReader.readLine().split(" ")));//read first line, get VT
            VN = new ArrayList<>(Arrays.asList(bufferedReader.readLine().split(" ")));//read second line, get V
            S = bufferedReader.readLine();// read third, get S
            P = new HashMap<>();
            String s;
            while ((s = bufferedReader.readLine()) != null) {//read surplus(sheng yu de)
                String[] split = s.split("->");
                if (P.containsKey(split[0])) {
                    //if exist key split[0]
                    P.get(split[0]).add(split[1]);
                } else {
                    List<String> strings = new ArrayList<>();
                    strings.add(split[1]);
                    P.put(split[0], strings);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("VT:" + VT);
        System.out.println("VN:" + VN);
        System.out.println("S:" + S);
        for (String key : P.keySet()) {
            System.out.println(key + "->" + P.get(key));
        }
    }

    public List<String> getVT() {
        return VT;
    }

    public List<String> getVN() {
        return VN;
    }

    public String getS() {
        return S;
    }

    public Map<String, List<String>> getP() {
        return P;
    }

    public static void main(String[] args) {
        new Grammar();
    }
}

程序運行結果截圖:

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