Eleventh job -LL (1) grammar is determined

1. grammar G (S):

(1)S -> AB

(2)A ->Da|ε

(3)B -> cC

(4)C -> aADC |ε

(5)D -> b|ε

Verify grammar G (S) is not LL (1) syntax.

A->Da

A->ε

C->aADC

C->ε

D->b

D->ε

FIRST集:

First(Da) = {b,a}

First (e) = {e}

First(aADC) = {a}

First(b) = {b}

FOLLOW集:

Follow(A) = {c,b,a,#}

Follow(C) = {#}

Follow(D) = {a,#}

SELECT set:

Select(A->Da) = {b,a}

Select(A->ε) = {c,b,a,#}

Select(C->aADC) ={a}

Select(C->ε) = {#}

Select(D->b) = {b}

Select(D->ε) = {a,#}

Thus be obtained: Select (A-> Da) ∩ Select (A-> ε) ≠ ∅

                  Select(C->aADC) ∩ Select(C->ε) =∅

                  Select(D->b) ∩ Select(D->ε) =∅

Thus the LL (1) grammar that defines the grammar is not LL (1) syntax.

 

2. Whether the expression grammar after the elimination of left recursion method is LL (1) grammar?

Eliminate left recursion:

(1) E-> TE '

     E '-> + TE' | e

(2) T->FT'

      T'->*FT'|ε

 (3) F->(E)|i

SELECT set:

Select(E->TE') = First(TE') = {(,i}

Select(E'->+TE') = First(+TE') = {+}

Select (E '-> e) = (First (e) = {e}) ∪Follow (E') = {), #}

Select(T->FT') = First(FT') = {(,i}

Select(T'->*FT') = First(+TE') = {*}

Select(T'->ε) = (First(ε) = {ε})∪Follow(T') = {+,),#}

Select( F->(E)) = First((E)) = {( }

Select( F->i) = First(i) = {i}

Thus can be obtained, Select (E '-> + TE') ∩ Select (E '-> ε) = ∅

                  Select(T'->*FT') ∩ Select(T'->ε) = ∅

                  Select( F->(E)) ∩ Select( F->i) = ∅

So that (1) the grammar defined by the grammar is LL LL (1) syntax.

 

3. The connection 2, if it is LL (1) grammar, write its recursive descent parser code.

E()

    {T();

       E'();

     }

E'()

T()

T'()

F()

 

 

 

 Experiment 4. Add a lexical analyzer, parser can form a run, analyzing any input symbol string is not a valid expression.

 

 

 

 

Guess you like

Origin www.cnblogs.com/hqling/p/11888353.html
1LL