-
Notifications
You must be signed in to change notification settings - Fork 0
/
错误处理.cpp
2882 lines (2819 loc) · 69.9 KB
/
错误处理.cpp
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
void delete_name_list(int depth);
void add_name_list(char* n, int deep, int type);
int checknow_name_list(char* n, int deep);
int search_name_list(char* n, int type);
void print_code();
void print_code_pre1();
void print_code_pre2();
void print_error(struct error_info error);
void deal_token(char c);
void getsym();
int find_return_func(char* name);
int find_no_return_func(char* name);
int judge_string();
int judge_program();
int judge_const_inform();
int judge_const_define();
int judge_unsigned_integer();
int judge_integer();
int judge_head();
int judge_const();
int judge_var_inform();
int judge_var_define();
int judge_function_has_return();
int judge_function_no_return();
int judge_mix_statement();
int judge_parameter_list(int type);
int judge_main_function();
int judge_expression();
int judge_item();
int judge_factor();
int judge_statement();
int judge_value_statement();
int judge_condition_statement();
int judge_condition();
int judge_circulation_statement();
int judge_stepsize();
int judge_case_statement();
int judge_case_list(int type);
int judge_case_son_statement(int type);
int judge_default();
int judge_returnfunc_use();
int judge_noreturnfunc_use();
int judge_func_call();
int judge_value_parameter_list(char* name);
int judge_statement_list();
int judge_scanf();
int judge_printf();
int judge_return_statement();
FILE* in = freopen("testfile.txt", "r", stdin);
FILE* out = freopen("error.txt", "w", stdout);
char c;
char s[1000];
char s_low[1000];
enum Code
{
IDENFR,
INTCON,
CHARCON,
STRCON,
CONSTTK,
INTTK,
CHARTK,
VOIDTK,
MAINTK,
IFTK,
ELSETK,
SWITCHTK,
CASETK,
DEFAULTTK,
WHILETK,
FORTK,
SCANFTK,
PRINTFTK,
RETURNTK,
PLUS,
MINU,
MULT,
DIV,
LSS,
LEQ,
GRE,
GEQ,
EQL,
NEQ,
COLON,
ASSIGN,
SEMICN,
COMMA,
LPARENT,
RPARENT,
LBRACK,
RBRACK,
LBRACE,
RBRACE
};
Code symbol, pre1, pre2;
char content1[1000], content2[1000];
int flag1 = 0, flag2 = 0;
void Code_map(Code symbol)
{
switch (symbol) {
case 0: printf("IDENFR"); break;
case 1: printf("INTCON"); break;
case 2: printf("CHARCON"); break;
case 3: printf("STRCON"); break;
case 4: printf("CONSTTK"); break;
case 5: printf("INTTK"); break;
case 6: printf("CHARTK"); break;
case 7: printf("VOIDTK"); break;
case 8: printf("MAINTK"); break;
case 9: printf("IFTK"); break;
case 10: printf("ELSETK"); break;
case 11: printf("SWITCHTK"); break;
case 12: printf("CASETK"); break;
case 13: printf("DEFAULTTK"); break;
case 14: printf("WHILETK"); break;
case 15: printf("FORTK"); break;
case 16: printf("SCANFTK"); break;
case 17: printf("PRINTFTK"); break;
case 18: printf("RETURNTK"); break;
case 19: printf("PLUS"); break;
case 20: printf("MINU"); break;
case 21: printf("MULT"); break;
case 22: printf("DIV"); break;
case 23: printf("LSS"); break;
case 24: printf("LEQ"); break;
case 25: printf("GRE"); break;
case 26: printf("GEQ"); break;
case 27: printf("EQL"); break;
case 28: printf("NEQ"); break;
case 29: printf("COLON"); break;
case 30: printf("ASSIGN"); break;
case 31: printf("SEMICN"); break;
case 32: printf("COMMA"); break;
case 33: printf("LPARENT"); break;
case 34: printf("RPARENT"); break;
case 35: printf("LBRACK"); break;
case 36: printf("RBRACK"); break;
case 37: printf("LBRACE"); break;
case 38: printf("RBRACE"); break;
}
}
int linenum = 1;//当前的行数
int mark_newline = 0; //判断前一个符号是不是换行符的标记,0不是1是
struct error_info {
int line;
char type;
};
struct name_info {
char name[1000]; //名字
int type; //类型,0:const_int; 1:const_char; 2:var_int; 3:var_char; 4:func;
int depth; //所在层次
};
struct func_info {
char name[1000]; //函数名
int type; //0:void, 1:int, 2:char,表示函数返回值的种类
int parameter_type[1000]; //0:int, 1:char,按顺序表示参数的种类
int parameter_sum; //表示参数的数量
};
struct name_info name_list[1000]; //所有名字都保存在这里,包括变量,常量,函数名
int name_sum = 0; //现在一共声明了多少个名字
int depth = 0; //这表明了某个声明所在的层次
struct func_info no_return_func[1000];
struct func_info has_return_func[1000];
int no_return_func_sum = 0, has_return_func_sum = 0;
int func_type = 0;
struct func_info search_function(char* name)
{
int i;
struct func_info tmp;
tmp.type = 3;
for (i = 0; i < has_return_func_sum; i++) {
if (strcmp(has_return_func[i].name, name) == 0) {
return has_return_func[i];
}
}
for (i = 0; i < no_return_func_sum; i++) {
if (strcmp(no_return_func[i].name, name) == 0) {
return no_return_func[i];
}
}
return tmp;
}
void delete_name_list(int deep) //把当前层次的所有名字全部删掉
{
while (name_list[name_sum - 1].depth == deep) {
name_sum--;
}
}
void add_name_list(char* n, int deep, int type)
{
strcpy(name_list[name_sum].name, n);
name_list[name_sum].depth = deep;
name_list[name_sum].type = type;
name_sum++;
}
int checknow_name_list(char* n, int deep) //检查当前层次是否有重名的
{
int i;
for (i = name_sum - 1; name_list[i].depth == deep; i--)
{
if (strcmp(name_list[i].name, n) == 0)
{
return 1;
}
}
return 0;
}
int search_name_list(char* n, int type)
{
int i;
for (i = 0; i < name_sum; i++) {
if (strcmp(name_list[i].name, n) == 0) {
if ((name_list[i].type == 4 && type == 4) || type == 0) {
return i;
}
else {
return -1;
}
}
}
return -1;
}
struct name_info find_name_list(char* name)
{
int i;
struct name_info a;
for (i = 0; i < name_sum; i++) {
if (strcmp(name_list[i].name, name) == 0) {
return name_list[i];
}
}
return a;
}
void print_code()
{
Code_map(symbol);
printf(" %s\n", s);
}
void print_code_pre1()
{
Code_map(pre1);
printf(" %s\n", content1);
}
void print_code_pre2()
{
Code_map(pre2);
printf(" %s\n", content2);
}
void print_error(struct error_info error)
{
printf("%d %c\n", error.line, error.type);
}
void deal_token(char c)
{
if (isalpha(c) || c == '_')
{
int i = 0;
s[i] = c;
if (c >= 'A' && c <= 'Z') {
c = c - 'A' + 'a';
}
s_low[i++] = c;
while ((c = getchar())) {
if (!isalnum(c) && c != '_') {
fseek(in, -1, SEEK_CUR);
break;
}
s[i] = c;
if (c >= 'A' && c <= 'Z') {
c = c - 'A' + 'a';
}
s_low[i++] = c;
}
s[i] = '\0';
s_low[i] = '\0';
if (strcmp(s_low, "const") == 0) {
symbol = CONSTTK;
}
else if (strcmp(s_low, "int") == 0) {
symbol = INTTK;
}
else if (strcmp(s_low, "char") == 0) {
symbol = CHARTK;
}
else if (strcmp(s_low, "void") == 0) {
symbol = VOIDTK;
}
else if (strcmp(s_low, "main") == 0) {
symbol = MAINTK;
}
else if (strcmp(s_low, "if") == 0) {
symbol = IFTK;
}
else if (strcmp(s_low, "else") == 0) {
symbol = ELSETK;
}
else if (strcmp(s_low, "switch") == 0) {
symbol = SWITCHTK;
}
else if (strcmp(s_low, "case") == 0) {
symbol = CASETK;
}
else if (strcmp(s_low, "default") == 0) {
symbol = DEFAULTTK;
}
else if (strcmp(s_low, "while") == 0) {
symbol = WHILETK;
}
else if (strcmp(s_low, "for") == 0) {
symbol = FORTK;
}
else if (strcmp(s_low, "scanf") == 0) {
symbol = SCANFTK;
}
else if (strcmp(s_low, "printf") == 0) {
symbol = PRINTFTK;
}
else if (strcmp(s_low, "return") == 0) {
symbol = RETURNTK;
}
else {
symbol = IDENFR;
}
}
else if (isdigit(c)) {
int i = 0;
s[i++] = c;
while ((c = getchar())) {
if (!isdigit(c)) {
fseek(in, -1, SEEK_CUR);
break;
}
s[i++] = c;
}
s[i] = '\0';
symbol = INTCON;
}
else if (c == '\'')
{
int i = 0;
c = getchar();
s[i++] = c;
s[i] = '\0';
c = getchar();
symbol = CHARCON;
}
else if (c == '"')
{
int i = 0;
while ((c = getchar())) {
if (c == '"') {
break;
}
s[i++] = c;
}
s[i] = '\0';
symbol = STRCON;
}
else if (c == '+') {
symbol = PLUS;
strcpy(s, "+");
}
else if (c == '-') {
symbol = MINU;
strcpy(s, "-");
}
else if (c == '*') {
symbol = MULT;
strcpy(s, "*");
}
else if (c == '/') {
symbol = DIV;
strcpy(s, "/");
}
else if (c == '<') {
c = getchar();
if (c == '=') {
symbol = LEQ;
strcpy(s, "<=");
}
else {
fseek(in, -1, SEEK_CUR);
symbol = LSS;
strcpy(s, "<");
}
}
else if (c == '>') {
c = getchar();
if (c == '=') {
symbol = GEQ;
strcpy(s, ">=");
}
else {
fseek(in, -1, SEEK_CUR);
symbol = GRE;
strcpy(s, ">");
}
}
else if (c == '=') {
c = getchar();
if (c == '=') {
symbol = EQL;
strcpy(s, "==");
}
else {
fseek(in, -1, SEEK_CUR);
symbol = ASSIGN;
strcpy(s, "=");
}
}
else if (c == '!') {
c = getchar();
if (c == '=') {
symbol = NEQ;
strcpy(s, "!=");
}
}
else if (c == ':') {
symbol = COLON;
strcpy(s, ":");
}
else if (c == ';') {
symbol = SEMICN;
strcpy(s, ";");
}
else if (c == ',') {
symbol = COMMA;
strcpy(s, ",");
}
else if (c == '(') {
symbol = LPARENT;
strcpy(s, "(");
}
else if (c == ')') {
symbol = RPARENT;
strcpy(s, ")");
}
else if (c == '[') {
symbol = LBRACK;
strcpy(s, "[");
}
else if (c == ']') {
symbol = RBRACK;
strcpy(s, "]");
}
else if (c == '{') {
symbol = LBRACE;
strcpy(s, "{");
}
else if (c == '}') {
symbol = RBRACE;
strcpy(s, "}");
}
}
void getsym() {
mark_newline = 0;
c = getchar();
while (isspace(c)) {
//TODO
if (c == '\n')
{
linenum++;
mark_newline = 1;
}
c = getchar();
}
if (c == EOF) {
return;
}
deal_token(c);
}
int find_return_func(char* name)
{
int i;
for (i = 0; i < has_return_func_sum; i++)
{
if (strcmp(has_return_func[i].name, name) == 0)
return 1;
}
return 0;
}
int find_no_return_func(char* name)
{
int i;
for (i = 0; i < no_return_func_sum; i++)
{
if (strcmp(no_return_func[i].name, name) == 0)
return 1;
}
return 0;
}
int judge_string()
{
if (symbol != STRCON)
return -1;
//print_code();
int i, biaoji = 0;
for (i = 0; i < strlen(s); i++)
{
if (s[i] != 32 && s[i] != 33 && !(s[i] >= 35 && s[i] <= 126))
{
biaoji = 1;
break;
}
}
if (biaoji == 1 || strlen(s) == 0)
{
struct error_info error;
error.line = linenum;
error.type = 'a';
print_error(error);
}
getsym();
//printf("<字符串>\n");
return 0;
}
int judge_program()
{
int r;
r = judge_const_inform();
if (r < 0)
return r;
r = judge_var_inform();
if (r < 0)
return r;
while (1)
{
if (judge_function_has_return() < 0)
{
if (judge_function_no_return() < 0)
{
break;
}
}
}
r = judge_main_function();
if (r < 0)
return r;
//printf("<程序>\n");
return 0;
}
int judge_const_inform()
{
int r;
if (symbol != CONSTTK) {
return 0; //这里不应该返回负数,否则就直接退了
}
while (symbol == CONSTTK)
{
//print_code();
getsym();
r = judge_const_define();
if (r < 0)
return r;
//print_code(); //输出;
if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号
{
struct error_info error;
if (mark_newline)
{
error.line = linenum - 1;
mark_newline = 0;
}
else
error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1
error.type = 'k';
print_error(error);
}
else
{
getsym();
} //如果没错,那么还要继续往下读一个
}
//printf("<常量说明>\n");
return 0;
}
int judge_const_define()
{
int r;
if (symbol == INTTK)
{
//print_code();
getsym(); //读标识符
if (checknow_name_list(s_low, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(s_low, depth, 0);
}
//print_code();
getsym(); //读等号
//print_code();
getsym(); //读整数
r = judge_integer();
if (r < 0)
return r;
while (symbol == COMMA) //不出意外的话,逗号在前面读了
{
//print_code();
getsym(); //读标识符
if (checknow_name_list(s_low, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(s_low, depth, 0);
}
//print_code();
getsym(); //读等号
//print_code();
getsym(); //读整数
r = judge_integer();
if (r < 0)
return r;
}
}
else if (symbol == CHARTK) //???可否有else
{
//print_code();
getsym(); //读标识符
if (checknow_name_list(s_low, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(s_low, depth, 1);
}
//print_code();
getsym(); //读等号
//print_code();
getsym(); //读字符
if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a')
&& !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9')
&& s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/')
{
struct error_info error;
error.line = linenum;
error.type = 'a';
print_error(error);
}
//print_code();
getsym(); //读逗号,因为并没有处理逗号的函数
while (symbol == COMMA) //不出意外的话,逗号在前面读了
{
//print_code();
getsym(); //读标识符
if (checknow_name_list(s_low, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(s_low, depth, 1);
}
//print_code();
getsym(); //读等号
//print_code();
getsym(); //读字符
if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a')
&& !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9')
&& s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/')
{
struct error_info error;
error.line = linenum;
error.type = 'a';
print_error(error);
}
//print_code();
getsym(); //读逗号,因为并没有处理逗号的函数,若不是逗号,那么读sym并跳出
}
}
//printf("<常量定义>\n");
return 0;
}
int judge_unsigned_integer()
{
if (symbol != INTCON) {
return -1;
}
//print_code(); //输出数
int tmp = atoi(s);
getsym(); //读下一个
//printf("<无符号整数>\n");
return tmp;
}
int judge_integer()
{
int r;
if (symbol == PLUS || symbol == MINU) //???是否需要判断是不是char常量
{
//print_code(); //输出符号
getsym(); //读数
}
r = judge_unsigned_integer();
if (r < 0)
return r;
//printf("<整数>\n");
return 0;
}
int judge_head()
{
int type;
if ((flag1 == 1 && pre1 != INTTK && pre1 != CHARTK)
|| (flag1 == 0 && symbol != INTTK && symbol != CHARTK)) {
return -1;
}
if (flag1 == 1) {
flag1 = 0;
//print_code_pre1();
if (pre1 == INTTK)
{
type = 1;
has_return_func[has_return_func_sum].type = 1;
}
else if (pre1 == CHARTK)
{
type = 2;
has_return_func[has_return_func_sum].type = 2;
}
}
else {
//print_code();
if (symbol == INTTK)
{
type = 1;
has_return_func[has_return_func_sum].type = 1;
}
else if (symbol == CHARTK)
{
type = 2;
has_return_func[has_return_func_sum].type = 2;
}
getsym();
}
if (flag2 == 1) {
flag2 = 0;
if (checknow_name_list(content2, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(content2, depth, 4);
}
strcpy(has_return_func[has_return_func_sum].name, content2);
//print_code_pre2(); //输出标识符
}
else {
if (checknow_name_list(s_low, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(s_low, depth, 4);
}
strcpy(has_return_func[has_return_func_sum].name, s_low);
//print_code(); //输出标识符
getsym();
}
//printf("<声明头部>\n");
return type;
}
int judge_const()
{
if (judge_integer() == 0)
{
//printf("<常量>\n");
return 0;
}
else if (s[0] == '+' || s[0] == '-' || s[0] == '*' || s[0] == '/' || (s[0] >= '0' && s[0] <= '9')
|| (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= 'A' && s[0] <= 'Z') || s[0] == '_')
{
//print_code(); //输出这个字符
getsym();
//printf("<常量>\n");
return 1;
}
else if (s[0] != '_' && !(s[0] <= 'z' && s[0] >= 'a')
&& !(s[0] <= 'Z' && s[0] >= 'A') && !(s[0] >= '0' && s[0] <= '9')
&& s[0] != '+' && s[0] != '-' && s[0] != '*' && s[0] != '/')
{
struct error_info error;
error.line = linenum;
error.type = 'a';
print_error(error);
getsym(); //这里应该是要读一个,因为不存在缺少,只有错误的情况
return 1; //不应返回负数
}
else
return -1;
}
int judge_var_inform()
{
int r;
int sum = 0;
while (1)
{
if (symbol != INTTK && symbol != CHARTK)
{
break;
}
pre1 = symbol;
strcpy(content1, s_low); //注意这里和下面做出了改动,因为不需要输出内容,为了方便判断,将其改为小写存储
flag1 = 1;
getsym();
pre2 = symbol;
strcpy(content2, s_low);
flag2 = 1;
getsym(); //注意,下面接的有可能是无返回值函数定义,也有可能是有返回值函数定义,也有可能是个变量,也有可能是主函数
if (symbol != LBRACK && symbol != COMMA && symbol != SEMICN && symbol != ASSIGN)
{
break;
}
r = judge_var_define();
if (r < 0)
return r;
sum++;
//print_code();
if (strcmp(s, ";") != 0)//到了句子末尾,关注是否为分号
{
struct error_info error;
if (mark_newline)
{
error.line = linenum - 1;
mark_newline = 0;
}
else
error.line = linenum;//同时判断前面的符号是否为换行符,如果是,则错误在前一行,因为在读的时候已经加了1
error.type = 'k';
print_error(error);
}
else
{
getsym();
} //如果没错,那么还要继续往下读一个
}
if (sum == 0)
return 0; //如果为0,说明没有变量说明
//printf("<变量说明>\n");
return 0;
}
int judge_var_define()
{
int r;
int flag = 0;
int type;
int a1 = 0, a2 = 0;
int sum1 = 0, sum2 = 0;
//print_code_pre1(); //输出类型标识符
if (pre1 == INTTK)
{
type = 2;
}
else if (pre1 == CHARTK)
{
type = 3;
}
flag1 = 0;
//print_code_pre2(); //输出标识符
if (checknow_name_list(content2, depth))
{
struct error_info error;
error.line = linenum;
error.type = 'b';
print_error(error);
}
else
{
add_name_list(content2, depth, type);
}
flag2 = 0;
if (symbol == LBRACK)
{
sum1 = 0;
//print_code(); //输出[
getsym();
r = judge_unsigned_integer();
if (r < 0)
return r;
a1 = r; //这是第一个中括号里面的数
//print_code(); //输出]
if (strcmp(s, "]") != 0)
{
struct error_info error;
error.line = linenum;
error.type = 'm';
print_error(error);
}
else
{
getsym();
}
if (symbol == LBRACK)
{
sum2 = 0;
//print_code(); //输出[
getsym();
r = judge_unsigned_integer();
if (r < 0)
return r;
a2 = r;
//print_code(); //输出]
if (strcmp(s, "]") != 0)
{
struct error_info error;
error.line = linenum;
error.type = 'm';
print_error(error);
}
else
{
getsym();
}
if (symbol == ASSIGN)
{
flag = 1;
//print_code(); //输出=
getsym();
//print_code(); //输出{
getsym();
//print_code(); //输出{
getsym();
r = judge_const();
if (r < 0)
return r;
if ((r == 0 && type == 3) || (r == 1 && type == 2))
{
struct error_info error;
error.line = linenum;
error.type = 'o';
print_error(error);
}
sum2++;
while (symbol == COMMA)
{
//print_code(); //输出,
getsym();
r = judge_const();