编译原理第二次试验

#include<stdio.h> #include<string.h> void Fenxi(char c,char b); void word(char a[]); void number(char a[]); int i; int s=1; main(){     char a[100];     printf("请输入源程序:");     gets(a);     printf("%s",a);     printf("\n");     for(i=0;(a[i]!='\0')&&(i<100)&&s==1;i++)     {         if((a[i]>='a' && a[i]<='z')||(a[i]>='A' && a[i]<='Z'))             word(a);         else if(a[i]>='0' && a[i]<='9')             number(a);         else             Fenxi(a[i],a[i+1]);     }     printf("\n"); }

void number(char a[])  //对数字字符进行扫描分析 {     char b[50];     int m,k=0,t;     m=i;     while(a[m]>='0' && a[m]<='9')      {         b[k]=a[m];   //用数组b存放数组a中的数字         k++;         m++;     }     i=m-1;     printf("(11,");     for(t=0;t<k;t++)             printf("%c",b[t]);     printf(")"); }

void word(char a[])   {     int k=0,m,flag=0,t;     char b[50];     char *key[6]={"begin","if","then","while","do","end"};     m=i;     while((a[m]>='a'&&a[m]<='z')||(a[m]>='A'&&a[m]<='Z'))  //用数组b存放数组a中的字母     {         b[k]=a[m];           k++;         b[k]='\0';         m++;                                                                                                                         }     i=m-1;     for(t=0;t<6;t++)     {         if(strcmp(b,key[t])==0)         {             printf("(%d,%s)",t+1,key[t]);              flag=1;         }     }     if(flag==0)     {         printf("(10,%s)",b);      } } void Fenxi(char c,char b)  //对特殊字符进行扫描分析 {     switch(c){         case ' ':             break;         case '+':             printf("(13,+)");             break;         case '-':             printf("(14,-)");             break;         case '*':             printf("(15,*)");             break;         case '/':             printf("(16,/)");             break;         case ':':             if(b=='=')             {                 i++;                 printf("(18,:=)");             }             else                 printf("(17,:)");             break;         case '<':             if(b=='>')             {                 i++;                 printf("(21,<>)");             }             else if(b=='=')             {                 i++;                 printf("(22,<=)");             }             else                 printf("(20,<)");             break;         case '>':             if(b=='=')             {                 printf("(24,>=)");                 i++;             }             else                 printf("(23,>)");             break;         case '=':             printf("(25,=)");             break;         case ';':             printf("(26,;)");             break;         case '(':             printf("(27,()");             break;         case ')':             printf("(28,))");             break;         case '#':             printf("(20,#)");             break;         default:             {             printf("\n存在字符 '%c',无法继续识别!\n",c);             s=0;    //用s=0记录存在非法字符             break;             }     } }

 

posted @ 2015-09-21 19:04  45赵创佳  阅读(184)  评论(0编辑  收藏  举报