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

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

 SELECT(A→ε)=FIRST(ε)-ε+FOLLOW(A)=FOLLOW(A)=FIRST(B)∪FIRST(D)∪FIRST(C)={c,b,a,#}

 SELECT(A→Da)∩SELECT(A→ε)≠∅

 SELECT(C→aADC)=FIRST(aADC)={a}

 SELECT(C→ε) =FIRST(ε)-{ε}+FOLLOW(C)=FOLLOW(B)=FOLLOW(S)={#}

 SELECT(C→aADC)∩SELECT(C→ε)=∅

 SELECT(D→b)=FIRST(b)={b}

 SELECT(D→ε)=FIRST(ε)-ε+FOLLOW(D)={a,FOLLOW(C)}={a,FOLLOW(B)}={a,FOLLOW(S)}={a,#}

 SELECT(D→b)∩SELECT(D→ε)=∅

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

Not necessarily, there are other situations like back, it determines the grammar is not necessary to calculate for each production LL1 select the set, determined according to the calculation result

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

void ParseE()

    {Frset ();

       ParseE'();

     }

void ParseE'()

  {switch(lookahead):

    case +:

      Match Token (+);

      Frset ();

      ParseE'();

      break;

    case #,):

      MatchToken (e)?

      break;

    default:

      printf('synax error!\n');

      exit(0);

  }

void ParseT()

{  

  ParseF();

  Frset '();

}

void ParseT'()

{

  switch(lookahead):

    case *:

      Match Token (*);

      ParseF();

      Frset '();

      break;

    case #,),+:

      MatchToken (e)?

      break;

    default:

      printf('synax error!\n');

      exit(0);

}

void ParseF()

{ 

  switch(lookahead):

    case (:

      Match Token (();

      ParseE();
      MatchToken());

      break;

    case i:

      Match Token (in);

      break;

    default:

      printf('synax error!\n');

      exit(0);

}

 

 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/lxml/p/11887053.html
1LL