-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
625 lines (519 loc) · 13.6 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
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
package cop5556sp18;
import cop5556sp18.Scanner.Token;
//import cop5556sp18.Types.Type;
import cop5556sp18.Scanner.Kind;
import static cop5556sp18.Scanner.Kind.*;
import java.util.ArrayList;
import cop5556sp18.AST.*;
public class Parser {
@SuppressWarnings("serial")
public static class SyntaxException extends Exception {
Token t;
public SyntaxException(Token t, String message) {
super(message);
this.t = t;
}
}
Scanner scanner;
Token t;
Parser(Scanner scanner) {
this.scanner = scanner;
t = scanner.nextToken();
}
public Program parse() throws SyntaxException {
Program prog = program();
matchEOF();
return prog;
}
/*
* Program ::= Identifier Block
*/
Program program() throws SyntaxException {
Token first=t;
Token programName = match(IDENTIFIER);
Block block = block();
return new Program (first , programName , block);
}
/*
* Block ::= { ( (Declaration | Statement) ; )* }
*/
Kind[] firstDec = { KW_int, KW_boolean, KW_image, KW_float, KW_filename };
Kind[] firstStatement = {KW_sleep, KW_show, KW_if, KW_while, KW_write , KW_input, KW_red ,KW_green ,KW_alpha ,KW_blue , IDENTIFIER, };
Kind[] colors = {KW_red ,KW_green ,KW_blue ,KW_alpha };
Kind[] funName = {KW_sin ,KW_cos , KW_atan, KW_abs ,KW_log ,KW_cart_x ,KW_cart_y,KW_polar_a ,KW_polar_r ,KW_int ,KW_float ,KW_width ,KW_height,KW_red ,KW_green ,KW_blue ,KW_alpha};
Kind[] Ops = { OP_LT, OP_GT, OP_LE, OP_GE };
Kind[] predefName = { KW_Z, KW_default_height, KW_default_width};
Block block() throws SyntaxException
{ Token first =t;
match(LBRACE);
ArrayList<ASTNode> statanddec = new ArrayList<ASTNode>();
while (isKind(firstDec)|| isKind(firstStatement))
{
if (isKind(firstDec))
{ Declaration decl = declaration();
statanddec.add(decl);
}
else if (isKind(firstStatement))
{ Statement st = statement();
statanddec.add(st);
}
match(SEMI);
}
match(RBRACE);
return new Block(first, statanddec);
}
Declaration declaration() throws SyntaxException
{
Token first = t;
Token type = consume();// match(KW_image);
Token name = match (IDENTIFIER);
Expression ex1 = null;
Expression ex2 = null;
if (type.kind == KW_image) {
//match(KW_image);
//match(IDENTIFIER);
if(isKind(LSQUARE))
{
consume();
ex1 = expression();
match(COMMA);
ex2 = expression();
match(RSQUARE);
}
}
return new Declaration(first, type, name, ex1, ex2);
}
Statement statement() throws SyntaxException
{
Statement st = null;
if (isKind(KW_input))
{
st = inputSt();
}
else if (isKind(KW_write))
{
st = writeSt();
}
else if (isKind(KW_if))
{
st = ifSt();
}
else if (isKind(KW_while))
{
st = whileSt();
}
else if (isKind(KW_show))
{
st = showSt();
}
else if (isKind(KW_sleep))
{
st = sleepSt();
}
else if (isKind(colors) || isKind(IDENTIFIER))
{
st = assignmentSt();
}
else {
throw new UnsupportedOperationException();
}
return st;
}
StatementInput inputSt() throws SyntaxException
{
Token first = t;
// Statement st = null;
consume();
Token nameDest = match(IDENTIFIER);
match(KW_from);
match(OP_AT);
Expression ex = expression();
return new StatementInput(first,nameDest, ex);
}
StatementWrite writeSt() throws SyntaxException
{ Token first = t;
//Statement st = null;
consume();
Token nameS = match(IDENTIFIER);
match(KW_to);
Token nameD = match(IDENTIFIER);
return new StatementWrite(first, nameS , nameD);
}
StatementWhile whileSt() throws SyntaxException
{ Token first = t;
//Statement st = null;
match(KW_while);//consume();
match(LPAREN);
Expression ex = expression();
match(RPAREN);
Block bl = block();
return new StatementWhile(first,ex,bl);
}
StatementIf ifSt() throws SyntaxException
{
Token first = t;
//Statement st = null;
match(KW_if);//consume();
match(LPAREN);
Expression ex = expression();
match(RPAREN);
Block bl = block();
return new StatementIf(first , ex, bl);
}
StatementShow showSt() throws SyntaxException
{
Token first = t;
//Statement st = null;
consume();
Expression ex = expression();
return new StatementShow(first,ex);
}
StatementSleep sleepSt() throws SyntaxException
{
Token first = t;
//Statement st = null;
consume();
Expression ex = expression();
return new StatementSleep(first,ex);
}
StatementAssign assignmentSt() throws SyntaxException
{
Token first = t;
//Statement st = null;
LHS lhs = LHS_exp();
match(OP_ASSIGN);
Expression ex = expression();
return new StatementAssign(first, lhs, ex);
}
LHS LHS_exp() throws SyntaxException
{
Token first = t;
Token col = null;
if (isKind(IDENTIFIER))
{ Token name = match(IDENTIFIER);//consume();
if (isKind(LSQUARE))
{
PixelSelector pl = pixelSel();
return new LHSPixel(first,name,pl);
}
return new LHSIdent(first,name);
}
else if (isKind(colors))
{
for (int i=0;i<colors.length;i++)
{
if(isKind(colors[i]))
{
col = consume();//match(colors[i]);
}
}
match(LPAREN);
Token name = match(IDENTIFIER);
PixelSelector pl2 = pixelSel();
match(RPAREN);
return new LHSSample(first,name,pl2,col);
}
else
throw new SyntaxException(t,"Incorrect Assignment"); //TODO give a better error message!
}
PixelSelector pixelSel() throws SyntaxException
{
Token first = t;
consume();
Expression ex1 = expression();
match(COMMA);
Expression ex2 = expression();
match(RSQUARE);
return new PixelSelector (first, ex1, ex2);
}
Expression expression() throws SyntaxException
{ Token first = t;
Expression ex1 = orExpression();
if (isKind (OP_QUESTION))
{
match(OP_QUESTION);
Expression ex2 = expression();
match(OP_COLON);
Expression ex3 = expression();
ex1 = new ExpressionConditional(first, ex1, ex2, ex3);
}
return ex1;
}
Expression orExpression() throws SyntaxException
{ Token first = t;
Expression e0 = andExpression();
while (isKind(OP_OR))
{
Token op = match(OP_OR);
Expression e1 = andExpression();
e0 = new ExpressionBinary(first, e0, op, e1);
}
return e0;
}
Expression andExpression() throws SyntaxException
{
Token first = t;
Expression e0 = eqExpression();
while (isKind(OP_AND))
{
Token op = consume();
Expression e1 = eqExpression();
e0 = new ExpressionBinary(first, e0, op, e1);
}
return e0;
}
Expression eqExpression() throws SyntaxException
{
Token first = t;
Expression e0 = relExpression();
while (isKind(OP_EQ) || isKind(OP_NEQ))
{
Token op = consume();
Expression e1 = relExpression();
e0 = new ExpressionBinary(first, e0,op,e1);
}
return e0;
}
Expression relExpression() throws SyntaxException
{
Token first = t;
Token op = null;
Expression e0 = addExpression();
while (isKind(OP_LT) || isKind(OP_GT) || isKind(OP_LE)|| isKind(OP_GE))
{ op = consume();
Expression e1 = addExpression();
e0 = new ExpressionBinary(first,e0,op,e1);
}
return e0;
}
Expression addExpression() throws SyntaxException
{
Token first = t;
Token op = null;
Expression e0 = multExpression();
while (isKind(OP_PLUS ) || isKind(OP_MINUS))
{ op = consume();
Expression e1 = multExpression();
e0 = new ExpressionBinary(first,e0,op,e1);
}
return e0;
}
Expression multExpression() throws SyntaxException
{
Token first = t;
Expression e0 = powerExpression();
Token op =null;
while (isKind(OP_TIMES) || isKind(OP_DIV)||isKind(OP_MOD))
{ op = consume();//match(OP_MOD);
Expression e1 = powerExpression();
e0 = new ExpressionBinary(first,e0,op,e1);
}
return e0;
}
Expression powerExpression() throws SyntaxException
{
Token first = t;
Expression e0 = unaryExpression();
if (isKind(OP_POWER)) {
Token op = consume();
Expression e1 = powerExpression();
return new ExpressionBinary(first, e0, op, e1);
}
return e0;
}
Expression unaryExpression() throws SyntaxException
{ Token first = t;
if(isKind(OP_PLUS) || isKind(OP_MINUS))
{
Token op = consume();//match(OP_PLUS);
Expression ex1 = unaryExpression();
return new ExpressionUnary(first, op, ex1);
}
else if(isKind(OP_EXCLAMATION))
{
Token op = consume();
Expression ex2 = unaryExpression();
return new ExpressionUnary(first,op,ex2);
}
else
{
//System.out.println("here2");
return primary();
}
}
private Expression primary() throws SyntaxException
{ Token first = t;
if(isKind(INTEGER_LITERAL))
{
Token integerliteral = match(INTEGER_LITERAL);
return new ExpressionIntegerLiteral(first, integerliteral);
}
else if(isKind(BOOLEAN_LITERAL))
{
Token boolliteral = match(BOOLEAN_LITERAL);
return new ExpressionBooleanLiteral(first, boolliteral);
}
else if(isKind(FLOAT_LITERAL))
{
Token floatliteral = match(FLOAT_LITERAL);
return new ExpressionFloatLiteral(first, floatliteral);
}
else if(isKind(IDENTIFIER))
{
Token name = match(IDENTIFIER);
if(isKind(LSQUARE))
{
PixelSelector pixel = pixelSel();
return new ExpressionPixel(first, name, pixel);
}
return new ExpressionIdent(first, name);
}
else if(isKind(LPAREN))
{
consume();
Expression ex = expression();
match(RPAREN);
return ex;
}
else if(isKind(funName) || isKind(colors))
{
return funAppn();
}
else if(isKind(LPIXEL))
{
return pixelConstructor();
}
else if(isKind(predefName))
{ Token t = null;
for (int j=0;j<predefName.length;j++)
{
if(isKind(predefName[j]))
{
t = consume();
}
}
return new ExpressionPredefinedName (first, t);
}
else
{
throw new SyntaxException(t,"Starting of the Expression is illegal.");
}
}
ExpressionPixelConstructor pixelConstructor() throws SyntaxException
{
Token first = t;
ExpressionPixelConstructor epc = null;
match(LPIXEL);
Expression a1 = expression();
match(COMMA);
Expression a2 = expression();
match(COMMA);
Expression a3 = expression();
match(COMMA);
Expression a4 = expression();
match(RPIXEL);
epc = new ExpressionPixelConstructor(first, a1, a2, a3, a4);
return epc;
}
Expression funAppn() throws SyntaxException
{
Token first = t;
Token name = func();
if(isKind(LPAREN)){
match(LPAREN);
Expression ex = expression();
match(RPAREN);
return new ExpressionFunctionAppWithExpressionArg(first, name, ex);
}
else if(isKind(LSQUARE)){
match(LSQUARE);
Expression ex2 = expression();
match(COMMA);
Expression ex3 =expression();
match(RSQUARE);
return new ExpressionFunctionAppWithPixel(first, name, ex2, ex3);
}
else {
throw new SyntaxException(t,"Parser has erros. Invalid.");
}
}
Token func() throws SyntaxException
{ Token t = null;
if(isKind(funName))
{
for (int j=0;j<funName.length;j++)
{
if(isKind(funName[j]))
{
t = match(funName[j]);
}
}
}
else
throw new SyntaxException (t, "Function name is invalid");
return t;
}
/*
ExpressionPredefinedName predefinedName() throws SyntaxException
{ Token first = t;
ExpressionPredefinedName ep =null;
for (int j=0;j<predefName.length;j++)
{
if(isKind(predefName[j]))
{
consume();
}
}
ep = new ExpressionPredefinedName (first, t);
return ep;
}
*/
protected boolean isKind(Kind kind) {
return t.kind == kind;
}
protected boolean isKind(Kind... kinds) {
for (Kind k : kinds) {
if (k == t.kind)
return true;
}
return false;
}
/**
* Precondition: kind != EOF
*
* @param kind
* @return
* @throws SyntaxException
*/
private Token match(Kind kind) throws SyntaxException {
Token tmp = t;
if (isKind(kind)) {
consume();
return tmp;
}
throw new SyntaxException(t,"Token Mismatch error at "+ t.kind); //TODO give a better error message!
}
private Token consume() throws SyntaxException {
Token tmp = t;
if (isKind( EOF)) {
throw new SyntaxException(t,"Syntax Error. EOF checking incorrect "); //TODO give a better error message!
//Note that EOF should be matched by the matchEOF method which is called only in parse().
//Anywhere else is an error. */
}
t = scanner.nextToken();
return tmp;
}
/**
* Only for check at end of program. Does not "consume" EOF so no attempt to get
* nonexistent next Token.
*
* @return
* @throws SyntaxException
*/
private Token matchEOF() throws SyntaxException {
if (isKind(EOF)) {
return t;
}
throw new SyntaxException(t,"EOF not encountered."); //TODO give a better error message!
}
}