递归下降语法分析
1 # include <stdio.h> 2 # include <string.h> 3 # include <stdlib.h> 4 int E(); 5 int G(); 6 int T(); 7 int Ss(); 8 int F(); 9 char a[52]; 10 int total=0; 11 int i=0; 12 int temp=1; 13 int main() 14 { 15 gets(a); 16 int f; 17 18 f=E(); 19 if(f==0) 20 return 0; 21 if(a[i] == '#') 22 printf("accept\n"); 23 else 24 { 25 printf("error\n"); 26 return 0; 27 } 28 } 29 int E() 30 { 31 int f,t; 32 printf("%d E-->TG\n",total++); 33 f=T(); 34 if(!f) 35 return 0; 36 t=G(); 37 if(!t) 38 return 0; 39 else 40 return 1; 41 42 } 43 int G() 44 { 45 int f,t; 46 if(a[i] == '+') 47 { 48 i++; 49 printf("%d G-->+TG\n",total++); 50 f=T(); 51 if(!f) 52 return 0; 53 t=G(); 54 if(!t) 55 return 0; 56 else 57 return 1; 58 } 59 else 60 { 61 printf("%d G-->&\n",total++); 62 return 1; 63 } 64 65 } 66 int T() 67 { 68 int f,t; 69 printf("%d T-->FS\n",total++); 70 f=F(); 71 if(!f) 72 return 0; 73 t=Ss(); 74 if(!t) 75 return 0; 76 else 77 return 1; 78 } 79 int Ss() 80 { 81 int f,t; 82 if(a[i] == '*') 83 { 84 i++; 85 printf("%d S-->*FS\n",total++); 86 f=F(); 87 if(!f) 88 return 0; 89 t=Ss(); 90 if(!t) 91 return 0; 92 else 93 return 1; 94 } 95 else 96 { 97 printf("%d S-->&\n",total++); 98 return 1; 99 } 100 101 } 102 int F() 103 { 104 int f,t; 105 if(a[i] == '(') 106 { 107 i++; 108 printf("%d F-->(E)\n",total++); 109 f=E(); 110 if(!f) 111 return 0; 112 if(a[i] == ')') 113 i++; 114 else 115 { 116 printf("error\n"); 117 return 0; 118 } 119 } 120 else if(a[i] == 'i') 121 { 122 i++; 123 printf("%d F-->i\n",total++); 124 } 125 return 1; 126 }