递归下降分析程序(语法分析)

 

1. 文法 GS:

1S -> AB

2A ->Da|ε

3B -> cC

4C -> aADC |ε

5D -> b|ε

验证文法 GS)是不是 LL1)文法?

 

FIRST

FIRST(Da)={b,a}

FIRST(ε)={ε}

FIRST(cC)={c}

FIRST(aADC)={a}

FIRST(b)={b}

 

FOLLOW

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

其中#FOLLOW(A)=FOLLOW(C)=FOLLOW(B)=FOLLOW(S)

FOLLOW(B)={#}

FOLLOW(C)={#}

FOLLOW(D={a,#}

 

SELECT

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

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

SELECT(C->aADC)=FIRST(aADC)={a}

SELECT(C->ε)=FOLLOW(C)={#}

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

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

 

因为SELECT(A->Da)SELECTA->ε)≠∅

SElECT(C->aADC)∩SELECT(C->ε)=

SELECT(D->b)SELECT(D->ε)=

所以G(S)不是LL(1)文法。

 

 

 

 

 

 

 

 

 

 

2.(上次作业)消除左递归之后的表达式文法是否是LL(1)文法?

消除左递归得:

E->TE’

E’->+TE’|ε

T->FT’

T’->*FT’|ε

F->(E)|i

 

FIRST集:

FIRST(TE’)={ (, i }

FIRST(+TE')={+ε}

FIRST(FT’)={(,i}

FIRST(*FT')={ *,ε}

FIRST((E))={ (}

FIRST(i)={ i }

 

FOLLOW集:

FOLLOW(E)={ ),# }

FOLLOW(E')={ ),# }

FOLLOW(T)={+,),#}

FOLLOW(T')={+,),#}

FOLLOW(F)={*,+,),#}

 

SELECT集:

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

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

SELECT(E’ -> ε) = FIRST(ε) - {ε} U FOLLOW(E’) = FOLLOW(E’) = { ) , # }

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

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

SELECT(T’ -> ε) = FIRST(ε) - {ε} U FOLLOW(T’) = FOLLOW(T’) = { + , ) ,# }

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

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

 

SELECT(E’ -> +TE’) = FIRST(+TE’) = { + } ∩ SELECT(E’ -> ε) = FOLLOW(E’) = { ) , # }=

SELECT(T’ -> *FT’) = FIRST(*FT’) = { * } ∩ SELECT(T’ -> ε) =FOLLOW(T’) = { + , ) ,# }=

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

所以该文法为LL(1)文法。

 

 

 

 

 

 

 

3.2,如果是LL(1)文法,写出它的递归下降语法分析程序代码。

E()

    T();

       E'();

     }

E'()

T()

T'()

F()

 

Void ParseE()

{       

         Switch(lookahead){

         case ’(‘,’I’:

                  ParseT();

                  ParseE’()

break;

default:

printf("syntx error\n");

    exit(0);

}

}

 

Void ParseE’()

{

         Switch(lookahead){

                  case ‘+’:

                            MatchToken(+);

                            ParseT();

                            ParseE’();

                            break;

       case ‘)’,’#’

                            break;

default:

printf("syntx error\n");

                 exit(0);

 

}

}

 

Void ParseT()

{       

         Switch(lookahead){

         case ‘(’,’i’

                  ParseF();

                  ParseT’();

                  break;

default:

printf("syntx error\n");

        exit(0);

 

}

 

}

Void ParseT’()

{       

         Switch(lookahead){

          case  ‘*’:

                   MatchToken(*);

                   ParseF();

                   ParseT’();

                   break;

         case  ‘+’ ,’ )’ ,’#’:

                   break;

default:

printf("syntx error\n");

        exit(0);

 

}

}

void ParseF(){

  switch(lookahead){

        case '(':

                MatchToken( '(' );

                ParseE();

                MatchToken( ')' );

                break;

        case 'i':

               MatchToken( 'i' );

               break;

        default:

              printf("syntx error\n");

                exit(0);

    }

}

 

 

posted @ 2019-11-20 14:57  爱飞的大白鲨  阅读(2563)  评论(0编辑  收藏  举报