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

词法分析程序

时间:2016-09-30 18:39:21      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

  1 #include <stdio.h> 
  2 #include <string.h>  
  3 char prog[30],token[5],ch; 
  4 int syn,p,m,n,sum;  
  5 char *rwtab[6]={"begin","if","then","while","do","end"}; 
  6 scaner(); 
  7 main() 
  8 {p=0;  
  9  printf("请输入你要分析的程序(按“#”结束):"); 
 10  do{  
 11      scanf("%c",&ch); 
 12      prog[p++]=ch; 
 13  }while(ch!=#); 
 14    p=0; 
 15  do{  scaner(); 
 16    switch(syn)  
 17    {    case 11:printf("( %-10d%5d )\n",sum,syn); 
 18         break;  
 19         case -1:printf("you have input a wrong string\n"); 
 20         getch(); 
 21         exit(0);  
 22         default: printf("( %-10s%5d )\n",token,syn); 
 23         break; 
 24 }  
 25 }while(syn!=0); 
 26   getch();  
 27 } 
 28    scaner() 
 29    {  sum=0;  
 30     for(m=0;m<8;m++)token[m++]=NULL; 
 31      ch=prog[p++]; 
 32      m=0;  
 33  while((ch== )||(ch==\n))ch=prog[p++];  
 34  if(((ch<=z)&&(ch>=a))||((ch<=Z)&&(ch>=A)))  
 35  { 
 36      while(((ch<=z)&&(ch>=a))||((ch<=Z)&&(ch>=A))||((ch>=0)&&(ch<=9))) 
 37  {
 38          token[m++]=ch; 
 39          ch=prog[p++]; 
 40 }  
 41     p--; 
 42     syn=10;
 43     for(n=0;n<6;n++)  
 44     if(strcmp(token,rwtab[n])==0) 
 45     {  syn=n+1; 
 46        break;  
 47     } 
 48  }  
 49     else if((ch>=0)&&(ch<=9)) 
 50     {  while((ch>=0)&&(ch<=9)) 
 51     {  sum=sum*10+ch-0; 
 52         ch=prog[p++]; 
 53     } 
 54       p--;  
 55       syn=11; 
 56     }  
 57      else switch(ch)  
 58      {   case <:token[m++]=ch; 
 59     ch=prog[p++]; 
 60 if(ch===) 
 61 { 
 62     syn=22;  
 63     token[m++]=ch; 
 64 }  
 65 else  
 66 { 
 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         case -:token[m++]=ch;  
 94            ch=prog[p++]; 
 95     if(ch==-) 
 96     {        syn=29;  
 97          token[m++]=ch; 
 98     }  
 99         else  
100         {       syn=14; 
101                 p--; 
102         }  
103           break; 
104        case !:ch=prog[p++];
105     if(ch===) 
106     { syn=21;  
107       token[m++]=ch; 
108     } 
109     else 
110     { syn=31;
111        p--;
112     }  
113       break; 
114       case =:token[m++]=ch; 
115           ch=prog[p++];
116            if(ch===) 
117            { syn=25;  
118               token[m++]=ch;  
119            } 
120       else 
121       { syn=18; 
122          p--; 
123       } 
124         break;
125         case *: syn=15;  
126           token[m++]=ch;  
127           break; 
128         case /: syn=16;  
129            token[m++]=ch; 
130            break; 
131         case (: syn=27;
132             token[m++]=ch; 
133             break; 
134         case ): syn=28;  
135             token[m++]=ch; 
136             break; 
137         case {: syn=5;  
138             token[m++]=ch; 
139             break; 
140         case }: syn=6;
141             token[m++]=ch; 
142             break; 
143         case ;: syn=26;  
144             token[m++]=ch; 
145             break; 
146         case \": syn=30;  
147             token[m++]=ch; 
148              break; 
149         case #: syn=0;  
150             token[m++]=ch; 
151              break; 
152         case ::syn=17;  
153             token[m++]=ch;  
154              break;
155         default: syn=-1; 
156              break; 
157 }
158 token[m++]=\0; 
159 }  

 

单词符号

种别码

单词符号

种别码

begin

1

:

17

if

2

:=

18

then

3

<

20

while

4

<=

21

do

5

<>

22

end

6

>

23

l(l|d)*

10

>=

24

dd*

11

=

25

+

13

;

26

-

14

(

27

*

15

)

28

/

16

#

0

词法分析程序

标签:

原文地址:http://www.cnblogs.com/li123/p/5924422.html

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