1022 词法分析程序

  1 #include <stdio.h>  
  2 #include <string.h>  
  3   
  4 char prog[80],token[8],ch;  
  5 int syn,p,m,n,sum;  
  6 char *rwtab[6]={"begin","if","then","while","do","end"};  
  7    
  8 void scaner(void);  
  9   
 10 main()  
 11 {  
 12     p=0;  
 13     printf("\n Please input the source code:\n");  
 14       
 15     do{                                   
 16           scanf("%c",&ch);  
 17           prog[p++]=ch;  
 18     }while(ch!='#');  
 19       
 20     p=0;  
 21     do{  
 22             scaner();  
 23             switch(syn)  
 24             {  
 25                 case 11:  
 26                     printf("( %d,%d )\n",syn,sum); 
 27                 break;  
 28                   
 29                 case -1:  
 30                     printf("you have input a wrong string\n");  
 31                     return 0;  
 32                 break;  
 33                   
 34                 default:   
 35                 printf("( %d,%s )\n",syn,token);  
 36                 break;  
 37             }  
 38         }while(syn!=0);  
 39          printf(" Over.\n");
 40  }  
 41   
 42 void scaner(void)  
 43 {    
 44     sum=0;  
 45       
 46     for(m=0;m<8;m++)  
 47         token[m++]= NULL;  
 48       
 49         ch=prog[p++];  
 50         m=0;  
 51           
 52     while((ch==' ')||(ch=='\n'))  
 53         ch=prog[p++];  
 54       
 55     if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))  
 56      {   
 57         while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))  
 58         {  
 59             token[m++]=ch;  
 60             ch=prog[p++];  
 61         }  
 62           
 63         p--;  
 64         syn=10;  
 65   
 66         for(n=0;n<6;n++)  
 67         if(strcmp(token,rwtab[n])==0)  
 68         {   
 69             syn=n+1;  
 70             break;  
 71         }  
 72      }  
 73      else if((ch>='0')&&(ch<='9'))  
 74      {   
 75         while((ch>='0')&&(ch<='9'))  
 76         {  
 77             sum=sum*10+ch-'0';  
 78             ch=prog[p++];  
 79         }  
 80         p--;  
 81         syn=11;  
 82     }  
 83     else   
 84     {  
 85         switch(ch)  
 86         {  
 87         case '<':  
 88             token[m++]=ch;  
 89             ch=prog[p++];  
 90             if(ch=='=')  
 91             {   
 92                 syn=22;  
 93                 token[m++]=ch;  
 94             }  
 95             else  
 96             {    
 97                 syn=20;  
 98                 p--;  
 99             }  
100         break;  
101   
102         case '>':  
103             token[m++]=ch;  
104             ch=prog[p++];  
105             if(ch=='=')  
106             {  
107                 syn=24;  
108                 token[m++]=ch;  
109             }  
110             else  
111             {   
112                 syn=23;  
113                 p--;  
114             }  
115         break;  
116   
117         case '+':  
118             token[m++]=ch;  
119             ch=prog[p++];  
120             if(ch=='+')  
121             {  
122                 syn=17;  
123                 token[m++]=ch;  
124             }  
125             else  
126             {  
127                 syn=13;  
128                 p--;  
129             }  
130         break;  
131   
132         case '-':  
133             token[m++]=ch;  
134             ch=prog[p++];  
135             if(ch=='-')  
136             {  
137                 syn=29;  
138                 token[m++]=ch;  
139             }  
140             else  
141             {   
142                 syn=14;  
143                 p--;  
144             }  
145         break;  
146   
147         case '=':  
148             token[m++]=ch;  
149             ch=prog[p++];  
150             if(ch=='=')  
151             {  
152                 syn=25;  
153                 token[m++]=ch;  
154             }  
155             else  
156             {  
157                 syn=18;  
158                 p--;  
159             }  
160         break;  
161   
162         case '*': syn=15;token[m++]=ch; break;
163         case '/': syn=16;token[m++]=ch; break;
164         case '(': syn=27;token[m++]=ch; break;
165         case ')': syn=28;token[m++]=ch; break;
166         case ';': syn=26;token[m++]=ch; break; 
167         case '#': syn=0;token[m++]=ch; break;
168         case ':': syn=17;token[m++]=ch; break;
169         default: syn=-1;  break; 
170         }  
171     }  
172         token[m++]='\0';  
173 }

你可能感兴趣的:(1022 词法分析程序)