Compilation principle: LL(1) grammar parser (predictive analysis table method)

Design requirements: For an LL(1) grammar with any input, construct its prediction analysis table, and analyze whether the specified input string is a sentence of the grammar.

Idea: First implement the set FIRST(X) construction algorithm and the set FOLLOW(A) construction algorithm, then construct a prediction analysis table based on the FIRST and FOLLOW sets, and print out the analysis process of the analysis stack for the specified sentence to determine whether it is the grammar. sentence.

  • //文法
    E->TK
    K->+TK
    K->$
    T->FM
    M->*FM
    M->$
    F->i
    F->(E)

For the input string i+i*i#, here we first give a screenshot of the experimental results:
write picture description here
write picture description here

(1) Algorithmic idea of ​​​​seeking the FIRST set

  • If X->a.., add terminal a to FIRST(X);
    if X->e, add terminal e to FIRST(X) (e means empty set);
    if X->BCD... E, then add all elements of First (B) (except the empty set) to First (X), and then detect First (B), if there is no empty set in First (B), that is, e, then stop, if there is, go to B Check back, add all elements (except the empty set) in First (C) to First (X), and then check whether there is e in First (C)... Until the end, if all the non-terminal symbols before E are in the First set If it contains e, when E is detected, add First (E) to First (X), if First (E) contains e, add e to First (X).

(2) Algorithm idea for finding FOLLOW set

  • The way to construct FOLLOW(A) for each nonterminal symbol A in the grammar G is to use the following rules continuously until each FOLLOW does not increase.
    (1) For the start symbol S of the grammar, put # in FOLLOW(S );
    (2) If A->aBb is a production, add FIRST(b){ε} to FOLLOW(B);
    (3) If A->aB is a production, or A-> aBb is a production and b=>ε (ie, ε∈FIRST(b)) adds FOLLOW(A) to FOLLOW(B)

(3) Algorithmic idea of ​​generating predictive analysis table

  • The algorithm for constructing the analysis table M is:
    (1) Perform the second and third steps for each production A->a of the grammar G;
    (2) For each terminal a∈FIRST(a), put A- >a is added to M[A,a];
    (3) If ε∈FIRST(a), then any b∈FOLLOW(A) add A->a to M[A,b];
    (4) Mark all undefined M[A,a] with an error flag.

(4) The analysis process of the symbol string The
general control program of the predictive analysis program acts according to the stack top symbol X and the current input symbol at any time. For any (X, a), the general control program is executed
every time . One of the following three possible actions:
(1) If X=a=”#”, declare the analysis successful and stop the analysis process.
(2) If X=a≠”#”, remove X from the top of the STACK stack Eject, let a point to the next input symbol.
(3) If X is a non-terminal symbol, check the analysis table M, if M[A, a] stores a production about X, then first put X Eject the top of the STACK stack, and then
push the right-hand symbol string of the production into the STACK stack one by one in reverse order (if the right-hand symbol is ε, it means nothing is pushed onto the stack). After the production's right-hand
symbol When pushing the stack, the corresponding semantic action of this production should be performed. If the "error flag" is stored in M[A, a], the error diagnosis program ERROR is called.

This article is transferred from the compilation principle: LL(1) grammar parser (predictive analysis table method)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325934749&siteId=291194637