-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSLPTokeniser.java
715 lines (680 loc) · 18.3 KB
/
SLPTokeniser.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
/* Generated By:JavaCC: Do not edit this line. SLPTokeniser.java */
public class SLPTokeniser implements SLPTokeniserConstants {
public static void main(String args[]){
SLPTokeniser tokeniser;
if (args.length == 0){
System.out.println("Reading from standard input . . .");
tokeniser = new SLPTokeniser(System.in);
}
else if (args.length == 1) {
try{
tokeniser = new SLPTokeniser(new java.io.FileInputStream(args[0]));
}
catch (java.io.FileNotFoundException e){
System.err.println("File " + args[0] + " not found.");
return;
}
}
else {
System.out.println("SLP Tokeniser: Usage is one of:");
System.out.println(" java SLPTokeniser < inputfile");
System.out.println("OR");
System.out.println(" java SLPTokeniser inputfile");
return;
}
/*
* We've now initialised the tokeniser to read from the appropriate place,
* so just keep reading tokens and printing them until we hit EOF
*/
for (Token t = getNextToken(); t.kind!=EOF; t = getNextToken()) {
// Print out the actual text for the constants, identifiers etc.
if (t.kind==NUM){
System.out.print("Number");
System.out.print("("+t.image+") ");
}
else if (t.kind==ID){
System.out.print("Identifier");
System.out.print("("+t.image+") ");
}
else if (t.kind==STRING){
System.out.print("String");
System.out.print("("+t.image+") ");
}
else
System.out.print(t.image+" ");
}
}
//Grammar starts here
static final public void program() throws ParseException {
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CONST:
case VAR:
;
break;
default:
jj_la1[0] = jj_gen;
break label_1;
}
decl();
}
label_2:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case RBR:
case BOOL:
case INT:
case REAL:
case STR:
case VOID:
;
break;
default:
jj_la1[1] = jj_gen;
break label_2;
}
function();
}
main_prog();
}
static final public void decl() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case VAR:
var_decl();
break;
case CONST:
const_decl();
break;
default:
jj_la1[2] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void var_decl() throws ParseException {
jj_consume_token(VAR);
ident_list();
jj_consume_token(COLON);
type();
label_3:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case COMMA:
;
break;
default:
jj_la1[3] = jj_gen;
break label_3;
}
jj_consume_token(COMMA);
ident_list();
jj_consume_token(COLON);
type();
}
jj_consume_token(SEMIC);
}
static final public void const_decl() throws ParseException {
jj_consume_token(CONST);
jj_consume_token(ID);
jj_consume_token(COLON);
type() = expression();
label_4:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case COMMA:
;
break;
default:
jj_la1[4] = jj_gen;
break label_4;
}
jj_consume_token(COMMA);
jj_consume_token(ID);
jj_consume_token(COLON);
type() = expression();
}
jj_consume_token(SEMIC);
}
static final public void function() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case BOOL:
case INT:
case REAL:
case STR:
case VOID:
type();
jj_consume_token(ID);
jj_consume_token(LBR);
param_list();
jj_consume_token(RBR);
jj_consume_token(LCBR);
label_5:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CONST:
case VAR:
;
break;
default:
jj_la1[5] = jj_gen;
break label_5;
}
decl();
}
label_6:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SEMIC:
case LCBR:
case EXLM:
case QUESTION:
case IF:
case WHILE:
case ID:
;
break;
default:
jj_la1[6] = jj_gen;
break label_6;
}
statement();
jj_consume_token(SEMIC);
}
jj_consume_token(RETURN);
jj_consume_token(LBR);
expression();
break;
case RBR:
jj_consume_token(RBR);
jj_consume_token(SEMIC);
jj_consume_token(RCBR);
break;
default:
jj_la1[7] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void param_list() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ID:
jj_consume_token(ID);
jj_consume_token(COLON);
type();
label_7:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case COMMA:
;
break;
default:
jj_la1[8] = jj_gen;
break label_7;
}
jj_consume_token(COMMA);
jj_consume_token(ID);
jj_consume_token(COLON);
type();
}
break;
default:
jj_la1[9] = jj_gen;
}
}
static final public void type() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case INT:
jj_consume_token(INT);
break;
case BOOL:
jj_consume_token(BOOL);
break;
case REAL:
jj_consume_token(REAL);
break;
case STR:
jj_consume_token(STR);
break;
case VOID:
jj_consume_token(VOID);
break;
default:
jj_la1[10] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void main_prog() throws ParseException {
jj_consume_token(MAIN);
jj_consume_token(LCBR);
label_8:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case CONST:
case VAR:
;
break;
default:
jj_la1[11] = jj_gen;
break label_8;
}
decl();
}
label_9:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SEMIC:
case LCBR:
case EXLM:
case QUESTION:
case IF:
case WHILE:
case ID:
;
break;
default:
jj_la1[12] = jj_gen;
break label_9;
}
statement();
jj_consume_token(SEMIC);
}
jj_consume_token(RCBR);
}
static final public void statement() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ID:
jj_consume_token(ID);
jj_consume_token(ASSIGN);
expression();
break;
jj_consume_token(ID);
jj_consume_token(ASSIGN);
jj_consume_token(STR);
break;
case EXLM:
jj_consume_token(EXLM);
expression();
break;
case QUESTION:
jj_consume_token(QUESTION);
jj_consume_token(ID);
break;
jj_consume_token(ID);
jj_consume_token(LBR);
arg_list();
jj_consume_token(RBR);
break;
case LCBR:
jj_consume_token(LCBR);
label_10:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case SEMIC:
case LCBR:
case EXLM:
case QUESTION:
case IF:
case WHILE:
case ID:
;
break;
default:
jj_la1[13] = jj_gen;
break label_10;
}
statement();
jj_consume_token(SEMIC);
}
jj_consume_token(RCBR);
break;
case IF:
jj_consume_token(IF);
condition();
jj_consume_token(THEN);
statement();
break;
jj_consume_token(IF);
condition();
jj_consume_token(THEN);
statement();
jj_consume_token(ELSE);
statement();
break;
case WHILE:
jj_consume_token(WHILE);
condition();
jj_consume_token(DO);
statement();
break;
default:
jj_la1[14] = jj_gen;
}
}
static final public void expression() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS_SIGN:
jj_consume_token(PLUS_SIGN);
break;
case MINUS_SIGN:
jj_consume_token(MINUS_SIGN);
break;
case MULT_SIGN:
jj_consume_token(MULT_SIGN);
break;
case DIV_SIGN:
jj_consume_token(DIV_SIGN);
break;
default:
jj_la1[15] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void fragment() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ID:
jj_consume_token(ID);
break;
case NUM:
jj_consume_token(NUM);
break;
case PLUS_SIGN:
case MINUS_SIGN:
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case PLUS_SIGN:
jj_consume_token(PLUS_SIGN);
break;
case MINUS_SIGN:
jj_consume_token(MINUS_SIGN);
break;
default:
jj_la1[16] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
fragment();
break;
case MULT_SIGN:
case DIV_SIGN:
expression();
break;
default:
jj_la1[17] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void condition() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case NOT:
jj_consume_token(NOT);
expression();
break;
case PLUS_SIGN:
case MINUS_SIGN:
case MULT_SIGN:
case DIV_SIGN:
expression();
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case EQUALS:
jj_consume_token(EQUALS);
break;
case NOT_EQUAL:
jj_consume_token(NOT_EQUAL);
break;
case LEFT_BRACE:
jj_consume_token(LEFT_BRACE);
break;
case RIGHT_BRACE:
jj_consume_token(RIGHT_BRACE);
break;
case LEFT_BRACE_EQUAL:
jj_consume_token(LEFT_BRACE_EQUAL);
break;
case RIGHT_BRACE_EQUAL:
jj_consume_token(RIGHT_BRACE_EQUAL);
break;
case AND:
jj_consume_token(AND);
break;
case OR:
jj_consume_token(OR);
break;
default:
jj_la1[18] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
break;
case ID:
jj_consume_token(ID);
break;
default:
jj_la1[19] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
static final public void ident_list() throws ParseException {
jj_consume_token(ID);
label_11:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case COMMA:
;
break;
default:
jj_la1[20] = jj_gen;
break label_11;
}
jj_consume_token(COMMA);
jj_consume_token(ID);
}
}
static final public void arg_list() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case ID:
jj_consume_token(ID);
label_12:
while (true) {
switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
case COMMA:
;
break;
default:
jj_la1[21] = jj_gen;
break label_12;
}
jj_consume_token(COMMA);
jj_consume_token(ID);
}
break;
default:
jj_la1[22] = jj_gen;
}
}
static private boolean jj_initialized_once = false;
/** Generated Token Manager. */
static public SLPTokeniserTokenManager token_source;
static JavaCharStream jj_input_stream;
/** Current token. */
static public Token token;
/** Next token. */
static public Token jj_nt;
static private int jj_ntk;
static private int jj_gen;
static final private int[] jj_la1 = new int[23];
static private int[] jj_la1_0;
static private int[] jj_la1_1;
static {
jj_la1_init_0();
jj_la1_init_1();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x0,0x8000,0x0,0x10000,0x10000,0x0,0x1c00800,0x8000,0x10000,0x0,0x0,0x0,0x1c00800,0x1c00800,0x1c00000,0x1e0000,0x60000,0x1e0000,0xfc000000,0x1e0000,0x10000,0x10000,0x0,};
}
private static void jj_la1_init_1() {
jj_la1_1 = new int[] {0x20008,0x45104,0x20008,0x0,0x0,0x20008,0x880080,0x45104,0x0,0x800000,0x45104,0x20008,0x880080,0x880080,0x880080,0x0,0x0,0xa00000,0x802,0x800400,0x0,0x0,0x800000,};
}
/** Constructor with InputStream. */
public SLPTokeniser(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public SLPTokeniser(java.io.InputStream stream, String encoding) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new SLPTokeniserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 23; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
static public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
static public void ReInit(java.io.InputStream stream, String encoding) {
try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 23; i++) jj_la1[i] = -1;
}
/** Constructor. */
public SLPTokeniser(java.io.Reader stream) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
jj_input_stream = new JavaCharStream(stream, 1, 1);
token_source = new SLPTokeniserTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 23; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
static public void ReInit(java.io.Reader stream) {
jj_input_stream.ReInit(stream, 1, 1);
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 23; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public SLPTokeniser(SLPTokeniserTokenManager tm) {
if (jj_initialized_once) {
System.out.println("ERROR: Second call to constructor of static parser. ");
System.out.println(" You must either use ReInit() or set the JavaCC option STATIC to false");
System.out.println(" during parser generation.");
throw new Error();
}
jj_initialized_once = true;
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 23; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(SLPTokeniserTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 23; i++) jj_la1[i] = -1;
}
static private Token jj_consume_token(int kind) throws ParseException {
Token oldToken;
if ((oldToken = token).next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
if (token.kind == kind) {
jj_gen++;
return token;
}
token = oldToken;
jj_kind = kind;
throw generateParseException();
}
/** Get the next Token. */
static final public Token getNextToken() {
if (token.next != null) token = token.next;
else token = token.next = token_source.getNextToken();
jj_ntk = -1;
jj_gen++;
return token;
}
/** Get the specific Token. */
static final public Token getToken(int index) {
Token t = token;
for (int i = 0; i < index; i++) {
if (t.next != null) t = t.next;
else t = t.next = token_source.getNextToken();
}
return t;
}
static private int jj_ntk() {
if ((jj_nt=token.next) == null)
return (jj_ntk = (token.next=token_source.getNextToken()).kind);
else
return (jj_ntk = jj_nt.kind);
}
static private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
static private int[] jj_expentry;
static private int jj_kind = -1;
/** Generate ParseException. */
static public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[58];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 23; i++) {
if (jj_la1[i] == jj_gen) {
for (int j = 0; j < 32; j++) {
if ((jj_la1_0[i] & (1<<j)) != 0) {
la1tokens[j] = true;
}
if ((jj_la1_1[i] & (1<<j)) != 0) {
la1tokens[32+j] = true;
}
}
}
}
for (int i = 0; i < 58; i++) {
if (la1tokens[i]) {
jj_expentry = new int[1];
jj_expentry[0] = i;
jj_expentries.add(jj_expentry);
}
}
int[][] exptokseq = new int[jj_expentries.size()][];
for (int i = 0; i < jj_expentries.size(); i++) {
exptokseq[i] = jj_expentries.get(i);
}
return new ParseException(token, exptokseq, tokenImage);
}
/** Enable tracing. */
static final public void enable_tracing() {
}
/** Disable tracing. */
static final public void disable_tracing() {
}
}