编程语言
首页 > 编程语言> > 递归下降分析程序(语法分析)

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

作者:互联网

 

1. 文法 G(S):

(1)S -> AB

(2)A ->Da|ε

(3)B -> cC

(4)C -> aADC |ε

(5)D -> b|ε

验证文法 G(S)是不是 LL(1)文法?

 

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)∩SELECT(A->ε)≠∅

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);

    }

}

 

 

标签:语法分析,递归,分析程序,break,FT,FOLLOW,TE,SELECT,FIRST
来源: https://www.cnblogs.com/zhff/p/11897801.html