码迷,mamicode.com
首页 > 其他好文 > 详细

词法分析程序

时间:2015-10-14 23:32:01      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include<stdio.h>
  2 #include<string.h>
  3 #define max 1000
  4 char a,token[max],word[max];
  5 void main()
  6 {
  7     int n=0,i=0;
  8     char *rwtab[6]={"begin","if","then","while","do","end"};
  9     int p=0,j=0,k=0,m=0;
 10     int key=0;
 11     printf("Please input the source code:\n");
 12     while(a!=#)
 13     {
 14         scanf("%c",&a);
 15         word[p]=a;
 16         p++;
 17     };
 18     while(word[i]!=#)
 19     {
 20         while((word[i]>=a&&word[i]<=z)||(word[i]>=A&&word[i]<=Z))
 21         {
 22             if((word[i]>=a&&word[i]<=z)||(word[i]>=A&&word[i]<=Z))
 23             {
 24                 token[n]=word[i];
 25                 n++;
 26                 token[n]=\0;
 27                 i++;           
 28             }
 29             for(k=0;k<6;k++)
 30             {
 31                 key=0;
 32                 if(strcmp(rwtab[k],token)==0)
 33                 {
 34                     printf("<%d,%s>\n",k+1,rwtab[k]);
 35                     key=1;
 36                     break;
 37                 }
 38             }   
 39         }
 40         if(key==0)
 41         {
 42             printf("<10,%s>\n",token);
 43         }
 44         key=1;
 45         i=i+1;
 46         n=0;
 47     }
 48     p=0;
 49     int sign=0;
 50     for(p=0;p<max;p++)
 51     {
 52         switch(word[p])
 53         {
 54         case +:
 55             printf("<13,+>\n");
 56             break;
 57         case -:
 58             printf("<14,->\n");
 59             break;
 60         case *:
 61             printf("<15,*>\n");
 62             break;
 63         case /:
 64             printf("<16,/>\n");
 65             break;
 66         case ::
 67             if(word[p+1]===)
 68             {
 69                 printf("<18,:=>\n");
 70                 p++;
 71             }
 72             else
 73                 printf("<17,:>\n");
 74             break;
 75         case <:
 76             if(word[p+1]===)
 77             {
 78                 printf("<21,<=>\n");
 79                 sign=1;
 80                 p++;
 81             }
 82             else if(word[p+1]==>)
 83             {
 84                 printf("<22,<>>\n");
 85                 sign=1;
 86                 p++;
 87             }
 88             else
 89                 printf("<20,<>\n");
 90             break;
 91         case >:
 92             if(word[p+1]===)
 93             {
 94                 printf("<24,>=>\n");
 95                 p++;
 96             }
 97             else
 98                 printf("<23,>>\n");
 99             break;
100         case =:
101             printf("<25,=>\n");
102             break;
103         case ;:
104             printf("<26,;>\n");
105             break;
106         case (:
107             printf("<27,(>\n");
108             break;
109         case ):
110             printf("<28,)>\n");
111             break;
112         case #:
113             printf("<29,#>\n");
114             break;
115         }
116     }
117     p=0,j=0,key=0;
118     int t=0;
119     char Shu[100];
120     while(word[p]!=#)
121     {
122         key=0;
123         while(word[p]>=0&&word[p]<=9)
124         {
125             Shu[j]=word[p];
126             p++;
127             j++;
128             key=1;
129         }
130         if(key==1)
131         {
132             printf("<11,");
133             for(t=0;t<j;t++)
134             {
135                 printf("%c",Shu[t]);
136             }
137             printf(">\n");
138             j=0;
139         }
140         p++;
141     }
142 }

 

词法分析程序

标签:

原文地址:http://www.cnblogs.com/qq1942589447/p/4881035.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!