1. 文法 G(S):
(1)S -> AB
(2)A ->Da|ε
(3)B -> cC
(4)C -> aADC |ε
(5)D -> b|ε
验证文法 G(S)是不是 LL(1)文法?
2.法消除左递归之后的表达式文法是否是LL(1)文法?
3.接2,如果是LL(1)文法,写出它的递归下降语法分析程序代码。
E()
{T();
E'();
}
E'()
T()
T'()
F()
void ParseE(){ 2 switch(lookahead){ 3 case (,i: 4 ParseT(); 5 ParseE'(); 6 break; 7 default: 8 printf("syntax error \n"); 9 exit(0); 10 } 11 } 12 void ParseE'(){ 13 switch(lookahead){ 14 case +: 15 MatchToken(+); 16 ParseT(); 17 ParseE'(); 18 break; 19 case #,): 20 break; 21 default: 22 printf("syntax error \n"); 23 exit(0); 24 } 25 } 26 void ParseT(){ 27 switch(lookahead){ 28 case (,i: 29 ParseF(); 30 ParseT'(); 31 break; 32 default: 33 printf("syntax error \n"); 34 exit(0); 35 } 36 } 37 void ParseT'(){ 38 switch(lookahead){ 39 case *: 40 MatchToken(*); 41 ParseF(); 42 ParseT'(); 43 break; 44 case #,),+: 45 break; 46 default: 47 printf("syntax error \n"); 48 exit(0); 49 } 50 } 51 void ParseF(){ 52 switch(lookahead){ 53 case (: 54 MatchToken((); 55 ParseE() 56 MatchToken()); 57 break; 58 case i: 59 MatchToken(i); 60 break; 61 default: 62 printf("syntax error \n"); 63 exit(0); 64
void ParseE(){
2 switch(lookahead){
3 case (,i:
4 ParseT();
5 ParseE'();
6 break;
7 default:
8 printf("syntax error \n");
9 exit(0);
10 }
11 }
12 void ParseE'(){
13 switch(lookahead){
14 case +:
15 MatchToken(+);
16 ParseT();
17 ParseE'();
18 break;
19 case #,):
20 break;
21 default:
22 printf("syntax error \n");
23 exit(0);
24 }
25 }
26 void ParseT(){
27 switch(lookahead){
28 case (,i:
29 ParseF();
30 ParseT'();
31 break;
32 default:
33 printf("syntax error \n");
34 exit(0);
35 }
36 }
37 void ParseT'(){
38 switch(lookahead){
39 case *:
40 MatchToken(*);
41 ParseF();
42 ParseT'();
43 break;
44 case #,),+:
45 break;
46 default:
47 printf("syntax error \n");
48 exit(0);
49 }
50 }
51 void ParseF(){
52 switch(lookahead){
53 case (:
54 MatchToken(();
55 ParseE()
56 MatchToken());
57 break;
58 case i:
59 MatchToken(i);
60 break;
61 default:
62 printf("syntax error \n");
63 exit(0);
64