递归下降识别器——编译原理

作业用的简化版递归下降识别器 :



例子说明:

文法G[S]:

S-> A|B

A->aM

M->A|#

B->bN

N->B|#

 

①文法不含左递归

 

 

是否=>#

First

follow

S

{a,b}

#

A

{a}

#

B

{b}

#

M

{a,#}

#

N

{b,#}

#

 

 

①文法不含左递归

M N的产生式至多有一个#,因此:

First(A) follow(M) =

First(B) follow{N} =

S A B的产生式没有#, 因此

First(A) first(B) =

First{aM}={a}

First{bN} ={b}

 

因此此文法符合LL(1)判断

 

递归下降分析:

Select(S->A) = {a}

Select(S->B)={b}

Select(A->aM)={a}

Select(M->A)={a)

Selec(M->#)=(#)

Select(B->bN)={b}

Select(N->B)={b}

Select(N->#)={#}

 

Void praseS(){
    Switc(lookahead){
        Case a:
            ParseA();
            Break;
        Case b:
            ParseB();
            Break;
        Defalut:
            Print(“error”);
            Exit();
    }
}
Void praseA(){
    Switc(lookahead){
        Case a:
            MatchToken(a)
            ParseM();
            Break;
        Defalut:
            Print(“error”);
            Exit();
    }
}

Void praseB(){
    Switc(lookahead){
        Case b:
            MatchToken(b)
            ParseN();
            Break;
         Defalut:
            Print(“error”);
            Exit();
    }
}

Void praseM(){
    Switc(lookahead){
        Case a:
            ParseA();
            Break;
        Case #:
            MatchToken(#)
            Break;
        Defalut:
            Print(“error”);
            Exit();
    }
}


Void praseN(){
    Switc(lookahead){
        Case b:
            ParseB();
            Break;
        Case #:
            MatchToken(#)
            Break;
        Defalut:
            Print(“error”);
            Exit();
    }    
}

你可能感兴趣的:(编译原理)