Analyzing eleventh job LL (1) grammar, recursive descent parser

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) grammar?

solution:

first(AB)={b,a,c}

First(Da)={b,a}

First (e) = {e}

First(cC)={c}

First(aADC)={a}

First(b)={b}

 

Follow(S)={c,b,a}

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

Follow(B)={a,b,c}

Follow(C)={#}

Follow(D)={#,a}

 

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

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

Since Sellect (A-> Da) ∩Sellect (A-> ε) ≠ Ø, can be seen, G (s) is not LL (1) syntax.

 

2. (last job) after the elimination of left recursion whether the expression grammar is LL (1) grammar?

 

( . 1 ) SELECT set:

  SELECT(E->TE')=FIRST(TE')={ (, i }

  SELECT(E'->+TE')=FIRST(+TE')={+}

  SELECT (E '-> e) = FIRST (e) - {e} UFOLLOW (E') = FOLLOW (E ') = {), #}

  SELECT(T->FT')=FIRST(FT')={ (,i }

  SELECT(T'->*FT')=FIRST(*FT')={*}

  SELECT(T'->ε)=FIRST(ε)-{ε}UFOLLOW(T')=FOLLOW(T')={ +,),# }

  SELECT(F->(E))=FIRST((E))={ ( }

  SELECT(F->i)=FIRST(i)={i}

 solution:

SELECT (E '-> + TE') ∩SELECT (E '-> ε) = O

SELECT(T'->*FT')∩SELECT(T'->ε)=Ø

SELECT(F->(E))∩SELECT(F->i)=Ø

Thus can be obtained, This grammar is LL (1) syntax.

 

(2)

select set:

  SELECT(A->aA')=FIRST(aA')={a}

  SELECT(A'->ABe)=FIRST(ABe)={a}

  SELECT(A'->ε)=FIRST(ε)-{ε}UFOLLOW(A')=FOLLOW(A')={d,#}

  SELECT(B->dB')=FIRST(dB')={d}

  SELECT(B'->bB')=FIRST(bB')={b}

  SELECT(B'->ε)=FIRST(ε)-{ε}UFOLLOW(B')=FOLLOW(B')={e}

solution: 

SELECT(A'->ABe)∩SELECT(A'->ε)=Ø

SELECT(B'->bB')∩SELECT(B'->ε)=Ø

Thus can be obtained, This grammar is LL (1) syntax.

 

 

(3)

select set:

  SELECT(S->bS')=FIRST(bS')={b}

  SELECT(S'->BaS')=FIRST(BaS')={a}

  SELECT(S'->ε)=FIRST(ε)-{ε}UFOLLOW(S')=FOLLOW(S')={#}

SELECT(B->ab)=FIRST(ab)={a}

 solution:

SELECT(S'->BaS')∩SELECT(S'->ε)=Ø

Thus can be obtained, This grammar is LL (1) syntax.

 

(4)

select set: 

  SELECT(S->Ap)=FIRST(Ap)={a,c,p}

  SELECT(A->a)=FIRST(a)={a}

  SELECT(A->ε)=FIRST(ε)-{ε}UFOLLOW(A)=FOLLOW(A)={p}

  SELECT(A->cA)=FIRST(cA)={c}

       SELECT(A->aA)=FIRST(aA)={a}

 solution:

SELECT(A->a)∩SELECT(A->aA)≠Ø

Thus can be obtained, This grammar is not LL (1) syntax.

 

(5)

select set:

  SELECT(S->Ap)=FIRST(Ap)={a,c}

  SELECT(S->Bp)=FIRST(Bq)={b,d}

  SELECT(A->a)=FIRST(a)={a}

  SELECT(A->cA)=FIRST(cA)={c}

  SELECT(B->b)=FIRST(b)={b}

  SELECT(B->dB)=FIRST(dB)={d}

 solution:

SELECT(S->Ap)∩SELECT(S->Bp)=Ø

SELECT(A->a)∩SELECT(A->cA)=Ø

SELECT(B->b)∩SELECT(B->dB)=Ø

Thus can be obtained, This grammar is LL (1) syntax.

 

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

(1) (2) (3) (5) recursive descent parser code is as follows :

(1)

Void  parseE(){

 If(lookahead==’(‘,i)

{Prset ();

parseE’();

}

}

Void parseE’(){

If(lookahead==’+’){

 Frset ();

parseE’();

}

else if(lookahead==’)’,’#’)

{  }

}

Void parseT(){

  If(lookahead==’(‘,i){

  Match Token (*);

parseF();

Prset '();

}

else if(lookahead==’+’,’)’,’#’){

}

}

Void  parseF(){

if(lookahead==’(’){

match tokens (();

parseE();

match token ());

}

else if(lookahead==i){

  Match Token (in);

}

}

 

(2)

Void parseA(){

 If(lookahead==a)

{Matchtoken (a);

parseA’();

}

}

Void parseA’(){

  If(lookahead==a){

  parseA();

parseB ();

matchtoken (e);

}

else if(lookahead==d,#){

}

}

Void parseB () {

 If(lookahead==d)

{Matchtoken (d);

parseB '();

}

}

Void parseB '() {

 If(lookahead==b)

{Matchtoken (b);

parseB '();

}

else if(lookahead==e){

}

}

 

(3)

Void parseS(){

  If(lookahead==b){

Matchtoken (b);

parseS’();

}

}

Void parseS’(){

 If(lookahead==a){

 parseB ();

matchtoken (a);

parseS’();

}

Else if(lookahead==#){

}

}

Void parseB () {

If(lookahead==a){

 Matchtoken (a);

Matchtoken (b);

}

}

 

(5)

Void parseS(){

 If(lookahead==a,c){

  ParseA();

Matchtoken (p);

}

Else if(lookahead==b,d){

 parseB ();

matchtoken (p);

}

}

Void parseA(){

  If(lookahead==a){

 Matchtoken (a);

}

Else if(lookahead==c){

 Matchtoken (c);

parseA();

}

}

Void parseB () {

 If(lookahead==b){

 Matchtoken (b);

}

Else if(lookahead==d){

 Matchtoken (d);

parseB ();

}

}

Guess you like

Origin www.cnblogs.com/fqy1028/p/11900688.html