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

词法分析程序

词法分析程序

实验要求:

输入:源程序字符串

输出:二元组(字符,单词符号本身)

各种单词符号对应的种别码:

技术分享

程序源代码:

  1 #include<stdio.h>  2 #include<string.h>  3 #define N 100  4 void pristring(char str[],int i,int semp[]);  5 void prichar(char str[],int i,int semp[]);  6 void main()  7 {  8     int i,j,k;  9     int temp; 10     char str[N]; 11     char strtemp[N]={\0}; 12     int semp[N]={0}; 13     printf("输入您需要进行词法分析的源程序:"); 14     gets(str); 15     temp=strlen(str); 16     for(i=0;i<temp;i++) 17     { 18         if((semp[i]==0)&&(str[i-1]<48||str[i-1]>57)&&(str[i]>=48&&str[i]<=57)) 19         { 20             for(j=0;j<N;j++) 21             { 22                 if(str[i]<48||str[i]>57) break; 23                 strtemp[j]=str[i]; 24                 semp[i]=1; 25                 i++; 26             } 27             printf("<11,"); 28             k=0; 29             do{ 30                 printf("%c",strtemp[k]); 31                 k++; 32             }while(strtemp[k]!=\0); 33             printf(">\n"); 34             strnset(strtemp,\0,N);/*将字符串strtemp的前N个字符都设为指定字符‘\0‘*/  35         } 36         if((semp[i]!=1)&&((str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122))){ 37             pristring(str,i,semp);} 38         prichar(str,i,semp); 39     } 40 } 41  42 void pristring(char str[],int i,int semp[]) 43 { 44     int j=0; 45     int k; 46     int r[10]={1,1,1,1,1,1,1,1,1,1}; 47     char gechar[N]; 48     char *tempp[10]={"begin","if","then","while","do","end"}; 49     do{ 50           gechar[j]=str[i]; 51           semp[i]=1; 52           i++; 53           j++; 54     }while((semp[i]!=1)&&((str[i]>=65&&str[i]<=90)||(str[i]>=97&&str[i]<=122))); 55     if(strcmp(gechar,tempp[0])==0)  {printf("<1,begin>");} 56     else if(strcmp(gechar,tempp[1])==0)  {printf("<2,if>");} 57     else if(strcmp(gechar,tempp[2])==0)  {printf("<3,then>");} 58     else if(strcmp(gechar,tempp[3])==0)  {printf("<4,while>");} 59     else if(strcmp(gechar,tempp[4])==0)  {printf("<5,do>");} 60     else if(strcmp(gechar,tempp[5])==0)  {printf("<6,end>");} 61     else   { 62         printf("<10,"); 63         for(k=0;k<j;k++) 64         { 65             printf("%c",gechar[k]); 66         } 67         printf(">\n"); 68     } 69     strnset(gechar,\0,N); 70 } 71  72 void prichar(char str[],int i,int semp[]) 73 { 74     char gchar=str[i]; 75     switch(gchar) 76     { 77           case +:printf("<13,%c>\n",gchar);semp[i]=1;break; 78           case -:printf("<14,%c>\n",gchar);semp[i]=1;break; 79           case *:printf("<15,%c>\n",gchar);semp[i]=1;break; 80           case /:printf("<16,%c>\n",gchar);semp[i]=1;break; 81           case :: 82                 if(str[i+1]!==){ 83                     printf("<17,%c>\n",gchar); 84                     semp[i]=1; 85                 } 86                 else if(str[i+1]===){ 87                     printf("<18,%c%c>\n",gchar,str[i+1]); 88                     semp[i]=1; 89                     semp[i+1]=1; 90                 } 91                 break; 92           case <: 93                 if((str[i+1]!==)&&(str[i+1]!=>)){ 94                     printf("<20,%c>\n",gchar); 95                     semp[i]=1; 96                 } 97                 else if(str[i+1]===){ 98                     printf("<21,%c%c>\n",gchar,str[i+1]); 99                     semp[i]=1;100                     semp[i+1]=1;101                 }102                 else if(str[i+1]==>){103                     printf("<22,%c%c>\n",gchar,str[i+1]);104                     semp[i]=1;105                     semp[i+1]=1;106                 }107                 break;108           case >:109                 if(str[i+1]!==){110                     printf("<23,%c>\n",gchar);111                     semp[i]=1;112                 }113                 else if(str[i+1]===){114                     printf("<24,%c%c>\n",gchar,str[i+1]);115                     semp[i]=1;116                     semp[i+1]=1;117                 }118                 break;119           case =:120                if((str[i-1]!=<)&&(str[i-1]!=:)&&(str[i-1]!=>)){121                     printf("<25,%c>\n",gchar);122                     semp[i]=1;123                 }124                 break;125           case ;:printf("<26,%c>\n",gchar);semp[i]=1;break;126           case (:printf("<27,%c>\n",gchar);semp[i]=1;break;127           case ):printf("<28,%c>\n",gchar);semp[i]=1;break;128           case #:printf("<0,%c>\n",gchar);semp[i]=1;break;129           default: break;130     }131 }

运行输出:

技术分享

词法分析程序