Compilation principle recursive descent analysis First, Follow set and prediction analysis table

Let's start with an example:

With grammar:
E -> TE'
E'->+E|ε
T->FT'
T'->T|ε
F->PF'
F'->F'|ε
P->(E)|a|b|^

On the solution of First,Follow set and prediction analysis table

First of all, I read books and online materials. At first glance, I couldn't understand them. Next, let me share them in vernacular

First and Follow sets

Solution of First set:

Let's take a look at the example, we should be able to understand directly:
First of all, if you don't understand the expression, it's first?, It doesn't matter. I'm taking an example. This time, let's look at the expression p - > (E) | a | B ^, and its First(.P) is {(, a,b, ^} (here. P stands for P). Let's take another last example and look at the expression t '- > t| ε, Its First(T ') is {FIrst(T), ε}.

I believe many people have found the rule. The First method is to add the First digit of the following formula (if there are many cases, you need to take the First digit of the formula, such as the First (. P) above). If it is a non terminator, add the First set of the non terminator. If it is a terminator, just add the terminator directly

Solution of Follow set

First, set the expression a - > BCD
1. If First(d) contains ε, Then add the element of Follow(a) to Follow(.c) D may not exist That is, a - > BC. At this time, the element of Follow(a) should also be added to Follow(.c)
2. Divide First(d) ε Add elements outside to Follow(.c)
3. Add # to Follow(.c)

Forecast analysis table

I suggest you take a look at the end of this blog. I can understand it after reading this. Remember: if you don't understand the definition, you can understand it in combination with the table
https://blog.csdn.net/qq_41731283/article/details/106448620

Recursive descent analysis program

Generally speaking, this program is not difficult. First of all, we need to know our purpose. Our purpose is to enter a string, and then judge whether we can deduce this expression from E. look at the code directly. I believe you can understand it at a glance

package LeeCode;

import java.util.Scanner;

public class DiGuiRecursiveAnalysis {

    static String word;//Input string

    static int index = 0;//Used to record the position of the character currently ready to match

    public void error() {//If the match is wrong, the output is illegal, and then terminate the program to stop matching
        System.out.println("illegal!");
        System.exit(0);
    }

    public void match(char judge) {//If the match i succeeds, i will point to the next character to match
       if (word.charAt(index) == judge) {
            index++;
        }
    }


    //E->TE'
    public void E() {
        System.out.println("E->TE'");

        T();

        E1();



    }

    //E'->+E|null
    public void E1() {
        if (word.charAt(index) == '+') {
            match('+');

            System.out.println("E'->+E");


            E();

        } else {
            System.out.println("E'->ε");
        }
    }

    // T->FT'
    private void T() {
        System.out.println("T->FT'");

        F();
        T1();
    }



    //  T'->T|null
    private void T1() {
    /*
    If here, I want to say why the conditions are these, because the elements in our First(T ') are these plus ε. However, we need to execute T1 - > ε The final result of this step is ε Element. In any case, the elements in the if can be obtained by executing T1 - > T. therefore, if you want to execute T1 - > t, you need to match these elements
	*/
        if (word.charAt(index) == '('||word.charAt(index) == 'a'||word.charAt(index) == 'b'||word.charAt(index) == '&') {
            System.out.println("T'->T");
            T();
        } else{
            System.out.println("T'->ε");


        }




    }

    // F->PF'
    private void F() {
        System.out.println("F->PF'");

        P();
        F1();

    }

    //F'->*F'|null
    private void F1() {
        if (word.charAt(index) == '*') {
            match('*');
            System.out.println("F'->*F'");

            F1();
        } else {
            System.out.println("F'->ε");


        }
    }

    //P->(E)|a|b|&
    private void P() {

        if (word.charAt(index) == '(') {
            match('(');

            E();

            if (word.charAt(index) == ')') {
                match(')');

                System.out.println("P->(E)");

            } else {
                error();
            }
        } else if (word.charAt(index) == 'a') {

            match('a');

            System.out.println("P->a");
        }else if (word.charAt(index) == 'b') {
            match('b');

            System.out.println("P->b");
        }else if (word.charAt(index) == '&') {
            match('&');

            System.out.println("P->&");
        } else {
            error();
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.print("Please enter a string(with#(end) ");
        word = input.nextLine();

        if (word.charAt(word.length()-1)!='#'){
            System.out.println("The input does not meet the requirements,To#ending! ");
            System.exit(0);
        }

      

        DiGuiRecursiveAnalysis parse = new DiGuiRecursiveAnalysis();
        parse.E();
        System.out.println("Analysis successful");//Because if my program judges an error, it will terminate, and if it does not terminate, it means success Output directly

    }

}

Operation screenshot:
1.

2.

If you have any mistakes, please point them out. Learn together and make progress together!

Keywords: Java

Added by flemingmike on Sat, 19 Feb 2022 08:16:19 +0200