首页 > 代码库 > 一个词法分析程序

一个词法分析程序

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

 

一个词法分析程序