-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleC.java
More file actions
534 lines (514 loc) · 15.3 KB
/
SimpleC.java
File metadata and controls
534 lines (514 loc) · 15.3 KB
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
/* SimpleC.java */
/* Generated By:JavaCC: Do not edit this line. SimpleC.java */
/** Simple brace matcher. */
public class SimpleC implements SimpleCConstants {
/** Main entry point. */
public static void main(String args[]) throws ParseException {
SimpleC parser = new SimpleC(System.in);
parser.Input();
}
/**Root production. */
static final public void Input() throws ParseException {int count;
count = anytoken();
jj_consume_token(0);
System.out.println("The number of tokens is " + count);
}
static final public int anytoken() throws ParseException {Token t;
int count=0;
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case LPARENT:{
t = jj_consume_token(LPARENT);
System.out.println("LPARENT, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case RPARENT:{
t = jj_consume_token(RPARENT);
System.out.println("RPARENT, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case ID:{
t = jj_consume_token(ID);
System.out.println("ID, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LETTER:{
t = jj_consume_token(LETTER);
System.out.println("LETTER, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case DIGIT:{
t = jj_consume_token(DIGIT);
System.out.println("DIGIT, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LCURLY:{
t = jj_consume_token(LCURLY);
System.out.println("LCURLY, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case RCURLY:{
t = jj_consume_token(RCURLY);
System.out.println("RCURLY, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LBRACE:{
t = jj_consume_token(LBRACE);
System.out.println("LBRACE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case RBRACE:{
t = jj_consume_token(RBRACE);
System.out.println("RBRACE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case COMMA:{
t = jj_consume_token(COMMA);
System.out.println("COMMA, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case SEMICOLON:{
t = jj_consume_token(SEMICOLON);
System.out.println("SEMICOLON, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case EQUAL:{
t = jj_consume_token(EQUAL);
System.out.println("EQUAL, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case AMPERSAND:{
t = jj_consume_token(AMPERSAND);
System.out.println("AMPERSAND, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case OROR:{
t = jj_consume_token(OROR);
System.out.println("OROR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case ANDAND:{
t = jj_consume_token(ANDAND);
System.out.println("ANDAND, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case EQUALEQUAL:{
t = jj_consume_token(EQUALEQUAL);
System.out.println("EQUALEQUAL, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case NOTEQUAL:{
t = jj_consume_token(NOTEQUAL);
System.out.println("NOTEQUAL, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LESS:{
t = jj_consume_token(LESS);
System.out.println("LESS, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case GREAT:{
t = jj_consume_token(GREAT);
System.out.println("GREAT, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LESSEQUAL:{
t = jj_consume_token(LESSEQUAL);
System.out.println("LESSEQUAL, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case GREATEQUAL:{
t = jj_consume_token(GREATEQUAL);
System.out.println("GREATEQUAL, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case PLUS:{
t = jj_consume_token(PLUS);
System.out.println("PLUS, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case MINUS:{
t = jj_consume_token(MINUS);
System.out.println("MINUS, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case TIMES:{
t = jj_consume_token(TIMES);
System.out.println("TIMES, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case DIVIDE:{
t = jj_consume_token(DIVIDE);
System.out.println("DIVIDE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case PERCENT:{
t = jj_consume_token(PERCENT);
System.out.println("PERCENT, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case CHARSTARSTAR:{
t = jj_consume_token(CHARSTARSTAR);
System.out.println("CHARSTARSTAR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case CHARSTAR:{
t = jj_consume_token(CHARSTAR);
System.out.println("CHARSTAR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LONGSTAR:{
t = jj_consume_token(LONGSTAR);
System.out.println("LONGSTAR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case LONG:{
t = jj_consume_token(LONG);
System.out.println("LONG, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case VOID:{
t = jj_consume_token(VOID);
System.out.println("VOID, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case IF:{
t = jj_consume_token(IF);
System.out.println("IF, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case ELSE:{
t = jj_consume_token(ELSE);
System.out.println("ELSE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case WHILE:{
t = jj_consume_token(WHILE);
System.out.println("WHILE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case DO:{
t = jj_consume_token(DO);
System.out.println("DO, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case FOR:{
t = jj_consume_token(FOR);
System.out.println("FOR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case CONTINUE:{
t = jj_consume_token(CONTINUE);
System.out.println("CONTINUE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case BREAK:{
t = jj_consume_token(BREAK);
System.out.println("BREAK, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case RETURN:{
t = jj_consume_token(RETURN);
System.out.println("RETURN, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case DOUBLESTAR:{
t = jj_consume_token(DOUBLESTAR);
System.out.println("DOUBLESTAR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case DOUBLE:{
t = jj_consume_token(DOUBLE);
System.out.println("DOUBLE, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case CHAR_CONST:{
t = jj_consume_token(CHAR_CONST);
System.out.println("CHAR_CONST, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case STRING_CONST:{
t = jj_consume_token(STRING_CONST);
System.out.println("STRING_CONST, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case INTEGER_CONST:{
t = jj_consume_token(INTEGER_CONST);
System.out.println("INTEGER_CONST, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case DOUBLE_CONST:{
t = jj_consume_token(DOUBLE_CONST);
System.out.println("DOUBLE_CONST, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case PRINTING_CHAR:{
t = jj_consume_token(PRINTING_CHAR);
System.out.println("PRINTING_CHAR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case OCTAL:{
t = jj_consume_token(OCTAL);
System.out.println("OCTAL, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case HEX_DIGIT:{
t = jj_consume_token(HEX_DIGIT);
System.out.println("HEX_DIGIT, \u005c""+t.image+"\u005c"" ); count++;
break;
}
case OTHER_CHAR:{
t = jj_consume_token(OTHER_CHAR);
System.out.println("OTHER_CHAR, \u005c""+t.image+"\u005c"" ); count++;
break;
}
default:
jj_la1[0] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case LPARENT:
case RPARENT:
case LCURLY:
case RCURLY:
case LBRACE:
case RBRACE:
case COMMA:
case SEMICOLON:
case EQUAL:
case ANDAND:
case AMPERSAND:
case OROR:
case EQUALEQUAL:
case NOTEQUAL:
case LESSEQUAL:
case GREATEQUAL:
case LESS:
case GREAT:
case PLUS:
case MINUS:
case TIMES:
case DIVIDE:
case PERCENT:
case CHARSTARSTAR:
case CHARSTAR:
case LONGSTAR:
case LONG:
case VOID:
case IF:
case ELSE:
case WHILE:
case DO:
case FOR:
case CONTINUE:
case BREAK:
case RETURN:
case DOUBLESTAR:
case DOUBLE:
case ID:
case CHAR_CONST:
case STRING_CONST:
case INTEGER_CONST:
case DOUBLE_CONST:
case PRINTING_CHAR:
case LETTER:
case DIGIT:
case OCTAL:
case HEX_DIGIT:
case OTHER_CHAR:{
;
break;
}
default:
jj_la1[1] = jj_gen;
break label_1;
}
}
{if ("" != null) return count;}
throw new Error("Missing return statement in function");
}
static private boolean jj_initialized_once = false;
/** Generated Token Manager. */
static public SimpleCTokenManager token_source;
static SimpleCharStream 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[2];
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[] {0xffffff00,0xffffff00,};
}
private static void jj_la1_init_1() {
jj_la1_1 = new int[] {0x1ffffff,0x1ffffff,};
}
/** Constructor with InputStream. */
public SimpleC(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public SimpleC(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 SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new SimpleCTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 2; 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 < 2; i++) jj_la1[i] = -1;
}
/** Constructor. */
public SimpleC(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 SimpleCharStream(stream, 1, 1);
token_source = new SimpleCTokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 2; 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 < 2; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public SimpleC(SimpleCTokenManager 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 < 2; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(SimpleCTokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 2; 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_f() {
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[57];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 2; 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 < 57; 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() {
}
}