• SLR(1)语法分析(JAVA实现)


    要求



    代码

    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Stack;
    
    public class SLRFX {
    
        private List<String> wf = new ArrayList<String>();
        private HashMap<String,List<String>> follow = new HashMap<String, List<String>>();
        private String[][] table = new String[12][9];
        private Stack<String> stateStack = new Stack<String>();
        private Stack<String> charStack = new Stack<String>();
    
    
        public SLRFX(){
            init();
        }
    
        /**
         * 初始化
         */
        private void  init(){
            wf.add("S->E");
            wf.add("E->E+T");
            wf.add("E->T");
            wf.add("T->T*F");
            wf.add("T->F");
            wf.add("F->(E)");
            wf.add("F->i");
            List<String> Sfl = new ArrayList<String>();
            List<String> Efl = new ArrayList<String>();
            List<String> Tfl = new ArrayList<String>();
            List<String> Ffl = new ArrayList<String>();
            Sfl.add("#");
            Efl.add("#");
            Efl.add(")");
            Efl.add("+");
            Tfl.add("#");
            Tfl.add(")");
            Tfl.add("+");
            Tfl.add("*");
            Ffl.addAll(Tfl);
            follow.put("S",Sfl);
            follow.put("E",Efl);
            follow.put("T",Tfl);
            follow.put("F",Ffl);
            for (int i = 0; i < 12; i++) {
                for (int j = 0; j < 9; j++) {
                    table[i][j] = "";
                }
            }
            table[0][0] = "s5";
            table[0][3] = "s4";
            table[0][6] = "1";
            table[0][7] = "2";
            table[0][8] = "3";
            table[1][1] = "s6";
            table[1][5] = "acc";
            table[2][1] = "r2";
            table[2][2] = "s7";
            table[2][4] = "r2";
            table[2][5] = "r2";
            table[3][1] = "r4";
            table[3][2] = "r4";
            table[3][4] = "r4";
            table[3][5] = "r4";
            table[4][0] = "s5";
            table[4][3] = "s4";
            table[4][6] = "8";
            table[4][7] = "2";
            table[4][8] = "3";
            table[5][1] = "r6";
            table[5][2] = "r6";
            table[5][4] = "r6";
            table[5][5] = "r6";
            table[6][0] = "s5";
            table[6][3] = "s4";
            table[6][7] = "9";
            table[6][8] = "3";
            table[7][0] = "s5";
            table[7][3] = "s4";
            table[7][8] = "10";
            table[8][1] = "s6";
            table[8][4] = "s4";
            table[9][1] = "r1";
            table[9][2] = "s7";
            table[9][4] = "r1";
            table[9][5] = "r1";
            table[10][1] = "r3";
            table[10][2] = "r3";
            table[10][4] = "r3";
            table[10][5] = "r3";
            table[11][1] = "r5";
            table[11][2] = "r5";
            table[11][4] = "r5";
            table[11][5] = "r5";
        }
    
        /**
         * 展示文法的信息
         */
        private void showTips(){
            System.out.println("输入的文法:");
            for (String s : wf) {
                System.out.println(s);
            }
            System.out.println("VT:+	*	(
    )	i	#");
            System.out.println("VN:E	T	F");
            System.out.println("	i	+	*	(	)	#	E	T	F");
            for (int i = 0; i < 12; i++) {
                System.out.print(i);
                for (int j = 0; j < 9; j++) {
                    System.out.print("	" + table[i][j]);
                }
                System.out.println();
            }
        }
    
        /**
         * 删除字符串的第一个字符
         * @param str
         * @return
         */
        private String deleteFirstChar(String str){
            String[] strings = str.split("||");
            String newStr = "";
            for (int i = 1; i < strings.length; i++) {
                newStr += strings[i];
            }
            return newStr;
        }
    
        /**
         * 分析过程
         * @param jz
         */
        private void fx(String jz){
            List<String> stateStr = new ArrayList<String>();
            List<String> charStr = new ArrayList<String>();
            stateStack.push("0");
            stateStr.add("0");
            charStack.push("#");
            charStr.add("#");
            String sr = jz;
            boolean isok = false;
            int bz = 1;
            System.out.println("步骤		状态栈		符号栈		输入串		ACTION		GOTO		动作");
            while (!isok){
                int topState = Integer.valueOf(stateStack.peek()).intValue();
                char nowChar = sr.charAt(0);
                String what = table[topState][getIndex(nowChar)];
                if (what.startsWith("s")){
                    //移进
                    String[] split = what.split("||");
                    stateStack.push(split[1]);
                    stateStr.add(split[1]);
                    charStack.push(nowChar + "");
                    charStr.add(nowChar + "");
                    System.out.println(bz + "		" + stateStr.toString() + "		" + charStr  + "		" + sr +
                            "		" + what + "				" + "0" + "		移进");
                    //输入串要减少
                    sr = deleteFirstChar(sr);
                }else if (what.startsWith("r")){
                    //规约
                    String[] split = what.split("||");
                    String[] css = wf.get(Integer.valueOf(split[1])).split("->");
                    String VT = css[0];
                    int popNum = css[1].length();
                    //出栈
                    for (int i = 0; i < popNum; i++) {
                        stateStack.pop();
                        stateStr.remove(stateStr.size()-1);
                        charStack.pop();
                        charStr.remove(charStr.size()-1);
                    }
                    int newTopState = Integer.valueOf(stateStack.peek()).intValue();
                    String newState = table[newTopState][getIndex(VT.charAt(0))];
                    stateStack.push(newState);
                    stateStr.add(newState);
                    charStack.push(VT);
                    charStr.add(VT);
                    System.out.println(bz + "		" + stateStr.toString() + "		" + charStr.toString() + "		" +sr +
                            "		" + what + "				" + newState + "		规约");
                }else if (what.equals("acc")){
                    isok = true;
                    System.out.println(bz + "		" + stateStr.toString() + "		" + charStr.toString() + "		" +sr +
                            "		" + "acc" + "				" + "0" + "		接受");
                }else {
                    System.err.println("分析错误!输入的句子不符合语法!");
                    System.exit(-1);
                }
                bz++;
            }
            System.out.println("分析完成!");
        }
    
        private int getIndex(char t){
            switch (t){
                case 'i':return 0;
                case '+':return 1;
                case '*':return 2;
                case '(':return 3;
                case ')':return 4;
                case '#':return 5;
                case 'E':return 6;
                case 'T':return 7;
                case 'F':return 8;
            }
            return -1;
        }
    
        public void run(String jz){
            showTips();
            fx(jz);
        }
    
    
    }
    
    
    

    测试代码:

    
    public class TestSLR {
    
    
        public static void main(String[] args) {
            SLRFX slrfx = new SLRFX();
            slrfx.run("i+i*i#");
        }
    
    }
    
    

    结果


  • 相关阅读:
    [Vue]使用 vue-i18n 切换中英文
    轻松实现Ecshop商城多语言切换
    关于ecshop的mobile里user.php登录和注册验证码不显示
    ecshop用户中心菜单选项显示内容标签
    ECSHOP的JS文件代入问题
    在PHP中gmtime()与time()区别
    ECSHOP中 {insert name='ads' id=$ads_id num=$ads_num}含义
    ECshop 迁移到 PHP7版本时遇到的兼容性问题,ecshopphp7
    App开放接口api安全性—Token签名sign的设计与实现
    台湾拼音对照表
  • 原文地址:https://www.cnblogs.com/wuren-best/p/14000874.html
Copyright © 2020-2023  润新知