LR(0)分析法的实现

一、题目:根据LR(0)分析法控制程序:

和扩展文法:

及所对应的LR(0)分析表:

编程实现LR(0)分析程序,并输出符号串:a = abccd # 的分析过程。

二、源程序及运行结果截图

运行结果:

源代码:

import jdk.internal.util.xml.impl.Input;

import java.util.*;

public class LR0 {

     static    String[][] LRt = {//LR(0)分析表

                {"a2", "", "", "", "", "Z1", "", ""},
                {"", "", "", "", "OK", "", "", ""},
                {"", "b9", "c11", "", "", "", "", "", "B3"},
                {"", "b6", "c8", "", "", "", "A4", ""},
                {"", "", "", "d5", "", "", "", ""},
                {"a2B3A4d5", "a2B3A4d5", "a2B3A4d5", "a2B3A4d5", "a2B3A4d5", "", "", ""},
                {"", "", "c7", "", "", "", "", ""},
                {"b6c7", "b6c7", "b6c7", "b6c7", "b6c7", "", "", ""},
                {"c8", "c8", "c8", "c8", "c8", "", "", ""},
                {"", "b9", "c11", "", "", "", "", "B10"},
                {"b9B10", "b9B10", "b9B10", "b9B10", "b9B10", "", "", ""},
                {"c11", "c11", "c11", "c11", "c11", "", "", ""}

        };

    static Stack<String> stack = new Stack<String>();
    static int j = 0;
    static String s = new String();

    public static void main(String[] args) {

        final int a = 0;
        final int b = 1;
        final int c = 2;
        final int d = 3;
        final int e = 4;//此处e代表#号
        final int Z = 5;
        final int A = 6;
        final int B = 7;
        Scanner scanner = new Scanner(System.in);

        s = scanner.nextLine();

        stack.push("e");
        stack.push("0");

        System.out.println("分析栈" + "\t\t" + "w" + "\t" + "剩余串" + "\t" + "操作");

        while (true) {

            if (j == s.length()) break;

            if (stack.peek().contains("5") || stack.peek().contains("7") || stack.peek().contains("8") || stack.peek().contains("10") || stack.peek().contains("11")) {//判断是否为归约态,出栈并进行归约
                StringBuffer stringBuffer = new StringBuffer();
                for (String l ://查看栈中当前元素
                        stack) {
                    stringBuffer.append(l);
                }
                System.out.printf("%-12s",stringBuffer);

                if (s.charAt(j) >= 'a' && s.charAt(j) <= 'e' && stack.peek().contains("5")) {
                    ReduceShow(Integer.parseInt(stack.peek()));
                    PopStack();//出栈
                    stack.push("Z");
                    stack.push("1");
                    continue;
                } else if (s.charAt(j) >= 'a' && s.charAt(j) <= 'e' && stack.peek().contains("7")) {
                    ReduceShow(Integer.parseInt(stack.peek()));
                    PopStack();//出栈
                    stack.push("A");
                    stack.push("4");
                    continue;
                } else if (s.charAt(j) >= 'a' && s.charAt(j) <= 'e' && stack.peek().contains("8")) {
                    ReduceShow(Integer.parseInt(stack.peek()));
                    PopStack();//出栈
                    stack.push("A");
                    stack.push("4");
                    continue;
                } else if (s.charAt(j) >= 'a' && s.charAt(j) <= 'e' && stack.peek().contains("10")) {

                    ReduceShow(Integer.parseInt(stack.peek()));
                    PopStack();//出栈
                    stack.push("B");
                    stack.push("3");
                    continue;
                } else if (s.charAt(j) >= 'a' && s.charAt(j) <= 'e' && stack.peek().contains("11")) {

                    ReduceShow(Integer.parseInt(stack.peek()));
                    PopStack();//出栈
                    stack.push("B");
                    stack.push("10");

                    continue;

                }else {
                    System.out.println("错误!");
                    System.exit(0);
                }
            } else if (stack.peek().contains("0") || stack.peek().contains("1") || stack.peek().contains("2") || stack.peek().contains("3") || stack.peek().contains("4") || stack.peek().contains("6") || stack.peek().contains("9")) {//判断是否为移进态或接收态,是移进态查分析表移进,并读取下一个字符,是结束态正确结束
                StringBuffer stringBuffer = new StringBuffer();
                for (String l ://查看栈中当前元素
                        stack) {
                    stringBuffer.append(l);
                }
                System.out.printf("%-12s",stringBuffer);

                if (Integer.parseInt(stack.peek()) == 0) {
                    if (s.charAt(j) == 'a') {

                        InputShow(a);//显示操作
                        PushStack(a);//入栈
                        continue;
                    }else if (s.charAt(j) == 'Z'){
                        InputShow(Z);//显示操作
                        PushStack(Z);//入栈
                        continue;
                    }else {
                        System.out.println("错误!");break;
                    }

                    }else if (Integer.parseInt(stack.peek()) == 1 && s.charAt(j) == 'e') {//结束态
                    System.out.printf("%-4c",s.charAt(j));//输出w
                    StringBuffer string = new StringBuffer();
                    for (int i = j + 1; i < s.length(); i++) {//输出剩余串
                        string.append(s.charAt(i));
                    }
                    System.out.printf("%-8s",string);
                        System.out.println("OK");//输出操作
                        System.exit(0);//正确结束并退出
                        continue;
                    }else if (Integer.parseInt(stack.peek()) == 2) {

                        if (s.charAt(j) == 'b') {

                            InputShow(b);//显示操作
                            PushStack(b);//入栈

                        continue;

                        }else if (s.charAt(j) == 'c') {

                            InputShow(c);//显示操作
                            PushStack(c);//入栈
                            continue;
                        }else if(s.charAt(j) == 'B'){
                            InputShow(B);//显示操作
                            PushStack(B);//入栈
                            continue;
                        }
                        else {
                            System.out.println("错误!");break;
                        }

                    }else if (Integer.parseInt(stack.peek()) == 3) {
                        if (s.charAt(j) == 'b') {

                            InputShow(b);//显示操作
                            PushStack(b);//入栈

                        }
                        if (s.charAt(j) == 'c') {

                            InputShow(c);//显示操作
                            PushStack(c);//入栈

                        }else if (s.charAt(j) == 'A'){

                            InputShow(A);//显示操作
                            PushStack(A);//入栈

                        }
                        else {
                            System.out.println("错误!");break;
                        }
                        continue;
                    }else if (Integer.parseInt(stack.peek()) == 4 && s.charAt(j) == 'd') {

                        InputShow(d);//显示操作
                        PushStack(d);//入栈

                        continue;
                    }else if (Integer.parseInt(stack.peek()) == 6 && s.charAt(j) == 'c') {

                        InputShow(c);//显示操作
                        PushStack(c);//入栈

                        continue;
                    }else if (Integer.parseInt(stack.peek()) == 9) {
                        if (s.charAt(j) == 'b') {

                            InputShow(b);//显示操作
                            PushStack(b);//入栈

                        }
                        if (s.charAt(j) == 'c') {

                            InputShow(c);//显示操作
                            PushStack(c);//入栈

                        }else if (s.charAt(j) == 'B'){
                            InputShow(B);//显示操作
                            PushStack(B);//入栈
                        }
                        else {
                            System.out.println("错误!");break;
                        }
                        continue;
                    }else {
                        System.out.println("错误!");
                        System.exit(0);
                    }

                } else {
                    System.out.println("错误!");
                    System.exit(0);
                }


            }
        }

