-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc0lex.c
339 lines (304 loc) · 4.79 KB
/
c0lex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "c0lex.h"
#include "c0error.h"
#define MAX_TOKEN_SIZE (100)
TokenInfo tokeninfo;
static char ch;
static char token[MAX_TOKEN_SIZE];
static int tp=0;
void escapeChar(){
if(ch=='n')ch='\n';
else if(ch=='t')ch='\t';
else if(ch=='a')ch='\a';
else if(ch=='0')ch='\0';
else if(ch=='r')ch='\r';
else if(ch=='f')ch='\f';
else if(ch=='v')ch='\v';
else if(ch=='\'')ch='\'';
else if(ch=='"')ch='\"';
else if(ch=='?')ch='\?';
else if(ch=='\\')ch='\\';
}
void getch(){
ch = getchar();
tokPos++;
}
void retract(){
ungetc(ch,stdin);
tokPos--;
}
int isLetter(){
return isalpha(ch);
}
void clearToken(){
tp=0;
memset(token,0,sizeof(token));
}
void clearTokenInfo(){
memset(tokeninfo.u.str,0,sizeof(tokeninfo.u.str));
}
void catToken(){
if(tp>=MAX_TOKEN_SIZE){
error(tokPos,"token size over flow");
return;
}
token[tp]=ch;
tp++;
}
int isEnd(){
return (int)ch==-1;
}
int isSpace(){
return ch==' ';
}
int isTab(){
return ch=='\t';
}
int isNewline(){
if(ch=='\n'){
newline();
}
return ch=='\n';
}
int isDigit(){
return isdigit(ch);
}
int reserver(){
if(strcmp("const",token)==0){
return CONSTSY;
}
if(strcmp("int",token)==0){
return INTSY;
}
if(strcmp("if",token)==0){
return IFSY;
}
if(strcmp("else",token)==0){
return ELSESY;
}
if(strcmp("void",token)==0){
return VOIDSY;
}
if(strcmp("while",token)==0){
return WHILESY;
}
if(strcmp("main",token)==0){
return MAINSY;
}
if(strcmp("return",token)==0){
return RETURNSY;
}
if(strcmp("printf",token)==0){
return PRINTFSY;
}
if(strcmp("scanf",token)==0){
return SCANFSY;
}
if(strcmp("float",token)==0){
return FLOATSY;
}
if(strcmp("string",token)==0){
return STRINGSY;
}
return 0;
}
int transNum(){
return atoi(token);
}
float transFloat(){
return atof(token);
}
int isQuo(){
return ch=='"';
}
int isLpark(){
return ch=='{';
}
int isRpark(){
return ch=='}';
}
int isGt(){
return ch=='>';
}
int isLt(){
return ch=='<';
}
int isNe(){
return ch=='!';
}
int isUnl(){
return ch=='_';
}
int isEqu(){
return ch=='=';
}
int isComma(){
return ch==',';
}
int isPlus(){
return ch=='+';
}
int isMinus(){
return ch=='-';
}
int isSemi(){
return ch==';';
}
int isMul(){
return ch=='*';
}
int isBack(){
return ch=='\\';
}
int isLpar(){
return ch=='(';
}
int isRpar(){
return ch==')';
}
int isStar(){
return ch=='*';
}
int isDivi(){ //这个主要判断注释
return ch=='/';
}
int isDot(){
return ch=='.';
}
int isRet(){
return ch=='\r';
}
int get(){
ch = ' ';
clearToken();
clearTokenInfo();
while(isSpace() | isNewline() | isTab()|isRet()){
getch();
}
if(isEnd()){
tokeninfo.sym=EOFSY;
return 0; // 返回0表示正常结束
}
else if(isLetter()|isUnl()){
while(isLetter()||isDigit()||isUnl()){
catToken();
getch();
}
retract();
int resultValue=reserver();
if(resultValue==0){
tokeninfo.sym = IDSY;
strcat(tokeninfo.u.str,token);
}
else tokeninfo.sym=resultValue;
}
else if(isDigit()){
while(isDigit()){
catToken();
getch();
}
if(isDot()){
catToken();
getch();
while(isDigit()){
catToken();
getch();
}
retract();
tokeninfo.u.f =transFloat();
tokeninfo.sym =FLOAT;
}
else{
retract();
tokeninfo.u.num=transNum(token);
tokeninfo.sym = INT;
}
}
else if(isGt()){
getch();
if(isEqu()){
tokeninfo.sym=GESY;
}else{
retract();
tokeninfo.sym=GSY;
}
}
else if(isLt()){
getch();
if(isEqu()){
tokeninfo.sym=LESY;
}else{
retract();
tokeninfo.sym=LSY;
}
}
else if(isNe()){
getch();
if(isEqu()){
tokeninfo.sym=NESY;
}else{
error(tokPos,"expected = here (not !=)");
}
}else if(isEqu()){
getch();
if(isEqu()){
tokeninfo.sym=EQSY;
}else{
tokeninfo.sym=ASSSY;
retract();
}
}
else if(isMinus())tokeninfo.sym = MINUSSY;
else if(isPlus())tokeninfo.sym= ADDSY;
else if(isLpar())tokeninfo.sym= LPSY;
else if(isRpar())tokeninfo.sym= RPSY; //括号
else if(isLpark())tokeninfo.sym= LPKSY;
else if(isRpark())tokeninfo.sym= RPKSY;//大括号
else if(isComma())tokeninfo.sym=COMMASY;
else if(isSemi())tokeninfo.sym=SEMISY;
else if(isMul())tokeninfo.sym=MUSY;
else if(isDivi()){
getch();
if(isStar()){
do{
do{
getch();
}while(!isStar());
do{
getch();
if(isDivi())return -1;
}while(isStar());
}while(!isStar());
}
retract();
tokeninfo.sym=DISY;
}else if(isQuo()){
getch();
while(!isQuo()){
if(isBack()){
getch();
escapeChar();
catToken();
}else{
catToken();
}
getch();
if(isEnd()){
error(tokPos,"string doesn't end");
return 0;
}
}
tokeninfo.sym=STRING;
strcpy(tokeninfo.u.str,token);
}
else{
char mes[20]="unknown char ";
mes[13]=ch;mes[14]='\0';
error(tokPos,mes);
return -1;
}
return 1;
}