1217 실험 4귀속 하강 문법 분석 프로그램 설계

39925 단어
  1 #include 
  2 #include
  3 #include
  4 #include<string.h>
  5 char a[50] ,b[50],d[200],e[10];
  6 char ch;
  7 int n1,i1=0,flag=1,n=5;
  8 int total=0;
  9 int E();
 10 int E1();
 11 int T();
 12 int G();
 13 int S();
 14 int F();
 15 void input();
 16 void input1();
 17 void output();
 18 void main() 
 19 {
 20 int f,p,j=0;
 21 char x;
 22 d[0]='E';
 23 d[1]='=';
 24 d[2]='>';
 25 d[3]='T';
 26 d[4]='G';
 27 d[5]='#';
 28 printf(" ( <50, # ):
"); 29 do{ 30 scanf("%c",&ch); 31 a[j]=ch; 32 j++; 33 }while(ch!='#'); 34 n1=j; 35 ch=b[0]=a[0]; 36 printf(" \t \t \t\t \t
"); 37 f=E1(); 38 if (f==0) return; 39 if (ch=='#') 40 { 41 printf("accept
"); 42 p=0; 43 x=d[p]; 44 while(x!='#') { 45 printf("%c",x);p=p+1;x=d[p]; 46 } 47 }else { 48 printf("error
"); 49 printf("
"); 50 getchar(); 51 getchar(); 52 return; 53 } 54 printf("
"); 55 printf("
"); 56 getchar(); 57 getchar(); 58 59 } 60 int E1() 61 { 62 int f,t; 63 printf("%d\tE-->TG\t",total);total++; 64 flag=1; 65 input(); 66 input1(); 67 f=T(); 68 if (f==0) return(0); 69 t=G(); 70 if (t==0) return(0); 71 else return(1); 72 } 73 74 int E() 75 { 76 int f,t; 77 printf("%d\tE-->TG\t",total);total++; 78 e[0]='E';e[1]='=';e[2]='>';e[3]='T';e[4]='G';e[5]='#'; 79 output(); 80 flag=1; 81 input(); 82 input1(); 83 f=T(); 84 if (f==0) return(0); 85 t=G(); 86 if (t==0) return(0); 87 else return(1); 88 } 89 int T() 90 { 91 int f,t; 92 printf("%d\tT-->FS\t",total);total++; 93 e[0]='T';e[1]='=';e[2]='>';e[3]='F';e[4]='S';e[5]='#'; 94 output(); 95 flag=1; 96 input(); 97 input1(); 98 f=F(); 99 if (f==0) return(0); 100 t=S(); 101 if (t==0) return(0); 102 else return(1); 103 } 104 int G() 105 { 106 int f; 107 if(ch=='+') { 108 b[i1]=ch; 109 printf("%d\tG-->+TG\t",total);total++; 110 e[0]='G';e[1]='=';e[2]='>';e[3]='+';e[4]='T';e[5]='G';e[6]='#'; 111 output(); 112 flag=0; 113 input();input1(); 114 ch=a[++i1]; 115 f=T(); 116 if (f==0) return(0); 117 G(); 118 return(1); 119 } 120 printf("%d\tG-->^\t",total);total++; 121 e[0]='G';e[1]='=';e[2]='>';e[3]='^';e[4]='#'; 122 output(); 123 flag=1; 124 input();input1(); 125 return(1); 126 } 127 int S() 128 { 129 int f,t; 130 if(ch=='*') { 131 b[i1]=ch;printf("%d\tS-->*FS\t",total);total++; 132 e[0]='S';e[1]='=';e[2]='>';e[3]='*';e[4]='F';e[5]='S';e[6]='#'; 133 output(); 134 flag=0; 135 input();input1(); 136 ch=a[++i1]; 137 f=F(); 138 if (f==0) return(0); 139 t=S(); 140 if (t==0) return(0); 141 else return(1);} 142 printf("%d\tS-->^\t",total);total++; 143 e[0]='S';e[1]='=';e[2]='>';e[3]='^';e[4]='#'; 144 output(); 145 flag=1; 146 a[i1]=ch; 147 input();input1(); 148 return(1); 149 } 150 151 int F() 152 { 153 int f; 154 if(ch=='(') { 155 b[i1]=ch;printf("%d\tF-->(E)\t",total);total++; 156 e[0]='F';e[1]='=';e[2]='>';e[3]='(';e[4]='E';e[5]=')';e[6]='#'; 157 output(); 158 flag=0; 159 input();input1(); 160 ch=a[++i1]; 161 f=E(); 162 if (f==0) return(0); 163 if(ch==')') { 164 b[i1]=ch;printf("%d\tF-->(E)\t",total);total++; 165 flag=0;input();input1(); 166 ch=a[++i1]; 167 } 168 else { 169 printf("error
"); 170 return(0); 171 } 172 } 173 else if(ch=='i') { 174 b[i1]=ch;printf("%d\tF-->i\t",total);total++; 175 e[0]='F';e[1]='=';e[2]='>';e[3]='i';e[4]='#'; 176 output(); 177 flag=0;input();input1(); 178 ch=a[++i1]; 179 } 180 else {printf("error
");return(0);} 181 return(1); 182 } 183 void input() 184 { 185 int j=0; 186 for (;j<=i1-flag;j++) 187 printf("%c",b[j]); 188 printf("\t\t"); 189 printf("%c\t\t",ch); 190 } 191 192 void input1() 193 { 194 int j; 195 for (j=i1+1-flag;j) 196 printf("%c",a[j]); 197 printf("
"); 198 } 199 void output(){ 200 int m,k,j,q; 201 int i=0; 202 m=0;k=0;q=0; 203 i=n; 204 d[n]='=';d[n+1]='>';d[n+2]='#';n=n+2;i=n; 205 i=i-2; 206 while(d[i]!='>'&&i!=0) i=i-1; 207 i=i+1; 208 while(d[i]!=e[0]) i=i+1; 209 q=i; 210 m=q;k=q; 211 while(d[m]!='>') m=m-1; 212 m=m+1; 213 while(m!=q) { 214 d[n]=d[m];m=m+1;n=n+1; 215 } 216 d[n]='#'; 217 for(j=3;e[j]!='#';j++){ 218 d[n]=e[j]; 219 n=n+1; 220 } 221 k=k+1; 222 while(d[k]!='=') { 223 d[n]=d[k];n=n+1;k=k+1; 224 } 225 d[n]='#'; 226 system("pause"); 227 }

 
다음으로 전송:https://www.cnblogs.com/LwF39/p/5092436.html

좋은 웹페이지 즐겨찾기