-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPA4.jj
More file actions
535 lines (416 loc) · 9.36 KB
/
Copy pathPA4.jj
File metadata and controls
535 lines (416 loc) · 9.36 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
/*
* This is PA4.jj file.
* It will build a tree we can traverse using the Visitor pattern!
* The language is MiniC, which is the subset of MiniJava
* consisting of only MethodDecls (i.e. no classes or instance variables)
* and the only types are int and boolean
* and the only statments are Assign, If, and Print
* all of the expressions are available except for &&
* that is <, +,-,* integerLiterals and IdentifierExpressions
*/
/*
Video 1: https://brandeis.zoom.us/rec/share/tXjiXi99E29EppXPXkjg-_GbBS2bEX1hIlfQmzDHubxsfvU4SRZtAtfzJrr9qr6D.hJiFx2GHakf4fQIB?startTime=1710909695000
Video 2https://brandeis.zoom.us/rec/share/tXjiXi99E29EppXPXkjg-_GbBS2bEX1hIlfQmzDHubxsfvU4SRZtAtfzJrr9qr6D.hJiFx2GHakf4fQIB?startTime=1710910230000
Team: Xin Chen
*/
options {
IGNORE_CASE=false;
}
PARSER_BEGIN(PA4)
import syntaxtree.*;
import java.io.*;
public class PA4 {
/** Main entry point. */
public static void main(String args[]) {
try {
PA4 t = new PA4(new FileInputStream(args[0]));
Program n = t.Start();
/*
System.out.println("\n\nPretty Printing the Abstract Syntax Tree");
Visitor v1 = new AST_Visitor(); // pretty prints the Abstract Syntax Tree
n.accept(v1, 0);
*/
/* uncomment this only for PA4b
System.out.println("\n\nPretty Print the Program");
Visitor v2 = new PP_Visitor(); // pretty prints the MiniC program
String s = (String) n.accept(v2, 0);
//System.out.println("#include <stdio.h>\n#include <stdbool.h>\nvoid print(int n){printf(\"%10d\\n\",n);}");
System.out.println(s);
*/
/* uncomment this only for MiniC2Pascal translator
//System.out.println("\n\nPretty Print the Program");
Visitor v2 = new Pascal_Visitor(); // pretty prints the MiniC program
String s = (String) n.accept(v2, 0);
System.out.println("PROGRAM Demo;\n");
System.out.println(s);
System.out.println("BEGIN\n main(1);\nEND.");
*/
/* uncomment this only for PA4c and PA4d
System.out.println("\n\nGenerating Symbol Table");
SymbolTableVisitor v3 = new SymbolTableVisitor(); // generates a SymbolTable
SymbolTable st = v3.symbolTable;
n.accept(v3,"");
System.out.println(st);
*/
/* uncomment this only for PA4c and PA4d */
System.out.println("\n\nGenerating Symbol Table");
SymbolTableVisitorModified v3 = new SymbolTableVisitorModified(); // generates a SymbolTable
SymbolTableModified st = v3.symbolTable;
n.accept(v3,"");
System.out.println(st);
/* uncomment this only for PA4d */
System.out.println("\n\nType Checking");
TypeCheckingVisitor v4 = new TypeCheckingVisitor(st);
n.accept(v4,"");
System.out.println(v4.num_errors+" type errors found");
//System.out.println("\n\nDone!");
/* Only for C2Python_Visitor
System.out.println("\n\nPython Print the Program");
Visitor v2 = new C2Python_Visitor(); // pretty prints the MiniC program
String s = (String) n.accept(v2, 0);
System.out.println(s);
*/
} catch (Exception e) {
System.out.println("Oops.");
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
PARSER_END(PA4)
SKIP: /* Whitespace */
{
"\t"
| "\n"
| "\r"
| " "
}
TOKEN:
{
<PUBLIC: "public">
| <PREFACE: "#include <stdio.h>\n#include <stdbool.h>\nvoid print(int n){printf(\"%10d\\n\",n);}">
| <CLASS: "class">
| <STATIC: "static">
| <VOID: "void">
| <STRING: "String">
| <EXTENDS: "extends">
| <RETURN: "return">
| <INT: "int">
| <BOOLEAN: "boolean">
| <TRUE: "true">
| <FALSE: "false">
| <IF: "if">
| <ELSE: "else">
| <WHILE: "while">
| <PRINTLN: "System.out.println">
| <LENGTH: "length">
| <THIS: "this">
| <MAIN: "main">
| <NEW: "new">
| <BANG: "!">
| <COLON: ":">
| <SEMICOLON: ";">
| <COMMA: ",">
| <DOT: ".">
| <EQUALS: "=">
| <AND: "&&">
| <LT: "<">
| <GT: ">">
| <LPAREN: "(" >
| <RPAREN: ")" >
| <LBRACKET: "[" >
| <RBRACKET: "]" >
| <LCURLY: "{" >
| <RCURLY: "}" >
| <PLUS_OP: "+" >
| <MINUS_OP: "-" >
| <TIMES_OP: "*" >
| <NUMBER: (["0"-"9"])+ >
| <ID: (["a"-"z"]|"_"|["A"-"Z"])(["a"-"z"]|"_"|["A"-"Z"]|["0"-"9"])* >
}
/* Program Syntax */
Program Start():
{
MainClass mc;
ClassDeclList cl;
}
{
mc = MainClass()
cl = ClassDecls()
<EOF>
{return new Program(mc , cl);}
}
MainClass MainClass():
{
Statement s;
Token t;}
{
<CLASS>
t = <ID>
<LCURLY> <PUBLIC> <STATIC> <VOID> <MAIN>
<LPAREN> <STRING> <LBRACKET>
<RBRACKET>
<ID>
<RPAREN> <LCURLY>
s = Statement()
<RCURLY> <RCURLY>
{return new MainClass(new Identifier(t.image), s);}
}
ClassDeclList ClassDecls() :
{
ClassDeclList clist = null;
ClassDecl c;
}
{
(
( c=ClassDecl()
{clist = new ClassDeclList(c,clist);}
)*
)
{return clist;}
}
ClassDecl ClassDecl():
{
Token t;
VarDeclList vlist;
MethodDeclList mlist;
}
{
<CLASS>
t = <ID>
<LCURLY>
vlist = VarDecls()
mlist = MethodDecls()
<RCURLY>
{return new ClassDecl(new Identifier(t.image), vlist, mlist);}
}
MethodDeclList MethodDecls() :
{
MethodDeclList mlist = null;
MethodDecl m;
}
{
(
( m=MethodDecl()
{mlist = new MethodDeclList(m,mlist);}
)*
)
{return mlist;}
}
MethodDecl MethodDecl() :
{
Type ty;
Token t;
FormalList flist = null;
VarDeclList vlist;
StatementList slist;
Exp e;
}
{
<PUBLIC> ty=Type() t=<ID> <LPAREN> (flist=FormalList())? <RPAREN>
<LCURLY>
vlist = VarDecls()
slist = Statements()
<RETURN> e = Exp() <SEMICOLON>
<RCURLY>
{return new MethodDecl(ty,new Identifier(t.image),flist,vlist,slist,e);}
}
VarDeclList VarDecls() :
{
VarDeclList vlist = null;
VarDecl v;
}
{
( LOOKAHEAD(2)
v= VarDecl()
{vlist = new VarDeclList(v,vlist);}
)*
{return(vlist);}
}
VarDecl VarDecl() :
{
Type ty;
Token t;
}
{
ty=Type() t=<ID> <SEMICOLON>
{return(new VarDecl(ty,new Identifier(t.image)));}
}
Type Type():
{
Token t;
}
{
<BOOLEAN>
{return new BooleanType();}
|
LOOKAHEAD(2)
<INT> <LBRACKET> <RBRACKET>
{return new IntArrayType();}
|
<INT>
{return new IntegerType();}
|
t = <ID>
{return new IdentifierType(t.image);}
}
FormalList FormalList() :
{
FormalList flist=null;
Formal f1,f2;
}
{
f1=Formal() {flist = new FormalList(f1,null);}
(
<COMMA> f2=Formal()
{flist = new FormalList(f2,flist);}
)*
{return flist;}
}
Formal Formal() :
{
Type ty;
Token t;
}
{
ty=Type() t=<ID>
{return new Formal(ty,new Identifier(t.image));}
}
StatementList Statements() :
{
StatementList s_list = null;
Statement s;
}
{
(s=Statement()
{s_list = new StatementList(s,s_list);}
) *
{return s_list;}
}
Statement Statement() :
{
Token t;
StatementList s_list;
Exp e1,e2;
Statement s1,s2;
}
{
<LCURLY> s_list = Statements() <RCURLY>
{return(new Block(s_list));}
|
<IF> <LPAREN> e1=Exp()<RPAREN> s1=Statement() <ELSE> s2=Statement()
{return(new If(e1,s1,s2));}
|
<PRINTLN> <LPAREN> e1=Exp() <RPAREN> <SEMICOLON>
{return(new Print(e1));}
|
LOOKAHEAD(2) t=<ID> <EQUALS> e1=Exp() <SEMICOLON>
{return(new Assign(new Identifier(t.image),e1));}
|
<WHILE> <LPAREN> e1 = Exp() <RPAREN> s1 = Statement()
{return(new While(e1, s1)); }
|
t = <ID> <LBRACKET> e1 = Exp() <RBRACKET> <EQUALS> e2 = Exp() <SEMICOLON>
{return (new ArrayAssign(new Identifier(t.image),e1,e2));}
}
Exp Exp():
{Exp a,b;}
{
a=Exp4()
(LOOKAHEAD(2) <AND> b=Exp4() {a = new And(a,b);})*
{return(a);}
}
Exp Exp4():
{Exp a,b;}
{
a=Exp9()
(LOOKAHEAD(2) <LT> b=Exp9() {a = new LessThan(a,b);})*
{return(a);}
}
Exp Exp9():
{Exp a,b;}
{
a=Exp11()
(LOOKAHEAD(2)
(
"+" b=Exp11() {a = new Plus(a,b);}
|
"-" b=Exp11() {a = new Minus(a,b);}
)
)*
{return(a);}
}
Exp Exp11():
{Exp a,b;}
{
a=Exp12()
(
LOOKAHEAD(2)
"*" b=Exp12()
{a = new Times(a,b);}
)*
{return(a);}
}
Exp Exp12():
{Exp a,b;
ExpList c = null;
Token t;}
{
a=Exp14()
(
LOOKAHEAD(2)
<DOT> <LENGTH>
{a = new ArrayLength(a);}
|
<DOT> t = <ID> <LPAREN> (c = ExpList())? <RPAREN>
{a = new Call(a,new Identifier(t.image),c);}
|
<LBRACKET> b = Exp() <RBRACKET>
{a = new ArrayLookup(a,b);}
)*
{return(a);}
}
Exp Exp14():
{Exp a,b;}
{
<BANG> a = Exp16()
{return(new Not(a));}
|
a = Exp16()
{return(a);}
}
Exp Exp16() :
{Exp a;
Token t;
ExpList c;
}
{
t=<NUMBER> {return new IntegerLiteral(Integer.parseInt(t.image));}
|
<TRUE> {return new True();}
|
<FALSE> {return new False();}
|
<LPAREN> a=Exp() <RPAREN>
{return(new ExpGroup(a));}
|
t=<ID> {return new IdentifierExp(t.image);}
|
<THIS>
{return new This();}
|
LOOKAHEAD(2)
<NEW> <INT> <LBRACKET> a = Exp() <RBRACKET>
{return new NewArray(a);}
|
<NEW> t = <ID> <LPAREN> <RPAREN>
{return new NewObject(new Identifier(t.image));}
}
ExpList ExpList() :
{ExpList a;
Exp b;}
{
b=Exp() {a = new ExpList(null,b);}
(<COMMA> b=Exp() {a=new ExpList(a,b);})*
{return a;}
}