    public static String[] SplitString(String s) {//分别拆分成字母数字形式

            String []t=new String[2];
            t[0]=String.valueOf(s.charAt(0));
            t[1]=s.substring(1,s.length());

            return t;
        }




    public static void PushStack(int t){//入栈操作

        String []temp;
        temp = SplitString(LRt[Integer.parseInt(stack.peek())][t]);//分别拆分成字母数字形式

        for(int i=0;i<temp.length;i++){
            stack.push(temp[i]);//入栈
        }

        j++;
    }

    public static void InputShow(int t){//移进显示操作
        System.out.printf("%-4c",s.charAt(j));//输出w
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = j + 1; i < s.length(); i++) {//输出剩余串
            stringBuffer.append(s.charAt(i));
        }
        System.out.printf("%-8s",stringBuffer);
        System.out.println("PUSH(" + LRt[Integer.parseInt(stack.peek())][t] + "),NEXT(w)");//输出操作
    }

    public static void ReduceShow(int t){//归约操作显示
        System.out.printf("%-4c",s.charAt(j));//输出w
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = j + 1; i < s.length(); i++) {//输出剩余串
            stringBuffer.append(s.charAt(i));
        }
        System.out.printf("%-8s",stringBuffer);
        System.out.println("REDUCE(4)");
    }

    public static void PopStack(){//归约态出栈操作

        int count=0;

        for  (int i =0;i<LRt[Integer.parseInt(stack.peek())][0].length();i++){//计算栈顶元素需要归约的个数
            if(LRt[Integer.parseInt(stack.peek())][0].charAt(i)>='A'&&LRt[Integer.parseInt(stack.peek())][0].charAt(i) <= 'z'){
                count++;
            }
        }

        for (int i=0;i<2*count;i++){
            stack.pop();
        }

    }
}
posted @ 2021-06-20 17:29  乐茶茶  阅读(475)  评论(0编辑  收藏  举报