-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProg2.java
More file actions
612 lines (565 loc) · 16.1 KB
/
Copy pathProg2.java
File metadata and controls
612 lines (565 loc) · 16.1 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
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
/* Prog2.java */
/* Generated By:JJTree&JavaCC: Do not edit this line. Prog2.java */
import java.util.Scanner;
class Prog2/*@bgen(jjtree)*/implements Prog2TreeConstants, Prog2Constants {/*@bgen(jjtree)*/
protected JJTProg2State jjtree = new JJTProg2State();public static void main(String[] args) throws ParseException, CloneNotSupportedException {
System.out.println(">>> Lambda Expression Evaluator <<<");
//****************************generate the AST for any applied Lambda expression**********************//
System.out.println("Enter an applied Lambda expression:");
Prog2 parser = new Prog2(System.in);
ASTStart start = parser.Start();
System.out.println();
System.out.println("The abstract syntax tree:");
start.dumpAST("");
System.out.println();
//*****************************call method for concreate syntax print*********************************//
System.out.println("The Lambda expression in the concrete syntax:");
start.astToString();
System.out.println();
//*****************************Free Variables*********************************************************//
System.out.println();
System.out.println("The abstract syntax tree with free variables: \n");
start.dumpFV("", null);
System.out.println();
//*****************************Substitution**********************************************************//
System.out.println("Enter an applied Lambda expression:");
Prog2 parser2 = new Prog2(System.in);
ASTStart start2 = parser2.Start();
System.out.println();
System.out.println("The abstract syntax tree:");
start2.dumpAST("");
System.out.println();
System.out.println("Enter the variable to be substituted:");
Scanner sc = new Scanner(System.in);
String var=sc.next();
System.out.println();
System.out.println("Enter the substituting applied Lambda expression:");
Prog2 parser3 = new Prog2(System.in);
ASTStart start3 = parser3.Start();
System.out.println();
System.out.println("The abstract syntax tree:");
start3.dumpAST("");
System.out.println();
System.out.println("The substitution result:");
SimpleNode resultSub = start2.substitute(var, start3);
resultSub.dumpAST(" ");
//*******************************Normal Order Evaluation***************************************************//
System.out.println();
System.out.println("Enter an applied Lambda expression:");
Prog2 parser4 = new Prog2(System.in);
ASTStart start4 = parser4.Start();
System.out.println();
System.out.println("The normal order evaluation result:");
SimpleNode result = start4.jjtGetChild(0).normalOrderEvaluate();
result.dumpAST("");
}
final public ASTStart Start() throws ParseException {/*@bgen(jjtree) Start */
ASTStart jjtn000 = new ASTStart(JJTSTART);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
Expression();
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
{if ("" != null) return jjtn000;}
} catch (Throwable jjte000) {
if (jjtc000) {
jjtree.clearNodeScope(jjtn000);
jjtc000 = false;
} else {
jjtree.popNode();
}
if (jjte000 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte000;}
}
if (jjte000 instanceof ParseException) {
{if (true) throw (ParseException)jjte000;}
}
{if (true) throw (Error)jjte000;}
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
throw new Error("Missing return statement in function");
}
final public void Expression() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case 9:{
ASTadd jjtn001 = new ASTadd(JJTADD);
boolean jjtc001 = true;
jjtree.openNodeScope(jjtn001);
try {
jj_consume_token(9);
} finally {
if (jjtc001) {
jjtree.closeNodeScope(jjtn001, true);
}
}
break;
}
case 10:{
ASTsub jjtn002 = new ASTsub(JJTSUB);
boolean jjtc002 = true;
jjtree.openNodeScope(jjtn002);
try {
jj_consume_token(10);
} finally {
if (jjtc002) {
jjtree.closeNodeScope(jjtn002, true);
}
}
break;
}
case 11:{
ASTmul jjtn003 = new ASTmul(JJTMUL);
boolean jjtc003 = true;
jjtree.openNodeScope(jjtn003);
try {
jj_consume_token(11);
} finally {
if (jjtc003) {
jjtree.closeNodeScope(jjtn003, true);
}
}
break;
}
case 12:{
ASTdiv jjtn004 = new ASTdiv(JJTDIV);
boolean jjtc004 = true;
jjtree.openNodeScope(jjtn004);
try {
jj_consume_token(12);
} finally {
if (jjtc004) {
jjtree.closeNodeScope(jjtn004, true);
}
}
break;
}
case 13:{
ASTmod jjtn005 = new ASTmod(JJTMOD);
boolean jjtc005 = true;
jjtree.openNodeScope(jjtn005);
try {
jj_consume_token(13);
} finally {
if (jjtc005) {
jjtree.closeNodeScope(jjtn005, true);
}
}
break;
}
case IDENTIFIER:{
ID();
break;
}
case INTEGER:{
Int();
break;
}
case 14:{
jj_consume_token(14);
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case IDENTIFIER:
case INTEGER:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:{
Application();
break;
}
case L:{
Abstraction();
break;
}
default:
jj_la1[0] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
jj_consume_token(15);
break;
}
default:
jj_la1[1] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
final public void Application() throws ParseException {
Expression();
label_1:
while (true) {
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case IDENTIFIER:
case INTEGER:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:{
;
break;
}
default:
jj_la1[2] = jj_gen;
break label_1;
}
ASTappl jjtn001 = new ASTappl(JJTAPPL);
boolean jjtc001 = true;
jjtree.openNodeScope(jjtn001);
try {
Expression();
} catch (Throwable jjte001) {
if (jjtc001) {
jjtree.clearNodeScope(jjtn001);
jjtc001 = false;
} else {
jjtree.popNode();
}
if (jjte001 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte001;}
}
if (jjte001 instanceof ParseException) {
{if (true) throw (ParseException)jjte001;}
}
{if (true) throw (Error)jjte001;}
} finally {
if (jjtc001) {
jjtree.closeNodeScope(jjtn001, 2);
}
}
}
}
final public void Abstraction() throws ParseException {
jj_consume_token(L);
ID();
ASTlamb jjtn001 = new ASTlamb(JJTLAMB);
boolean jjtc001 = true;
jjtree.openNodeScope(jjtn001);
try {
body();
} catch (Throwable jjte001) {
if (jjtc001) {
jjtree.clearNodeScope(jjtn001);
jjtc001 = false;
} else {
jjtree.popNode();
}
if (jjte001 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte001;}
}
if (jjte001 instanceof ParseException) {
{if (true) throw (ParseException)jjte001;}
}
{if (true) throw (Error)jjte001;}
} finally {
if (jjtc001) {
jjtree.closeNodeScope(jjtn001, 2);
}
}
}
final public void Int() throws ParseException {/*@bgen(jjtree) Int */
ASTInt jjtn000 = new ASTInt(JJTINT);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
t = jj_consume_token(INTEGER);
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
jjtn000.setName(t.image);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void body() throws ParseException {
switch ((jj_ntk==-1)?jj_ntk_f():jj_ntk) {
case 16:{
jj_consume_token(16);
Application();
break;
}
case IDENTIFIER:{
ID();
ASTlamb jjtn001 = new ASTlamb(JJTLAMB);
boolean jjtc001 = true;
jjtree.openNodeScope(jjtn001);
try {
body();
} catch (Throwable jjte001) {
if (jjtc001) {
jjtree.clearNodeScope(jjtn001);
jjtc001 = false;
} else {
jjtree.popNode();
}
if (jjte001 instanceof RuntimeException) {
{if (true) throw (RuntimeException)jjte001;}
}
if (jjte001 instanceof ParseException) {
{if (true) throw (ParseException)jjte001;}
}
{if (true) throw (Error)jjte001;}
} finally {
if (jjtc001) {
jjtree.closeNodeScope(jjtn001, 2);
}
}
break;
}
default:
jj_la1[3] = jj_gen;
jj_consume_token(-1);
throw new ParseException();
}
}
final public void ID() throws ParseException {/*@bgen(jjtree) ID */
ASTID jjtn000 = new ASTID(JJTID);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);Token t;
try {
t = jj_consume_token(IDENTIFIER);
jjtree.closeNodeScope(jjtn000, true);
jjtc000 = false;
jjtn000.setName(t.image);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Add() throws ParseException {/*@bgen(jjtree) Add */
ASTAdd jjtn000 = new ASTAdd(JJTADD);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
jj_consume_token(9);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Sub() throws ParseException {/*@bgen(jjtree) Sub */
ASTSub jjtn000 = new ASTSub(JJTSUB);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
jj_consume_token(10);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Mul() throws ParseException {/*@bgen(jjtree) Mul */
ASTMul jjtn000 = new ASTMul(JJTMUL);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
jj_consume_token(11);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Div() throws ParseException {/*@bgen(jjtree) Div */
ASTDiv jjtn000 = new ASTDiv(JJTDIV);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
jj_consume_token(12);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
final public void Mod() throws ParseException {/*@bgen(jjtree) Mod */
ASTMod jjtn000 = new ASTMod(JJTMOD);
boolean jjtc000 = true;
jjtree.openNodeScope(jjtn000);
try {
jj_consume_token(13);
} finally {
if (jjtc000) {
jjtree.closeNodeScope(jjtn000, true);
}
}
}
/** Generated Token Manager. */
public Prog2TokenManager token_source;
SimpleCharStream jj_input_stream;
/** Current token. */
public Token token;
/** Next token. */
public Token jj_nt;
private int jj_ntk;
private int jj_gen;
final private int[] jj_la1 = new int[4];
static private int[] jj_la1_0;
static {
jj_la1_init_0();
}
private static void jj_la1_init_0() {
jj_la1_0 = new int[] {0x7f60,0x7f40,0x7f40,0x10040,};
}
/** Constructor with InputStream. */
public Prog2(java.io.InputStream stream) {
this(stream, null);
}
/** Constructor with InputStream and supplied encoding */
public Prog2(java.io.InputStream stream, String encoding) {
try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
token_source = new Prog2TokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 4; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.InputStream stream) {
ReInit(stream, null);
}
/** Reinitialise. */
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;
jjtree.reset();
jj_gen = 0;
for (int i = 0; i < 4; i++) jj_la1[i] = -1;
}
/** Constructor. */
public Prog2(java.io.Reader stream) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
token_source = new Prog2TokenManager(jj_input_stream);
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 4; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(java.io.Reader stream) {
if (jj_input_stream == null) {
jj_input_stream = new SimpleCharStream(stream, 1, 1);
} else {
jj_input_stream.ReInit(stream, 1, 1);
}
if (token_source == null) {
token_source = new Prog2TokenManager(jj_input_stream);
}
token_source.ReInit(jj_input_stream);
token = new Token();
jj_ntk = -1;
jjtree.reset();
jj_gen = 0;
for (int i = 0; i < 4; i++) jj_la1[i] = -1;
}
/** Constructor with generated Token Manager. */
public Prog2(Prog2TokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jj_gen = 0;
for (int i = 0; i < 4; i++) jj_la1[i] = -1;
}
/** Reinitialise. */
public void ReInit(Prog2TokenManager tm) {
token_source = tm;
token = new Token();
jj_ntk = -1;
jjtree.reset();
jj_gen = 0;
for (int i = 0; i < 4; i++) jj_la1[i] = -1;
}
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. */
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. */
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;
}
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);
}
private java.util.List<int[]> jj_expentries = new java.util.ArrayList<int[]>();
private int[] jj_expentry;
private int jj_kind = -1;
/** Generate ParseException. */
public ParseException generateParseException() {
jj_expentries.clear();
boolean[] la1tokens = new boolean[17];
if (jj_kind >= 0) {
la1tokens[jj_kind] = true;
jj_kind = -1;
}
for (int i = 0; i < 4; 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;
}
}
}
}
for (int i = 0; i < 17; 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);
}
private boolean trace_enabled;
/** Trace enabled. */
final public boolean trace_enabled() {
return trace_enabled;
}
/** Enable tracing. */
final public void enable_tracing() {
}
/** Disable tracing. */
final public void disable_tracing() {
}
}