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

词法分析程序(C)

#include <stdio.h>  #include <string.h>    char string[80],word[8],ch;  int type,p,i,n,sum;  char keyword[][6]={"begin","if","then","while","do","end"};    //关键字数组   void scaner(void);    main()  {      p=0;      printf("\n\n请输入一个程序段(以‘#‘结束):\n");      do{              scanf("%c",&ch);              string[p++]=ch;      }while(ch!=#);       printf("单词符号\t种别码 \n");      p=0;      do{              scaner();              switch(type)              {                  case 11:                      printf("   %-10d%5d\n",sum,type);                  break;                  case -1:                      printf("有无法识别的字符\n");                        return 0;                  break;                       default:                   printf("   %-10s%5d \n",word,type);                  break;              }          }while(type!=0);   }    void scaner(void)  {        sum=0;      for(i=0;i<8;i++)         word[i++]= NULL;            ch=string[p++];      i=0;                while((ch== )||(ch==\n))          ch=string[p++];            if(((ch<=z)&&(ch>=a))||((ch<=Z)&&(ch>=A)))       {           while(((ch<=z)&&(ch>=a))||((ch<=Z)&&(ch>=A))||((ch>=0)&&(ch<=9)))          {              word[i++]=ch;              ch=string[p++];          }               p--;      type=10;                     //先将以字母开头的字符识别为标识符          for(n=0;n<6;n++)          if(strcmp(word,keyword[n])==0)    //关键字数组比较,相同则表示为关键字,种别码即为下标值加一        {               type=n+1;              break;          }      }                                               else if((ch>=0)&&(ch<=9))       {           while((ch>=0)&&(ch<=9))          {              sum=sum*10+ch-0;    //将字符型转化为整形sum            ch=string[p++];          }          p--;          type=11;       }      else                   //else语句内识别除关键字、标识符、数字以外的其他字符    {          switch(ch)          {          case +:              type=13;              word[i++]=ch;                  break;            case -:               type=14;            word[i++]=ch;           break;                  case *:              type=15;              word[i++]=ch;          break;            case /:              type=16;              word[i++]=ch;          break;          case ::              word[i++]=ch;              ch=string[p++];           if(ch===)             {                 type=18;                 word[i++]=ch;             }              else              {                   type=18;                  p--;              }          break;                 case <:              word[i++]=ch;              ch=string[p++];              if(ch===)              {                   type=21;                  word[i++]=ch;              }              else              {                    type=20;                  p--;              }          break;            case >:              word[i++]=ch;              ch=string[p++];              if(ch===)              {                  type=24;                  word[i++]=ch;              }              else              {                   type=23;                  p--;              }          break;                case =:                  type=25;                  word[i++]=ch;          break;            case ;:              type=26;              word[i++]=ch;          break;          case (:               type=27;              word[i++]=ch;          break;            case ):              type=28;              word[i++]=ch;          break;            case #:               type=0;              word[i++]=ch;          break;            default:              type=-1;          break;          }      }          word[i++]=\0;                      //word字符数组变字符串}

 

词法分析程序(C)