-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdcheck.java
854 lines (735 loc) · 22.3 KB
/
rdcheck.java
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
import java.io.*;
import java.util.ArrayList;
import java.util.Stack;
public class rdcheck {
public static final String ID = "Ident";
public static final String CONST = "const";
public static final String INT = "int";
public static final String INT_CONST = "IntConst";
public static final String VOID = "void";
public static final String IF = "if";
public static final String IF_BODY = "if_body";
public static final String ELSE = "else";
public static final String ELSE_BODY = "else_body";
public static final String WHILE = "while";
public static final String WHILE_BODY = "while_body";
public static final String BREAK = "break";
public static final String CONTINUE = "continue";
public static final String RETURN = "return";
public static final String MAIN = "main";
public static class Bean {
public String type;
public String value;
public ArrayList<Bean> beans;
public Bean() {
beans = new ArrayList<>();
}
public Bean(String type) {
this();
this.type = type;
}
public Bean(String type, String value) {
this();
this.type = type;
this.value = value;
}
public void add(Bean bean) {
if (bean != null)
beans.add(bean);
}
@Override
public String toString() {
if (value == null || value.trim().length() == 0)
return type;
return type + "(" + value + ")";
}
}
public static BufferedReader reader;
public static int index = 0;
public static int length = 0;
public static int max_sign = 0;
public static ArrayList<String> type;
public static ArrayList<String> value;
public static Stack<Integer> sign;
public static ArrayList<StackTraceElement> elements = new ArrayList<>();
public static void mark() {
sign.push(index);
}
public static Bean back() {
index = sign.pop();
//跟踪识别最后位置
if (index > max_sign) {
max_sign = index;
elements = new ArrayList<>();
}
if (index >= max_sign) {
elements.add(Thread.currentThread().getStackTrace()[2]);
}
// max_sign = Math.max(index, max_sign);
return null;
}
public static void reset() {
index = sign.peek();
}
public static boolean matchType(String type) {
boolean match = index < rdcheck.type.size() && rdcheck.type.get(index).equals(type);
//仅在匹配时增加(避免内部错失)
//消费后才下一个
if (match) index++;
return match;
}
public static boolean matchValue(String value) {
boolean match = index < rdcheck.value.size() && rdcheck.value.get(index).equals(value);
if (match) index++;
return match;
}
public static Bean toBean(int i) {
if (i >= 0 && i < length) {
return new Bean(type.get(i), value.get(i));
}
return null;
}
public static void main(String[] args) {
//读取词法分析后的文件
if (args.length >= 1) {
File in = new File(args[0]);
try {
reader = new BufferedReader(new FileReader(in));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.exit(1);
}
}
//从lex分析后的文件中获取整个表
type = new ArrayList<>();
value = new ArrayList<>();
sign = new Stack<>();
do {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (line == null) break;
if (line.trim().length() == 0) continue;
String[] str = line.split(" ");
type.add(str[0]);
value.add(str[1]);
} while (true);
length = Math.min(type.size(), value.size());
//开始检查
while (index < length) {
Bean compUnit = CompUnit();
if (compUnit == null) {
System.out.println("语法出错,最大位置为:line" + max_sign);
System.out.println("对应内容:" + type.get(max_sign) + " " + value.get(max_sign));
for (StackTraceElement element : elements) {
System.out.println(element);
}
System.exit(0);
// index = max_sign + 1;
// continue;
}
}
System.out.println("检查结束");
}
public static Bean CompUnit() {
Bean compUnit = new Bean("CompUnit");
mark();
Bean decl = Decl();
if (decl != null) {
compUnit.add(decl);
Bean compUnit1 = CompUnit();
if (compUnit1 != null)
compUnit.add(compUnit1);
sign.pop();
return compUnit;
}
Bean funcDef = FuncDef();
if (funcDef != null) {
compUnit.add(funcDef);
Bean compUnit1 = CompUnit();
if (compUnit1 != null)
compUnit.add(compUnit1);
sign.pop();
return compUnit;
}
return back();
}
public static Bean Decl() {
Bean decl = new Bean("Decl");
mark();
Bean constDecl = ConstDecl();
if (constDecl != null) {
decl.add(constDecl);
sign.pop();
return decl;
}
Bean varDecl = VarDecl();
if (varDecl != null) {
decl.add(varDecl);
sign.pop();
return decl;
}
return back();
}
public static Bean ConstDecl() {
Bean constDecl = new Bean("ConstDecl");
mark();
if (!matchValue(CONST)) return back();
Bean bType = BType();
if (bType == null) return back();
constDecl.add(bType);
Bean constDef = ConstDef();
if (constDef == null) return back();
constDecl.add(constDef);
while (matchValue(",")) {
constDef = ConstDef();
if (constDef == null) return back();
constDecl.add(constDef);
}
if (!matchValue(";")) return back();
sign.pop();
return constDecl;
}
public static Bean BType() {
Bean bType = new Bean("BType");
mark();
if (!matchValue(INT)) return back();
bType.value = INT;
sign.pop();
return bType;
}
public static Bean ConstDef() {
Bean constDef = new Bean("ConstDef");
mark();
if (!matchType(ID)) return back();
constDef.add(toBean(index - 1));
while (matchValue("[")) {
Bean constExp = ConstExp();
if (constExp == null) return back();
constDef.add(constExp);
if (!matchValue("]")) return back();
}
if (!matchValue("=")) return back();
Bean constInitVal = ConstInitVal();
if (constInitVal == null) return back();
constDef.add(constInitVal);
sign.pop();
return constDef;
}
public static Bean ConstInitVal() {
Bean constInitVal = new Bean("ConstInitVal");
mark();
Bean constExp = ConstExp();
if (constExp != null) {
constInitVal.add(constExp);
sign.pop();
return constInitVal;
}
//force judge
if (!matchValue("{")) return back();
Bean constInitVal1 = ConstInitVal();
//Optional
if (constInitVal1 != null) {
constInitVal.add(constInitVal1);
while (matchValue(",")) {
constInitVal1 = ConstInitVal();
if (constInitVal1 == null) return back();
constInitVal.add(constInitVal1);
}
}
if (!matchValue("}")) return back();
sign.pop();
return constInitVal;
}
public static Bean VarDecl() {
Bean varDecl = new Bean("VarDecl");
mark();
Bean bType = BType();
if (bType == null) return back();
varDecl.add(bType);
Bean varDef = VarDef();
if (varDef == null) return back();
varDecl.add(varDef);
while (matchValue(",")) {
Bean varDef1 = VarDef();
if (varDef1 == null) return back();
varDecl.add(varDef1);
}
if (!matchValue(";")) return back();
sign.pop();
return varDecl;
}
public static Bean VarDef() {
Bean varDef = new Bean("VarDef");
mark();
if (!matchType(ID)) return back();
varDef.add(toBean(index - 1));
while (matchValue("[")) {
Bean constExp = ConstExp();
if (constExp == null) return back();
varDef.add(constExp);
if (!matchValue("]")) return back();
}
//Optional
if (matchValue("=")) {
varDef.add(toBean(index - 1));
Bean initVal = InitVal();
if (initVal == null) return back();
varDef.add(initVal);
}
sign.pop();
return varDef;
}
public static Bean InitVal() {
Bean initVal = new Bean("InitVal");
mark();
Bean exp = Exp();
if (exp != null) {
initVal.add(exp);
sign.pop();
return initVal;
}
if (!matchValue("{")) return back();
Bean initVal1 = InitVal();
//Optional
if (initVal1 != null) {
initVal.add(initVal1);
while (matchValue(",")) {
initVal1 = InitVal();
if (initVal1 == null) return back();
initVal.add(initVal1);
}
}
if (!matchValue("}")) return back();
sign.pop();
return initVal;
}
public static Bean FuncDef() {
Bean funcDef = new Bean("FuncDef");
mark();
Bean funcType = FuncType();
if (funcType == null) return back();
funcDef.add(funcType);
if (!matchType(ID) && !matchValue(MAIN)) return back();
funcDef.add(toBean(index - 1));
if (!matchValue("(")) return back();
//Optional
Bean funcFParams = FuncFParams();
if (funcFParams != null)
funcDef.add(funcFParams);
if (!matchValue(")")) return back();
Bean block = Block();
if (block == null) return back();
funcDef.add(block);
sign.pop();
return funcDef;
}
public static Bean FuncType() {
Bean funcType = new Bean("FuncType");
mark();
if (matchValue(VOID)) {
funcType.value = VOID;
sign.pop();
return funcType;
}
if (matchValue(INT)) {
funcType.value = INT;
sign.pop();
return funcType;
}
return back();
}
public static Bean FuncFParams() {
Bean funcFParams = new Bean("FuncFParams");
mark();
Bean funcFParam = FuncFParam();
if (funcFParam == null) return back();
funcFParams.add(funcFParam);
while (matchValue(",")) {
funcFParam = FuncFParam();
if (funcFParam == null) return back();
funcFParams.add(funcFParam);
}
sign.pop();
return funcFParams;
}
public static Bean FuncFParam() {
Bean funcFParam = new Bean("FuncFParam");
mark();
Bean bType = BType();
if (bType == null) return back();
funcFParam.add(bType);
if (!matchType(ID)) return back();
funcFParam.add(toBean(index - 1));
if (matchValue("[") && matchValue("]"))
while (matchValue("[")) {
Bean exp = Exp();
if (exp == null) return back();
funcFParam.add(exp);
if (!matchValue("]")) return back();
}
sign.pop();
return funcFParam;
}
public static Bean Block() {
Bean block = new Bean("Block");
mark();
if (!matchValue("{")) return back();
Bean blockItem;
while ((blockItem = BlockItem()) != null)
block.add(blockItem);
if (!matchValue("}")) return back();
sign.pop();
return block;
}
public static Bean BlockItem() {
Bean blockItem = new Bean("BlockItem");
mark();
Bean decl = Decl();
if (decl != null) {
blockItem.add(decl);
sign.pop();
return blockItem;
}
Bean stmt = Stmt();
if (stmt != null) {
blockItem.add(stmt);
sign.pop();
return blockItem;
}
return back();
}
public static Bean Stmt() {
Bean stmt = new Bean("Stmt");
mark();
if (matchValue(";")) return stmt;
Bean lVal = LVal();
if (lVal != null) {
stmt.add(lVal);
if (matchValue("=")) {
stmt.add(toBean(index - 1));
Bean exp = Exp();
if (exp != null) {
stmt.add(exp);
if (matchValue(";")) {
sign.pop();
return stmt;
}
}
}
}
reset();
stmt.beans.clear();
Bean exp = Exp();
//Optional
if (exp != null) {
stmt.add(exp);
if (matchValue(";")) {
sign.pop();
return stmt;
}
}
reset();
stmt.beans.clear();
Bean block = Block();
if (block != null) {
stmt.add(block);
sign.pop();
return stmt;
}
reset();
stmt.beans.clear();
if (matchValue(IF)) {
stmt.value = IF;
if (!matchValue("(")) return back();
Bean cond = Cond();
if (cond == null) return back();
stmt.add(cond);
if (!matchValue(")")) return back();
Bean stmt1 = Stmt();
if (stmt1 == null) return back();
stmt1.value = IF_BODY;
stmt.add(stmt1);
if (matchValue(ELSE)) {
Bean stmt2 = Stmt();
if (stmt2 == null) return back();
stmt2.value = ELSE_BODY;
stmt.add(stmt2);
}
sign.pop();
return stmt;
}
if (matchValue(WHILE)) {
stmt.value = WHILE;
if (!matchValue("(")) return back();
Bean cond = Cond();
if (cond == null) return back();
stmt.add(cond);
if (!matchValue(")")) return back();
Bean stmt1 = Stmt();
if (stmt1 == null) return back();
stmt1.value = WHILE_BODY;
stmt.add(stmt1);
sign.pop();
return stmt;
}
if (matchValue(BREAK)) {
stmt.value = BREAK;
if (!matchValue(";")) return back();
sign.pop();
return stmt;
}
if (matchValue(CONTINUE)) {
stmt.value = CONTINUE;
if (!matchValue(";")) return back();
sign.pop();
return stmt;
}
if (matchValue(RETURN)) {
stmt.value = RETURN;
Bean exp1 = Exp();
if (exp1 != null)
stmt.add(exp1);
if (!matchValue(";")) return back();
sign.pop();
return stmt;
}
return back();
}
public static Bean Exp() {
Bean exp = new Bean("Exp");
mark();
Bean addExp = AddExp();
if (addExp == null) return back();
exp.add(addExp);
sign.pop();
return exp;
}
public static Bean Cond() {
Bean cond = new Bean("Cond");
mark();
Bean lOrExp = LOrExp();
if (lOrExp == null) return back();
cond.add(lOrExp);
sign.pop();
return cond;
}
public static Bean LVal() {
Bean lVal = new Bean("LVal");
mark();
if (!matchType(ID)) return back();
lVal.add(toBean(index - 1));
while (matchValue("[")) {
Bean exp = Exp();
if (exp == null) back();
lVal.add(exp);
if (!matchValue("]")) return back();
}
sign.pop();
return lVal;
}
public static Bean PrimaryExp() {
Bean primaryExp = new Bean("PrimaryExp");
mark();
if (matchValue("(")) {
Bean exp = Exp();
if (exp == null) return back();
primaryExp.add(exp);
if (!matchValue(")")) return back();
sign.pop();
return primaryExp;
}
Bean lVal = LVal();
if (lVal != null) {
primaryExp.add(lVal);
sign.pop();
return primaryExp;
}
Bean number = Number();
if (number != null) {
primaryExp.add(number);
sign.pop();
return primaryExp;
}
return back();
}
public static Bean Number() {
Bean number = new Bean("Number");
mark();
if (!matchType(INT_CONST)) return back();
number.add(toBean(index - 1));
sign.pop();
return number;
}
public static Bean UnaryExp() {
Bean unaryExp = new Bean("UnaryExp");
mark();
if (matchType(ID)) {
unaryExp.add(toBean(index - 1));
if (matchValue("(")) {
Bean funcRParams = FuncRParams();
//Optional
if (funcRParams != null)
unaryExp.add(funcRParams);
if (matchValue(")")) {
sign.pop();
return unaryExp;
}
}
}
reset();
unaryExp.beans.clear();
Bean primaryExp = PrimaryExp();
if (primaryExp != null) {
unaryExp.add(primaryExp);
sign.pop();
return unaryExp;
}
Bean unaryOp = UnaryOp();
if (unaryOp != null) {
unaryExp.add(unaryOp);
Bean unaryExp1 = UnaryExp();
if (unaryExp1 == null) return back();
unaryExp.add(unaryExp1);
sign.pop();
return unaryExp;
}
return back();
}
public static Bean UnaryOp() {
Bean unaryOp = new Bean("UnaryOp");
mark();
if (matchValue("+")) {
unaryOp.value = "+";
sign.pop();
return unaryOp;
}
if (matchValue("-")) {
unaryOp.value = "-";
sign.pop();
return unaryOp;
}
if (matchValue("!")) {
unaryOp.value = "!";
sign.pop();
return unaryOp;
}
return back();
}
public static Bean FuncRParams() {
Bean funcRParams = new Bean("FuncRParams");
mark();
Bean exp = Exp();
if (exp == null) return back();
funcRParams.add(exp);
while (matchValue(",")) {
exp = Exp();
if (exp == null) return back();
funcRParams.add(exp);
}
sign.pop();
return funcRParams;
}
public static Bean MulExp() {
Bean mulExp = new Bean("MulExp");
mark();
Bean unaryExp = UnaryExp();
if (unaryExp == null) return back();
mulExp.add(unaryExp);
while (matchValue("*") || matchValue("/") || matchValue("%")) {
mulExp.add(toBean(index - 1));
Bean mulExp1 = MulExp();
if (mulExp1 == null) return back();
mulExp.add(mulExp1);
}
sign.pop();
return mulExp;
}
public static Bean AddExp() {
Bean addExp = new Bean("AddExp");
mark();
Bean mulExp = MulExp();
if (mulExp == null) return back();
addExp.add(mulExp);
while (matchValue("+") || matchValue("-")) {
addExp.add(toBean(index - 1));
Bean addExp1 = AddExp();
if (addExp1 == null) return back();
addExp.add(addExp1);
}
sign.pop();
return addExp;
}
public static Bean RelExp() {
Bean relExp = new Bean("RelExp");
mark();
Bean addExp = AddExp();
if (addExp == null) return back();
relExp.add(addExp);
while (matchValue("<") || matchValue(">") || matchValue("<=") || matchValue(">=")) {
relExp.add(toBean(index - 1));
Bean relExp1 = RelExp();
if (relExp1 == null) return back();
relExp.add(relExp1);
}
sign.pop();
return relExp;
}
public static Bean EqExp() {
Bean eqExp = new Bean("EqExp");
mark();
Bean relExp = RelExp();
if (relExp == null) return back();
eqExp.add(relExp);
while (matchValue("==") || matchValue("!=")) {
eqExp.add(toBean(index - 1));
Bean eqExp1 = EqExp();
if (eqExp1 == null) return back();
eqExp.add(eqExp1);
}
sign.pop();
return eqExp;
}
public static Bean LAndExp() {
Bean lAndExp = new Bean("LAndExp");
mark();
Bean eqExp = EqExp();
if (eqExp == null) return back();
lAndExp.add(eqExp);
while (matchValue("&&")) {
lAndExp.add(toBean(index - 1));
Bean lAndExp1 = LAndExp();
if (lAndExp1 == null) return back();
lAndExp.add(lAndExp1);
}
sign.pop();
return lAndExp;
}
public static Bean LOrExp() {
Bean lOrExp = new Bean("LOrExp");
mark();
Bean lAndExp = LAndExp();
if (lAndExp == null) return back();
lOrExp.add(lAndExp);
while (matchValue("||")) {
lOrExp.add(toBean(index - 1));
Bean lOrExp1 = LOrExp();
if (lOrExp1 == null) return back();
lOrExp.add(lOrExp1);
}
sign.pop();
return lOrExp;
}
public static Bean ConstExp() {
Bean constExp = new Bean("ConstExp");
mark();
Bean addExp = AddExp();
if (addExp == null) return back();
constExp.add(addExp);
sign.pop();
return constExp;
}
}