-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScanner.c
846 lines (714 loc) · 18.9 KB
/
Scanner.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#include<stdio.h>
#include<stdlib.h>
#include "state.h"
#include "ylib.h"
/*
Scanner.c
@author octobershiner
@version 0.1
2011 12 02
Description: This file implements the basic scanning function
*/
#define BUFFER_SIZE 30 /*The Max length of the buffer*/
#define MAX_LINE 250 /*Define the capacity of each line*/
#define TOKEN_SIZE 30 /*Define the max token size*/
/*Define Keyword*/
char keywords[30][30] = {"int","float","char","return","while","for","if","else","void"};
/*Operator*/
char symbol[40][30]= {"","","","","",
"{","}","+","-","/","*","=",
">","<",";","(",")",
"!=","[","]","<=",">=","/*","*/","&&","||","==",",","&","|"};
/*用于保存验证匹配类型 默认为双引号*/
char toMatch = 34;
/*用于标记 左右匹配情况 依次为 ( ) [] {} 注释符*/
int Lmatch[4] ={0,0,0,0};
/*用于记录是否开始注释*/
int Comment_Start = NO;
/*
*扫描函数
*/
void scanner(char * file_name)
{
char current_line[MAX_LINE]; /*存储每一行的字符串*/
char * current_p = NULL; /*当前字符指针*/
int line_number = 1;
char token_str[TOKEN_SIZE]; /*用于存储每一个token串*/
int token_ptr = 0; /*指向当前token串尾*/
int last_state = START; /*用于标记上一个token串的类型*/
int state = START;
int add = YES;
/*开始 读取源代码文件*/
FILE * fp = NULL;
if ( ( fp = fopen(file_name,"r") ) == NULL)
{
printf("YuiToken->Error: There is no such file.\n");
Token_Error = YES;
exit(0);
}
printf("YuiToken->Success: Source File loaded successfully!\n");
/*扫描代码中的每一行*/
while(fgets(current_line,MAX_LINE,fp) != 0)
{
printf("\nLine %d : \n",line_number);
current_p = current_line; /*重置当前指针*/
while(1)
{
add = YES;
switch(state)
{
case START:
/*处理空白字符*/
if(Comment_Start == YES && *(current_p) != '*' && *(current_p+1) != '/')
{
add =NO;
}
else if(isBlank(*current_p) == YES)
{
add = NO;
}
else if( isNumber(*current_p) == YES)
{
state = CONST_INT;
}
else if (*current_p ==39)/*遇到了单引号*/
{
state = CONST_CHAR;
}
else if(*current_p == 34) /*遇到了双引号*/
{
state = CONST_STRING;
}
else if( isLetter(*current_p) == YES)
{
state = IDN;
}
else if(*current_p == '+' || *current_p =='-' ||*current_p == ';'||*current_p ==',')
{
last_state = isSingleOP(current_p);
state = END;
}
else if(*current_p == '>' || *current_p == '<' || *current_p == '=')
{
if( *(current_p+1) == '=')
{
if(*current_p == '>')
state = GE;
else if(*current_p == '<')
state = LE;
else if(*current_p == '=')
state = EE;
}
else
{
last_state = isSingleOP(current_p);
state = END;
}
}
else if(*current_p == '&')
{
if( *(current_p+1) == '&')
state = AND;
else
{
last_state = isSingleOP(current_p);
state = END;
}
}
else if(*current_p == '|')
{
if( *(current_p+1) == '|')
state = OR;
else
{
last_state = isSingleOP(current_p);
state = END;
}
}
else if(*current_p == '!')
{
if( *(current_p+1) == '=')
state = NE;
else
{
last_state = ERROR;
state = END;
}
}
else if(*current_p =='(')
{
Lmatch[0]++;
last_state = SLP;
state = END;
}
else if(*current_p ==')')
{
if(Lmatch[0] > 0) /*判断前面是否有左括号可匹配*/
{
Lmatch[0]--;
last_state = SRP;
state = END;
}
else
{
toMatch = '(';
last_state = UNMATCH;
state = END;
}
}
else if(*current_p =='[')
{
Lmatch[1]++;
last_state = ML;
state = END;
}
else if(*current_p ==']')
{
if(Lmatch[1] > 0) /*判断前面是否有左中括号可匹配*/
{
Lmatch[1]--;
last_state = MR;
state = END;
}
else
{
toMatch = '[';
last_state = UNMATCH;
state = END;
}
}
else if(*current_p == '{')
{
Lmatch[2]++;
last_state = LP;
state = END;
}
else if(*current_p =='}')
{
if(Lmatch[2] > 0) /*判断前面是否有左大括号可匹配*/
{
Lmatch[2]--;
last_state = RP;
state = END;
}
else
{
toMatch = '{';
last_state = UNMATCH;
state = END;
}
}
else if(*current_p == '/')
{
if(*(current_p+1) =='*')
{
state = LC;
}
else
{
last_state = isSingleOP(current_p);
state = END;
}
}
else if(*current_p == '*')
{
if(*(current_p+1) == '/')
{
state = RC;
Comment_Start = NO;
}
else
{
last_state = isSingleOP(current_p);
state = END;
}
}
else
{
add = NO;
}
break;
/*处理整型*/
case CONST_INT:
//printf("CONST_INT\n");
if( *current_p == '.') /*发现小数点,转到浮点状态*/
{
state = CONST_FLOAT;
}
else if(!isNumber(*current_p) && !isLetter(*current_p)) /*发现了其他字符或空格*/
{
last_state = state;
state = END; /*状态结束*/
current_p--; /*处理指针回退*/
add = NO;
}
break;
case CONST_FLOAT:
if(isLetter(*current_p) || *current_p =='.' ) /*发现了字母或者其他非法结束符*/
{
state = ERROR;
}
if(!isNumber(*current_p) ) /*发现了其他字符或空格*/
{
last_state = state;
state = END; /*状态结束*/
current_p--; /*处理指针回退*/
add = NO;
}
break;
case CONST_CHAR:
if(*current_p == 39) /*标识收到了单引号*/
{
last_state = state;
state = END;
}
else if(*current_p =='\0')
{
last_state = UNMATCH;
toMatch = 39;
state = UNMATCH;
}
break;
case CONST_STRING:
if( *current_p == 34) /*标识收到了结束的双引号*/
{
last_state = state;
state = END;
}
else if(*current_p == '\0')
{
last_state = UNMATCH;
toMatch = 34;
state = UNMATCH;
}
break;
case IDN:
//printf("IDN\n");
if(*current_p == '\0') /*处理换行情况*/
{
last_state = state;
state = END;
add = NO;
}
else if(isLetter(*current_p) == NO && isNumber(*current_p) == NO && *current_p !='_')
{
//printf("back\n");
last_state =state;
state = END;
current_p--;
add = NO;
}
break;
case GE:
last_state = state;
state = END;
break;
case LE:
last_state = state;
state = END;
break;
case EE:
last_state = state;
state = END;
break;
case AND:
last_state = state;
state = END;
break;
case OR:
last_state = state;
state = END;
break;
case NE:
last_state = state;
state = END;
break;
case LC:
last_state = state;
state = END;
Lmatch[3]++;
break;
case RC:
if(Lmatch[3] >0)
{
Lmatch[3]--;
last_state = state;
state = END;
Comment_Start = NO; /*关闭注释处理*/
}
else
{
toMatch = 'c';
last_state = UNMATCH;
state = END;
}
break;
case ERROR:
last_state = state;
state = END; /*状态结束*/
current_p--; /*处理指针回退*/
add = NO;
break;
case UNMATCH:
break;
case END:
break;
default:
printf("unknown");
Token_Error = YES;
break;
}
if(add == YES && Comment_Start ==NO)
{
token_str[token_ptr++] = *current_p;
}
/*保存token串*/
if(state == END || state == UNMATCH)
{
token_str[token_ptr] = '\0'; /*结束当前的token串*/
token_ptr = 0;
while(token_str[token_ptr] != '\0')
printf("%c",token_str[token_ptr++]);
printf("\t\t\t");
printType(last_state,token_str,line_number);
token_ptr = 0;
state = START;
}
/*已到达行尾,结束,开始下一行*/
if(*current_p == '\0')
{
if(Lmatch[0] > 0 )
{
printf("Error: Line:%d Lack \'%c\' to match \n",line_number,')');
Token_Error = YES;
Lmatch[0]--;
}
if(Lmatch[1] > 0 )
{
printf("Error: Line:%d Lack \'%c\' to match \n",line_number,']');
Token_Error = YES;
Lmatch[1]--;
}
break;
}
current_p++;
}
line_number++;
}
/*扫描结束*/
/*源代码文件读取结束*/
if(Lmatch[2]>0)
{
printf("Error: Line:%d Lack \'%c\' to match \n",line_number,'}');
Token_Error = YES;
}
if(Lmatch[3]>0)
{
printf("Error: Line:%d Lack */ to match comments\n",line_number);
Token_Error = YES;
}
printf("\n----------------------------------\n");
printf("Source File Scanning finished .\n");
}
void getToken(char * c)
{
}
/*函数:判断字符是否为字母*/
int isLetter(char c)
{
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
return YES;
}
return NO;
}
/*函数:判断字符是否为数字*/
int isNumber(char c)
{
if( c >= '0' && c <= '9')
{
return YES;
}
return NO;
}
/*判断是否空格存在*/
int isBlank(char c)
{
if(c == ' ' || c == '\t')
{
return YES;
}
return NO;
}
/*打印字符串对应的类型*/
void printType(int Mode,char *pc,int Line)
{
// printf("%d\n",Mode);
int FLAG = 1; /*用于判断是否有转义字符*/
char *toSave = pc; /*用于保存进入token序列的词素*/
int keyword_type = 40; /*用于保存关键字类型*/
char *p = pc;
//printf("PRINT%d\n",Mode);
switch(Mode)
{
case CONST_INT:
while(*p != '\0')
if(isNumber(*p++) == NO)
{
printf("Error:Line:%d illlegal INT or Identifier.\n",Line);
Token_Error = YES;
FLAG = 0;
break;
}
if(FLAG)
{
printf("CONST INT\t");
printf("%d\n",Mode);
Token = add(Token,toSave,Mode,Line,0,0);
}
break;
case CONST_FLOAT:
printf("CONST FLOAT\t");
printf("%d\n",Mode);
Token = add(Token,toSave,Mode,Line,0,0); /*向字符链表中添加*/
break;
case CONST_CHAR:
if( *(p+2)==39)
{
printf("CONST CHAR\t");
printf("%d\n",Mode);
Token = add(Token,toSave,Mode,Line,0,0);
}
else
printf("Error:Line:%d illegal const char\n",Line);
Token_Error = YES;
break;
case CONST_STRING:
while(*p != '\0')
{
if(*p++ == 39)
FLAG = 0;
}
if(FLAG)
{
printf("STRING\t");
printf("%d\n",Mode);
Token = add(Token,toSave,Mode,Line,0,0);
}
else
printf("Error:Line:%d need to transfer\n",Line);
Token_Error = YES;
break;
case IDN:
keyword_type = isKeyword(pc);
if(keyword_type== INT)
{
printf("INT\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== FLOAT)
{
printf("FLOAT\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== CHAR)
{
printf("CHAR\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== RETURN)
{
printf("RETURN\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== WHILE)
{
printf("WHILE\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== FOR)
{
printf("FOR\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== IF)
{
printf("IF\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== ELSE)
{
printf("ELSE\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else if(keyword_type== VOID)
{
printf("VOID\t%d\n",keyword_type);
Token = add(Token,"",keyword_type,Line,0,0); /*属性值默认缺省*/
}
else /*标识符情况++++++*/
{
printf("IDN\t");
printf("%d\n",Mode);
Token = add(Token,toSave,Mode,Line,0,0);
}
break;
case ADD:
printf("ADD\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case SUB:
printf("SUB\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case SEMI:
printf("SEMI\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case COM:
printf("COM\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case DIV:
printf("DIV\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case GE:
printf("GE\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case LE:
printf("LE\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case EE:
printf("EE\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case GT:
printf("GT\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case LT:
printf("LT\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case EQ:
printf("EQ\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case AND:
printf("AND\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case ADDR:
printf("ADDR\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case OR:
printf("OR\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case ORR:
printf("ORR\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case NE:
printf("NE\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case SLP:
printf("SLP\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case SRP:
printf("SRP\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case ML:
printf("ML\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case MR:
printf("MR\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case LP:
printf("LP\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case RP:
printf("RP\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case LC:
Comment_Start = YES; /*开始处理注释*/
printf("LC\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case RC:
printf("RC\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case MUL:
printf("MUL\t%d\n",Mode);
Token = add(Token,"",Mode,Line,0,0); /*属性值默认缺省*/
break;
case UNMATCH:
Token_Error = YES;
if(toMatch == 'c')
printf("Error: Line:%d Lack /* to match comments\n",Line);
else
printf("Error: Line:%d Lack \'%c\' to match \n",Line,toMatch);
break;
case ERROR:
printf("Error: Line:%d Unknown token. \n",Line);
Token_Error = YES;
break;
}
}
/*用于判别是否为关键字*/
int isKeyword(char *c)
{
int i ;
for ( i = 0; i < 9; i++)
if( strcmp(c,keywords[i]) == 0 )
return ( i+40);
return NO;
}
/*用于判别是否为单界特殊符号*/
int isSingleOP(char *c)
{
int i ;
char cc[2];
cc[0] =*c;
cc[1] = '\0';
for ( i = 0; i < 30; i++)
{
if( strcmp(cc,symbol[i]) == 0 )
return (i+1); /*返回具体的符号类型*/
}
return NO;
}
/*判别是否可合成双界符*/
int isMatchable(char pre,char c)
{
/*包含了 += -= == := != >= <=*/
if(pre =='+' || pre == '-' || pre == ':' || pre =='=' || pre=='!'|| pre =='>' || pre == '<')
{
if(c == '=')
return YES;
else
return NO;
}
else if(pre == '&' && c == '&')
{
return YES;
}
else if (pre == '|' && c == '|')
{
return YES;
}
else
return NO;
}