-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.java.old
560 lines (503 loc) · 12.7 KB
/
Parser.java.old
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
public class Parser {
public static final int _EOF = 0;
public static final int _ident = 1;
public static final int _numeral = 2;
public static final int _floatLit = 3;
public static final int _charLit = 4;
public static final int _stringLit = 5;
public static final int _subtype = 6;
public static final int _record = 7;
public static final int _range = 8;
public static final int _is = 9;
public static final int _loop = 10;
public static final int _while = 11;
public static final int _for = 12;
public static final int _begin = 13;
public static final int _end = 14;
public static final int _procedure = 15;
public static final int _function = 16;
public static final int _package = 17;
public static final int _use = 18;
public static final int _with = 19;
public static final int _of = 20;
public static final int _array = 21;
public static final int _integer = 22;
public static final int _string = 23;
public static final int _boolean = 24;
public static final int _float = 25;
public static final int _null = 26;
public static final int _char = 27;
public static final int _colon = 28;
public static final int _semicolon = 29;
public static final int _comma = 30;
public static final int _dot = 31;
public static final int _self = 32;
public static final int _lbrace = 33;
public static final int _lbrack = 34;
public static final int _lpar = 35;
public static final int _mult = 36;
public static final int _and = 37;
public static final int _not = 38;
public static final int _minus = 39;
public static final int _plus = 40;
public static final int _rbrace = 41;
public static final int _rbrack = 42;
public static final int _rpar = 43;
public static final int _tilde = 44;
public static final int maxT = 53;
static final boolean _T = true;
static final boolean _x = false;
static final int minErrDist = 2;
public Token t; // last recognized token
public Token la; // lookahead token
int errDist = minErrDist;
public Scanner scanner;
public Errors errors;
Token peek(int n) {
scanner.ResetPeek();
Token x = la;
while (n > 0) { x = scanner.Peek(); n--; }
return x;
}
Object getKind(int t)
{
java.util.Set<java.util.Map.Entry> entries = Scanner.literals.entrySet();
for(java.util.Map.Entry entry : entries) {
if(entry.getValue().equals(t)) {
return entry.getKey();
}
}
return null;
}
class ExprKind {
static final int NONE = 0;
static final int CONDEXPR = 17;
static final int APPLY = 25;
static final int NEWCLASS = 26;
static final int NEWARRAY = 27;
static final int PARENS = 28;
static final int ASSIGN = 29;
static final int TYPECAST = 30;
static final int TYPETEST = 31;
static final int SELECT = 33;
static final int IDENT = 34;
static final int LITERAL = 35;
static final int POS = 41;
static final int NEG = 42;
static final int NOT = 43;
static final int COMPL = 44;
static final int PREINC = 45;
static final int PREDEC = 46;
static final int POSTINC = 47;
static final int POSTDEC = 48;
static final int BINARY = 50;
}
public Parser(Scanner scanner) {
this.scanner = scanner;
errors = new Errors();
}
void SynErr (int n) {
if (errDist >= minErrDist) errors.SynErr(la.line, la.col, n);
errDist = 0;
}
public void SemErr (String msg) {
if (errDist >= minErrDist) errors.SemErr(t.line, t.col, msg);
errDist = 0;
}
void Get () {
for (;;) {
t = la;
la = scanner.Scan();
if (la.kind <= maxT) {
++errDist;
break;
}
la = t;
}
}
void Expect (int n) {
if (la.kind==n) Get(); else { SynErr(n); }
}
boolean StartOf (int s) {
return set[s][la.kind];
}
void ExpectWeak (int n, int follow) {
if (la.kind == n) Get();
else {
SynErr(n);
while (!StartOf(follow)) Get();
}
}
boolean WeakSeparator (int n, int syFol, int repFol) {
int kind = la.kind;
if (kind == n) { Get(); return true; }
else if (StartOf(repFol)) return false;
else {
SynErr(n);
while (!(set[syFol][kind] || set[repFol][kind] || set[0][kind])) {
Get();
kind = la.kind;
}
return StartOf(syFol);
}
}
void ada() {
System.out.println("ADASTART");
pdecl();
System.out.println("Declarations:");
while (la.kind == 1) {
decl();
Expect(29);
System.out.print(t.val + "\n");
}
Expect(13);
System.out.print("\t" + t.val + "\n");
System.out.println("Statements:");
while (la.kind == 1 || la.kind == 26) {
stmt();
Expect(29);
System.out.print(t.val + "\n");
}
pend();
Expect(29);
System.out.print(t.val + "\n");
System.out.println("ADASTOP");
}
void pdecl() {
Expect(15);
System.out.print("\t" + t.val + " ");
Expect(1);
System.out.print(t.val + " ");
Expect(9);
System.out.print(t.val + "\n");
}
void decl() {
Expect(1);
System.out.print("\t" + t.val);
while (la.kind == 30) {
Get();
System.out.print(t.val + " ");
Expect(1);
System.out.print(t.val);
}
Expect(28);
System.out.print(t.val + " ");
type();
System.out.print(t.val);
if (la.kind == 8) {
Get();
System.out.print(" " + t.val + " ");
range_decl();
}
if (la.kind == 45) {
Get();
System.out.print(" " + t.val + " ");
Expr();
}
}
void stmt() {
if (la.kind == 26) {
Get();
} else if (la.kind == 1) {
assign();
} else SynErr(54);
}
void pend() {
Expect(14);
System.out.print("\t" + t.val + " ");
Expect(1);
System.out.print(t.val);
}
void type() {
switch (la.kind) {
case 22: {
Get();
break;
}
case 23: {
Get();
break;
}
case 24: {
Get();
break;
}
case 25: {
Get();
break;
}
case 27: {
Get();
break;
}
case 21: {
array_type_def();
break;
}
default: SynErr(55); break;
}
}
void range_decl() {
Expr();
Expect(47);
System.out.print(" " + t.val + " ");
Expr();
}
void Expr() {
Term();
while (la.kind == 39 || la.kind == 40) {
AddOp();
Term();
}
}
void array_type_def() {
Expect(21);
System.out.print(t.val);
Expect(35);
System.out.print(" " + t.val + " ");
range_decl();
Expect(43);
System.out.print(" " + t.val + " ");
Expect(20);
System.out.print(t.val + " ");
type();
}
void assign() {
Expect(1);
System.out.print("\t" + t.val);
if (la.kind == 35) {
Get();
System.out.print(" " + t.val + " ");
Expr();
Expect(43);
System.out.print(" " + t.val + " ");
}
Expect(45);
System.out.print(" " + t.val + " ");
Expr();
}
void prefix() {
name();
}
void name() {
Expect(1);
}
void range_attribute_reference() {
prefix();
System.out.print(" " + t.val + " ");
Expect(46);
System.out.print(" " + t.val + " ");
range_attribute_designator();
}
void range_attribute_designator() {
Expect(8);
if (la.kind == 35) {
Get();
System.out.print(" " + t.val + " ");
Expr();
Expect(43);
}
System.out.print(" " + t.val + " ");
}
void Primary() {
if (la.kind == 2) {
numeric_literal();
} else if (la.kind == 26) {
Get();
} else if (la.kind == 1) {
name();
} else if (la.kind == 35) {
Get();
Expr();
Expect(43);
} else SynErr(56);
}
void numeric_literal() {
Expect(2);
if (la.kind == 31 || la.kind == 52) {
if (la.kind == 31) {
decimal_literal();
} else {
based_literal();
}
}
if (la.kind == 51) {
exponent();
}
}
void Term() {
Factor();
System.out.print(t.val);
while (la.kind == 36) {
MulOp();
Factor();
}
}
void AddOp() {
if (la.kind == 40) {
Get();
} else if (la.kind == 39) {
Get();
} else SynErr(57);
System.out.print(" " + t.val + " ");
}
void Factor() {
if (StartOf(1)) {
} else if (StartOf(2)) {
Primary();
if (la.kind == 48) {
Get();
Primary();
}
} else if (la.kind == 49) {
Get();
} else if (la.kind == 50) {
Get();
} else SynErr(58);
}
void MulOp() {
Expect(36);
System.out.println(getKind(t.kind) + " = " + t.val);
}
void decimal_literal() {
Expect(31);
Expect(2);
}
void based_literal() {
Expect(52);
Expect(2);
if (la.kind == 31) {
Get();
Expect(2);
}
Expect(52);
}
void exponent() {
if (la.kind == 51) {
Get();
if (la.kind == 40) {
Get();
}
Expect(2);
} else if (la.kind == 51) {
Get();
if (la.kind == 39) {
Get();
}
Expect(2);
} else SynErr(59);
}
public void Parse() {
la = new Token();
la.val = "";
Get();
ada();
Expect(0);
}
private static final boolean[][] set = {
{_T,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x},
{_x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_T,_x,_x, _x,_x,_x,_x, _T,_x,_x,_T, _T,_x,_x,_T, _x,_T,_x,_T, _x,_x,_x,_x, _x,_x,_x},
{_x,_T,_T,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_T,_x, _x,_x,_x,_x, _x,_x,_x,_T, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x,_x, _x,_x,_x}
};
} // end Parser
class Errors {
public int count = 0; // number of errors detected
public java.io.PrintStream errorStream = System.out; // error messages go to this stream
public String errMsgFormat = "-- line {0} col {1}: {2}"; // 0=line, 1=column, 2=text
protected void printMsg(int line, int column, String msg) {
StringBuffer b = new StringBuffer(errMsgFormat);
int pos = b.indexOf("{0}");
if (pos >= 0) { b.delete(pos, pos+3); b.insert(pos, line); }
pos = b.indexOf("{1}");
if (pos >= 0) { b.delete(pos, pos+3); b.insert(pos, column); }
pos = b.indexOf("{2}");
if (pos >= 0) b.replace(pos, pos+3, msg);
errorStream.println(b.toString());
}
public void SynErr (int line, int col, int n) {
String s;
switch (n) {
case 0: s = "EOF expected"; break;
case 1: s = "ident expected"; break;
case 2: s = "numeral expected"; break;
case 3: s = "floatLit expected"; break;
case 4: s = "charLit expected"; break;
case 5: s = "stringLit expected"; break;
case 6: s = "subtype expected"; break;
case 7: s = "record expected"; break;
case 8: s = "range expected"; break;
case 9: s = "is expected"; break;
case 10: s = "loop expected"; break;
case 11: s = "while expected"; break;
case 12: s = "for expected"; break;
case 13: s = "begin expected"; break;
case 14: s = "end expected"; break;
case 15: s = "procedure expected"; break;
case 16: s = "function expected"; break;
case 17: s = "package expected"; break;
case 18: s = "use expected"; break;
case 19: s = "with expected"; break;
case 20: s = "of expected"; break;
case 21: s = "array expected"; break;
case 22: s = "integer expected"; break;
case 23: s = "string expected"; break;
case 24: s = "boolean expected"; break;
case 25: s = "float expected"; break;
case 26: s = "null expected"; break;
case 27: s = "char expected"; break;
case 28: s = "colon expected"; break;
case 29: s = "semicolon expected"; break;
case 30: s = "comma expected"; break;
case 31: s = "dot expected"; break;
case 32: s = "self expected"; break;
case 33: s = "lbrace expected"; break;
case 34: s = "lbrack expected"; break;
case 35: s = "lpar expected"; break;
case 36: s = "mult expected"; break;
case 37: s = "and expected"; break;
case 38: s = "not expected"; break;
case 39: s = "minus expected"; break;
case 40: s = "plus expected"; break;
case 41: s = "rbrace expected"; break;
case 42: s = "rbrack expected"; break;
case 43: s = "rpar expected"; break;
case 44: s = "tilde expected"; break;
case 45: s = "\":=\" expected"; break;
case 46: s = "\"\'\" expected"; break;
case 47: s = "\"..\" expected"; break;
case 48: s = "\"**\" expected"; break;
case 49: s = "\"true\" expected"; break;
case 50: s = "\"false\" expected"; break;
case 51: s = "\"e\" expected"; break;
case 52: s = "\"#\" expected"; break;
case 53: s = "??? expected"; break;
case 54: s = "invalid stmt"; break;
case 55: s = "invalid type"; break;
case 56: s = "invalid Primary"; break;
case 57: s = "invalid AddOp"; break;
case 58: s = "invalid Factor"; break;
case 59: s = "invalid exponent"; break;
default: s = "error " + n; break;
}
printMsg(line, col, s);
count++;
}
public void SemErr (int line, int col, String s) {
printMsg(line, col, s);
count++;
}
public void SemErr (String s) {
errorStream.println(s);
count++;
}
public void Warning (int line, int col, String s) {
printMsg(line, col, s);
}
public void Warning (String s) {
errorStream.println(s);
}
} // Errors
class FatalError extends RuntimeException {
public static final long serialVersionUID = 1L;
public FatalError(String s) { super(s); }
}