正在学习编译原理,试着写了一个词法分析器
代码
1 #include<iostream>
2 #include<string>
3 #include <ctype.h>
4 using namespace std;
5 void scaner(); //扫描子程序
6 char getch(); //读入字符函数
7 char check(); //检查读入字符是否为空格或注释
8 int synvalue(int n);//求关键字种别码函数
9 char prog[80],token[8];
10 char ch;
11 int syn,p=0,m,n,sum;//p是缓冲区prog指针,m是token指针
12 char *rwtab[6]={"begin","if","then","while","do","end"};
13 int main()
14 {
15 p=0;
16 cout<<"please input string:\n";
17 do
18 {
19 ch=cin.get();
20 prog[p++]=ch;
21 }while(ch!='#');
22 p=0;
23 do
24 {
25 scaner();
26 switch(syn)
27 {
28 case 11:cout<<"("<<syn<<","<<sum<<")"<<endl;
29 break;
30 case -1:cout<<"have error."<<endl;
31 break;
32 case 10:cout<<"("<<syn<<","<<"'"<<token<<"'"<<")"<<endl;
33 break;
34 default:cout<<"("<<syn<<","<<token<<")"<<endl;
35 }
36 }while(syn!=0);
37 return 0;
38 }
39 void scaner()
40 {
41 for(n=0;n<8;n++)
42 token[n]=NULL;
43 ch=getch();//读入下一个字符
44 check();//检查读入字符是否是空格或注释
45 m=0;
46 if(isalpha(ch))
47 {
48 while(isalpha(ch)||isdigit(ch))
49 {
50 token[m++]=ch;
51 ch=getch();
52 }
53 token[m++]='\0';
54 p--;//回退一个字符
55 syn=10;
56 for(n=0;n<6;n++)
57 {
58 if(strcmp(token,rwtab[n])==0)
59 {
60 syn=synvalue(n);//返回种别码syn
61 break;
62 }
63 }
64 }
65 else
66 if(isdigit(ch))
67 {sum=0;
68 while(isdigit(ch))
69 {
70
71 sum=sum*10+ch-'0';
72 ch=getch();
73 }
74 p--;
75 syn=11;
76 }
77 else
78 switch(ch)
79 {
80 case'<': m=0;
81 token[m++]=ch;
82 ch=getch();
83 if(ch=='>')
84 {
85 syn=21;
86 token[m++]=ch;
87 }
88 else if(ch=='=')
89 {
90 syn=22;
91 token[m++]=ch;
92 }
93 else
94 {
95 syn=20;
96 p--;
97 }
98 break;
99 case'>':token[m++]=ch;
100 ch=getch();
101 if(ch=='=')
102 {
103 syn=24;
104 token[m++]=ch;
105 }
106 else
107 {
108 syn=23;
109 p--;
110 }
111 break;
112 case':':token[m++]=ch;
113 ch=getch();
114 if(ch=='=')
115 {
116 syn=18;
117 token[m++]=ch;
118 }
119 else
120 {
121 syn=17;
122 p--;
123 }
124 break;
125 case'+':syn=13;token[0]=ch;break;
126 case'-':syn=14;token[0]=ch;break;
127 case'*':syn=15;token[0]=ch;break;
128 case'/':syn=16;token[0]=ch;break;
129 case'=':syn=25;token[0]=ch;break;
130 case';':syn=26;token[0]=ch;break;
131 case'(':syn=27;token[0]=ch;break;
132 case')':syn=28;token[0]=ch;break;
133 case'#':syn=0;token[0]=ch;break;
134 default:syn=-1;
135
136 }
137 }
138
139 char getch()
140 {
141 return prog[p++];
142 }
143 int synvalue(int n)
144 {
145 return n+1;
146 }
147 char check()
148 {
149 if(ch==' ')
150 {
151 do
152 {
153 ch=getch();
154 }while(ch==' ');
155 return ch;
156 }
157 else if(ch == '/')
158 {
159 ch=getch();
160 if(ch=='*')
161 {
162 do
163 {
164 ch=getch();
165 while(ch!='*')
166 ch=getch();
167 ch=getch();
168 while(ch=='*')
169 ch=getch();
170 }while(ch!='/');
171 ch=getch();
172 return ch;
173 }
174 else
175 {
176 p--;
177 ch='/';
178 return ch;
179 }
180 }
181 else
182 return ch;
183
184 }
2 #include<string>
3 #include <ctype.h>
4 using namespace std;
5 void scaner(); //扫描子程序
6 char getch(); //读入字符函数
7 char check(); //检查读入字符是否为空格或注释
8 int synvalue(int n);//求关键字种别码函数
9 char prog[80],token[8];
10 char ch;
11 int syn,p=0,m,n,sum;//p是缓冲区prog指针,m是token指针
12 char *rwtab[6]={"begin","if","then","while","do","end"};
13 int main()
14 {
15 p=0;
16 cout<<"please input string:\n";
17 do
18 {
19 ch=cin.get();
20 prog[p++]=ch;
21 }while(ch!='#');
22 p=0;
23 do
24 {
25 scaner();
26 switch(syn)
27 {
28 case 11:cout<<"("<<syn<<","<<sum<<")"<<endl;
29 break;
30 case -1:cout<<"have error."<<endl;
31 break;
32 case 10:cout<<"("<<syn<<","<<"'"<<token<<"'"<<")"<<endl;
33 break;
34 default:cout<<"("<<syn<<","<<token<<")"<<endl;
35 }
36 }while(syn!=0);
37 return 0;
38 }
39 void scaner()
40 {
41 for(n=0;n<8;n++)
42 token[n]=NULL;
43 ch=getch();//读入下一个字符
44 check();//检查读入字符是否是空格或注释
45 m=0;
46 if(isalpha(ch))
47 {
48 while(isalpha(ch)||isdigit(ch))
49 {
50 token[m++]=ch;
51 ch=getch();
52 }
53 token[m++]='\0';
54 p--;//回退一个字符
55 syn=10;
56 for(n=0;n<6;n++)
57 {
58 if(strcmp(token,rwtab[n])==0)
59 {
60 syn=synvalue(n);//返回种别码syn
61 break;
62 }
63 }
64 }
65 else
66 if(isdigit(ch))
67 {sum=0;
68 while(isdigit(ch))
69 {
70
71 sum=sum*10+ch-'0';
72 ch=getch();
73 }
74 p--;
75 syn=11;
76 }
77 else
78 switch(ch)
79 {
80 case'<': m=0;
81 token[m++]=ch;
82 ch=getch();
83 if(ch=='>')
84 {
85 syn=21;
86 token[m++]=ch;
87 }
88 else if(ch=='=')
89 {
90 syn=22;
91 token[m++]=ch;
92 }
93 else
94 {
95 syn=20;
96 p--;
97 }
98 break;
99 case'>':token[m++]=ch;
100 ch=getch();
101 if(ch=='=')
102 {
103 syn=24;
104 token[m++]=ch;
105 }
106 else
107 {
108 syn=23;
109 p--;
110 }
111 break;
112 case':':token[m++]=ch;
113 ch=getch();
114 if(ch=='=')
115 {
116 syn=18;
117 token[m++]=ch;
118 }
119 else
120 {
121 syn=17;
122 p--;
123 }
124 break;
125 case'+':syn=13;token[0]=ch;break;
126 case'-':syn=14;token[0]=ch;break;
127 case'*':syn=15;token[0]=ch;break;
128 case'/':syn=16;token[0]=ch;break;
129 case'=':syn=25;token[0]=ch;break;
130 case';':syn=26;token[0]=ch;break;
131 case'(':syn=27;token[0]=ch;break;
132 case')':syn=28;token[0]=ch;break;
133 case'#':syn=0;token[0]=ch;break;
134 default:syn=-1;
135
136 }
137 }
138
139 char getch()
140 {
141 return prog[p++];
142 }
143 int synvalue(int n)
144 {
145 return n+1;
146 }
147 char check()
148 {
149 if(ch==' ')
150 {
151 do
152 {
153 ch=getch();
154 }while(ch==' ');
155 return ch;
156 }
157 else if(ch == '/')
158 {
159 ch=getch();
160 if(ch=='*')
161 {
162 do
163 {
164 ch=getch();
165 while(ch!='*')
166 ch=getch();
167 ch=getch();
168 while(ch=='*')
169 ch=getch();
170 }while(ch!='/');
171 ch=getch();
172 return ch;
173 }
174 else
175 {
176 p--;
177 ch='/';
178 return ch;
179 }
180 }
181 else
182 return ch;
183
184 }