-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
566 lines (503 loc) · 14.7 KB
/
Parser.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
package cop5556sp17;
import cop5556sp17.AST.*;
import cop5556sp17.Scanner.Kind;
import static cop5556sp17.Scanner.Kind.*;
import java.util.ArrayList;
import java.util.List;
import cop5556sp17.Scanner.Token;
public class Parser {
/**
* Exception to be thrown if a syntax error is detected in the input.
* You will want to provide a useful error message.
*
*/
@SuppressWarnings("serial")
public static class SyntaxException extends Exception {
public SyntaxException(String message) {
super(message);
}
}
/**
* Useful during development to ensure unimplemented routines are
* not accidentally called during development. Delete it when
* the Parser is finished.
*
*/
@SuppressWarnings("serial")
public static class UnimplementedFeatureException extends RuntimeException {
public UnimplementedFeatureException() {
super();
}
}
Scanner scanner;
Token t;
Parser(Scanner scanner) {
this.scanner = scanner;
t = scanner.nextToken();
}
/**
* parse the input using tokens from the scanner.
* Check for EOF (i.e. no trailing junk) when finished
*
* @throws SyntaxException
*/
Program parse() throws SyntaxException {
Program p1 = null;
p1 = program();
matchEOF();
//System.out.println("Parse done, EOF matched");
return p1;
}
Expression expression() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
Token firstToken = t, opToken; Expression e1 = null, e2 = null;
e1 = term();
while (predictSetMembershipTest(Kind.LT, Kind.LE, Kind.GT, Kind.GE, Kind.EQUAL, Kind.NOTEQUAL)) //predict set for relOp
{
opToken = consume(); //consume relOp
e2 = term();
e1 = new BinaryExpression(firstToken, e1, opToken, e2);
}
return e1;
}
Expression term() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
Token firstToken = t, opToken; Expression e1 = null, e2 = null;
e1 = elem();
while (predictSetMembershipTest(Kind.PLUS, Kind.MINUS, Kind.OR)) //predict set for weakOp
{
opToken = consume(); //consume weakOp
e2 = elem();
e1 = new BinaryExpression(firstToken, e1, opToken, e2);
}
return e1;
}
Expression elem() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
Token firstToken = t, opToken; Expression e1 = null, e2 = null;
e1 = factor();
while (predictSetMembershipTest(Kind.TIMES, Kind.DIV, Kind.AND, Kind.MOD)) //predict set for strongOp
{
opToken = consume(); //consume strongOp
e2 = factor();
e1 = new BinaryExpression(firstToken, e1, opToken, e2);
}
return e1;
}
Expression factor() throws SyntaxException {
Kind kind = t.kind;
Expression e = null;
Token temp;
switch (kind) {
case IDENT: {
//System.out.println("factor -> IDENT case");
temp = consume();
e = new IdentExpression(temp);
}
break;
case INT_LIT: {
//System.out.println("factor -> INT_LIT case");
temp = consume();
e = new IntLitExpression(temp);
}
break;
case KW_TRUE:
case KW_FALSE: {
//System.out.println("factor -> KW_TRUE/KW_FALSE case");
temp = consume();
e = new BooleanLitExpression(temp);
}
break;
case KW_SCREENWIDTH:
case KW_SCREENHEIGHT: {
temp = consume();
e = new ConstantExpression(temp);
}
break;
case LPAREN: {
consume();
e = expression();
match(RPAREN);
}
break;
default:
//you will want to provide a more useful error message
throw new SyntaxException("illegal factor: " + kind + " @" + t.getLinePos());
}
return e;
}
Block block() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
//System.out.println("block()->");
ArrayList<Dec> decList = new ArrayList<Dec>(); Dec d1 = null;
ArrayList<Statement> statementList = new ArrayList<Statement>(); Statement s1 = null;
Token firstToken;
firstToken = match(Kind.LBRACE);
while (predictSetMembershipTest(Kind.KW_INTEGER, Kind.KW_BOOLEAN, Kind.KW_IMAGE, Kind.KW_FRAME) || predictSetMembershipTest(OP_SLEEP, KW_WHILE, KW_IF, IDENT, OP_BLUR, OP_GRAY, OP_CONVOLVE, KW_SHOW, KW_HIDE, KW_MOVE, KW_XLOC, KW_YLOC, OP_WIDTH, OP_HEIGHT, KW_SCALE)) //possible typo here
{
if (predictSetMembershipTest(Kind.KW_INTEGER, Kind.KW_BOOLEAN, Kind.KW_IMAGE, Kind.KW_FRAME)) //dec
{
//System.out.println("block()->dec()->");
d1 = dec();
decList.add(d1);
}
else {
//System.out.println("block()->statement()");
s1 = statement();
statementList.add(s1);
}
}
match(Kind.RBRACE);
return (new Block(firstToken, decList, statementList));
}
Program program() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
if (t.kind == Kind.IDENT)
{
Token firstToken;
ArrayList<ParamDec> params = new ArrayList<ParamDec>(); ParamDec p1 = null;
Block b1 = null;
//System.out.println("program() -> Kind.IDENT");
firstToken = consume(); //consumes the IDENT
if (t.isKind(Kind.LBRACE))
{
//program::=IDENT block
b1 = block();
}
else if (predictSetMembershipTest(Kind.KW_URL, Kind.KW_FILE, Kind.KW_INTEGER, Kind.KW_BOOLEAN))
{
p1 = paramDec();
params.add(p1);
while (predictSetMembershipTest(Kind.COMMA))
{
consume();
p1 = paramDec();
params.add(p1);
}
b1 = block();
}
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |program() -> IDENT| @ " + t.getLinePos());
}
return (new Program(firstToken, params, b1));
}
/*
else if (t.kind == Kind.EOF) //if the input is just an empty string, return. Matching EOF is done after program() returns.
{
//System.out.println("program() -> Kind.EOF");
return;
}
*/
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |program()| @ " + t.getLinePos());
}
}
ParamDec paramDec() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
if (predictSetMembershipTest(Kind.KW_URL, Kind.KW_FILE, Kind.KW_INTEGER, Kind.KW_BOOLEAN))
{
Token firstToken, ident;
firstToken = consume();
ident = match(Kind.IDENT);
return (new ParamDec(firstToken, ident)); //type ident
}
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |paramDec()| @ " + t.getLinePos());
}
}
Dec dec() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
//System.out.println("dec()->");
if (predictSetMembershipTest(Kind.KW_INTEGER, Kind.KW_BOOLEAN, Kind.KW_IMAGE, Kind.KW_FRAME))
{
Token firstToken, ident;
firstToken = consume();
ident = match(Kind.IDENT);
Dec e = new Dec(firstToken, ident); //type ident
return e;
}
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |dec()| @ " + t.getLinePos());
}
}
Statement statement() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
//System.out.println("statement()->");
Token firstToken;
if (predictSetMembershipTest(Kind.OP_SLEEP))
{
firstToken = consume();
Expression e1 = null;
e1 = expression();
match(Kind.SEMI);
return (new SleepStatement(firstToken, e1));
}
else if (predictSetMembershipTest(Kind.KW_WHILE))
{
WhileStatement e1 = null;
e1 = whileStatement();
return e1;
}
else if (predictSetMembershipTest(Kind.KW_IF))
{
IfStatement e1 = null;
e1 = ifStatement();
return e1;
}
else if (predictSetMembershipTest(Kind.IDENT) && ((scanner.peek()).isKind(Kind.ASSIGN))) //assign(). Careful of scanner.peek(). Might result in out of bounds
{
AssignmentStatement e1 = null;
e1 = assign();
match(Kind.SEMI);
return e1;
}
else if (predictSetMembershipTest(Kind.IDENT,OP_BLUR, OP_GRAY, OP_CONVOLVE, KW_SHOW, KW_HIDE, KW_MOVE, KW_XLOC, KW_YLOC, OP_WIDTH, OP_HEIGHT, KW_SCALE))
{
//System.out.println("statement()->chain() "+t.kind);
Chain binChain = null;
binChain = chain();
match(Kind.SEMI);
return binChain;
}
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |statement()| @ " + t.getLinePos());
}
}
AssignmentStatement assign() throws SyntaxException {
//System.out.println("assign()->");
Token firstToken; IdentLValue lValue = null; Expression e1 = null;
firstToken = match(Kind.IDENT);
lValue = new IdentLValue(firstToken); //IdentLValue is also first token?
match(Kind.ASSIGN);
//System.out.println("assign() -> going into expression()");
e1 = expression();
return (new AssignmentStatement(firstToken, lValue, e1));
}
WhileStatement whileStatement() throws SyntaxException {
Token firstToken; Expression e1 = null; Block b1 = null;
firstToken = match(Kind.KW_WHILE);
match(Kind.LPAREN);
e1 = expression();
match(Kind.RPAREN);
b1 = block();
return (new WhileStatement(firstToken, e1, b1));
}
IfStatement ifStatement() throws SyntaxException {
Token firstToken; Expression e1 = null; Block b1 = null;
firstToken = match(Kind.KW_IF);
match(Kind.LPAREN);
e1 = expression();
match(Kind.RPAREN);
b1 = block();
return (new IfStatement(firstToken, e1, b1));
}
Chain chain() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
//System.out.println("chain()->");
BinaryChain e1 = null; ChainElem c1 = null, c2 = null; Token op;
Token firstToken = t;
c1 = chainElem();
op = arrowOp();
c2 = chainElem();
e1 = new BinaryChain(firstToken, c1, op, c2);
while (predictSetMembershipTest(Kind.ARROW, Kind.BARARROW))
{
firstToken = t;
op = consume();
c2 = chainElem();
e1 = new BinaryChain(firstToken, e1, op, c2);
//System.out.println("chain()->inside while loop, returned from chainElem");
}
return e1;
//System.out.println("chain()->while loop exited, chain() returning");
}
ChainElem chainElem() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
//System.out.println("chainElem()->");
Token firstToken = t; Tuple t1 = null;
if (predictSetMembershipTest(Kind.IDENT))
{
firstToken = consume();
return (new IdentChain(firstToken));
}
else if(predictSetMembershipTest(Kind.OP_BLUR, Kind.OP_GRAY, Kind.OP_CONVOLVE)) //filterOp
{
firstToken = consume();
t1 = arg();
return (new FilterOpChain(firstToken, t1));
}
else if(predictSetMembershipTest(Kind.KW_SHOW, Kind.KW_HIDE, Kind.KW_MOVE, Kind.KW_XLOC, Kind.KW_YLOC)) //frameOp
{
firstToken = consume();
t1 = arg();
return (new FrameOpChain(firstToken, t1));
}
else if(predictSetMembershipTest(Kind.OP_WIDTH, Kind.OP_HEIGHT, Kind.KW_SCALE)) //imageOp
{
firstToken = consume();
t1 = arg();
return (new ImageOpChain(firstToken, t1));
}
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |chainElem()| @ " + t.getLinePos());
}
}
Tuple arg() throws SyntaxException {
//TODO
//throw new UnimplementedFeatureException();
Token firstToken = t; Expression e1 = null; Tuple t1 = null;
List<Expression> exprList = new ArrayList<Expression>();
if (predictSetMembershipTest(Kind.LPAREN)) //(expression (,expression)*)
{
firstToken = consume(); //consumes (
e1 = expression();
exprList.add(e1);
while (predictSetMembershipTest(Kind.COMMA))
{
consume(); //consumes ,
e1 = expression();
exprList.add(e1);
}
match(Kind.RPAREN); //attempt to match )
t1 = new Tuple(firstToken, exprList);
return t1;
}
else if (predictSetMembershipTest(Kind.ARROW, Kind.BARARROW, Kind.SEMI)) //generated by the empty set
{
t1 = new Tuple(firstToken, exprList); //null and empty list
return t1;
}
else
{
throw new SyntaxException("Unexpected token(" + t.kind + ") |arg()| @ " + t.getLinePos());
}
}
//fncs not used----------------------------------------------------------------------------
Token strongOp() throws SyntaxException
{
return match(Kind.TIMES, Kind.DIV, Kind.AND, Kind.MOD);
}
Token weakOp() throws SyntaxException
{
return match(Kind.PLUS, Kind.MINUS, Kind.OR);
}
Token relOp() throws SyntaxException
{
return match(Kind.LT, Kind.LE, Kind.GT, Kind.GE, Kind.EQUAL, Kind.NOTEQUAL);
}
Token imageOp() throws SyntaxException
{
return match(Kind.OP_WIDTH, Kind.OP_HEIGHT, Kind.KW_SCALE);
}
Token frameOp() throws SyntaxException
{
return match(Kind.KW_SHOW, Kind.KW_HIDE, Kind.KW_MOVE, Kind.KW_XLOC, Kind.KW_YLOC);
}
Token filterOp() throws SyntaxException
{
return match(Kind.OP_BLUR, Kind.OP_GRAY, Kind.OP_CONVOLVE);
}
Token arrowOp() throws SyntaxException
{
return match(Kind.ARROW, Kind.BARARROW);
}
//-------------------------------------------------------------------------------------------
/**
* Checks whether the current token is the EOF token. If not, a
* SyntaxException is thrown.
*
* @return
* @throws SyntaxException
*/
private Token matchEOF() throws SyntaxException {
if (t.isKind(EOF)) {
return t;
}
throw new SyntaxException("expected EOF");
}
/**
* Checks if the current token has the given kind. If so, the current token
* is consumed and returned. If not, a SyntaxException is thrown.
*
* Precondition: kind != EOF
*
* @param kind
* @return
* @throws SyntaxException
*/
private Token match(Kind kind) throws SyntaxException {
if (t.isKind(kind)) {
return consume();
}
throw new SyntaxException("saw " + t.kind + ", expected " + kind + " " + t.getLinePos());
}
/**
* Checks if the current token has one of the given kinds. If so, the
* current token is consumed and returned. If not, a SyntaxException is
* thrown.
*
* * Precondition: for all given kinds, kind != EOF
*
* @param kinds
* list of kinds, matches any one
* @return
* @throws SyntaxException
*/
private Token match(Kind... kinds) throws SyntaxException {
// TODO. Optional but handy
String errMessageAppend = "";
for (Kind someKind : kinds) //iterate through list of kinds passed in
{
if (t.kind == someKind)
{
//System.out.println("match(<List>Kinds) -> " + t.kind);
return consume(); //replace this statement
}
errMessageAppend = errMessageAppend + " " + someKind;
}
throw new SyntaxException("Unexpected token(" + t.kind + ")," + " expected(" + errMessageAppend + " ) @" + t.getLinePos());
}
private boolean predictSetMembershipTest(Kind... kinds) {
// test if the token is a member of the predict set
// CAREFUL. This method does not throw SyntaxException. It is up to the caller to check
for (Kind someKind : kinds) //iterate through list of kinds passed in
{
if (t.kind == someKind)
{
//System.out.println("predictSetMembershipTest -> " + t.kind);
return true; //found the token in some predict set
}
}
return false;
}
/**
* Gets the next token and returns the consumed token.
*
* Precondition: t.kind != EOF
*
* @return
*
*/
private Token consume() throws SyntaxException {
Token tmp = t;
t = scanner.nextToken();
//System.out.println("next token up for consumption: " + t.kind);
return tmp;
}
}