This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokens.cup
706 lines (673 loc) · 29.1 KB
/
tokens.cup
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
import java_cup.runtime.*;
terminal READ;
terminal PRINT;
terminal PRINTLINE;
terminal TIMES;
terminal DOUBLE_PLUS;
terminal DOUBLE_MINUS;
terminal PLUS;
terminal MINUS;
terminal DIVIDE;
terminal SEMI;
terminal ASSMNT;
terminal CLASS;
terminal DOUBLE_AND;
terminal DOUBLE_OR;
terminal ELSE;
terminal IF;
terminal WHILE;
terminal RETURN;
terminal VOID;
terminal LEFT_PARENTHESIS;
terminal RIGHT_PARENTHESIS;
terminal LEFT_SQUARE_PARENTHESIS;
terminal RIGHT_SQUARE_PARENTHESIS;
terminal LEFT_BRACKET;
terminal RIGHT_BRACKET;
terminal LESS_THAN;
terminal GREATER_THAN;
terminal LESS_THAN_OR_EQUAL_TO;
terminal GREATER_THAN_OR_EQUAL_TO;
terminal EQUAL_TO;
terminal NOT_EQUAL_TO;
terminal NOT;
terminal UPLUS;
terminal UMINUS;
terminal TERNARY_IF;
terminal TERNARY_ELSE;
terminal PARAMETER_SEPARATOR;
terminal FINAL;
terminal INT_PRIMITIVE;
terminal CHAR_PRIMITIVE;
terminal BOOL_PRIMITIVE;
terminal FLOAT_PRIMITIVE;
terminal String VAR;
terminal String ID;
terminal int INTLIT;
terminal String CHARLIT;
terminal String STRLIT;
terminal double FLOATLIT;
terminal boolean BOOL_TRUE;
terminal boolean BOOL_FALSE;
nonterminal tokens.Program program;
nonterminal tokens.MemberDecls memberdecls;
nonterminal tokens.fields.FieldDecls fielddecls;
nonterminal tokens.methods.MethodDecls methoddecls;
nonterminal tokens.methods.args.argdecls.ArgDecls argdecls;
nonterminal tokens.stmts.Stmts stmts;
nonterminal tokens.methods.args.Args args;
nonterminal tokens.methods.args.ReadList readlist;
nonterminal tokens.methods.args.PrintList printlist;
nonterminal tokens.methods.args.PrintLineList printlinelist;
nonterminal tokens.methods.args.argdecls.ArgDeclList argdecllist;
nonterminal tokens.fields.FieldDecl fielddecl;
nonterminal tokens.methods.MethodDecl methoddecl;
nonterminal tokens.methods.args.argdecls.ArgDecl argdecl;
nonterminal tokens.stmts.Stmt stmt;
nonterminal tokens.expr.OptionalExpr optionalexpr;
nonterminal tokens.lexeme.OptionalLexeme optionalsemi;
nonterminal tokens.methods.ReturnType returntype;
nonterminal tokens.lexeme.Type type;
nonterminal tokens.id.Name name;
nonterminal tokens.expr.Expr expr;
nonterminal tokens.expr.BinaryOp binaryop;
nonterminal tokens.id.TypeId typeid;
precedence right ASSMNT;
precedence right TERNARY_IF, TERNARY_ELSE;
precedence left DOUBLE_OR;
precedence left DOUBLE_AND;
precedence left EQUAL_TO, NOT_EQUAL_TO;
precedence nonassoc LESS_THAN, LESS_THAN_OR_EQUAL_TO, GREATER_THAN, GREATER_THAN_OR_EQUAL_TO;
precedence left PLUS, MINUS;
precedence left TIMES, DIVIDE;
precedence right NOT, UPLUS, UMINUS;
precedence nonassoc DOUBLE_PLUS, DOUBLE_MINUS;
precedence left LEFT_SQUARE_PARENTHESIS, RIGHT_SQUARE_PARENTHESIS, LEFT_PARENTHESIS, RIGHT_PARENTHESIS;
precedence left ELSE;
program ::= CLASS ID:id LEFT_BRACKET memberdecls:memberDecls RIGHT_BRACKET
{:
RESULT = tokens.Program.builder()
.id(id)
.memberDecls(memberDecls)
.build();
:};
memberdecls ::= fielddecls:fieldDecls methoddecls:methodDecls
{:
RESULT = tokens.MemberDecls.builder()
.fieldDecls(fieldDecls)
.methodDecls(methodDecls)
.build();
:}
| fielddecls:fieldDecls
{:
RESULT = tokens.MemberDecls.builder()
.fieldDecls(fieldDecls)
.build();
:}
| methoddecls:methodDecls
{:
RESULT = tokens.MemberDecls.builder()
.methodDecls(methodDecls)
.build();
:}
|
{:
RESULT = tokens.MemberDecls.builder().build();
:};
fielddecls ::= fielddecls:fieldDecls fielddecl:fieldDecl
{:
RESULT = tokens.fields.FieldDecls.builder()
.fieldDecl(fieldDecl)
.fieldDecls(fieldDecls)
.build();
:}
| fielddecl:fieldDecl
{:
RESULT = tokens.fields.FieldDecls.builder()
.fieldDecl(fieldDecl)
.build();
:};
methoddecls ::= methoddecls:methodDecls methoddecl:methodDecl
{:
RESULT = tokens.methods.MethodDecls.builder()
.methodDecl(methodDecl)
.methodDecls(methodDecls)
.build();
:}
| methoddecl:methodDecl
{:
RESULT = tokens.methods.MethodDecls.builder()
.methodDecl(methodDecl)
.build();
:};
fielddecl ::= FINAL typeid:typeId optionalexpr:optionalExpr SEMI
{:
RESULT = tokens.fields.ExpressionFieldDecl.builder()
.type(typeId.getType())
.id(typeId.getId())
.optionalFinal(tokens.lexeme.OptionalLexeme.shown(tokens.lexeme.Lexeme.of("final")))
.optionalExpr(optionalExpr)
.build();
:}
| typeid:typeId optionalexpr:optionalExpr SEMI
{:
RESULT = tokens.fields.ExpressionFieldDecl.builder()
.type(typeId.getType())
.id(typeId.getId())
.optionalFinal(tokens.lexeme.OptionalLexeme.hidden())
.optionalExpr(optionalExpr)
.build();
:}
| typeid:typeId LEFT_SQUARE_PARENTHESIS INTLIT:size RIGHT_SQUARE_PARENTHESIS SEMI
{:
RESULT = tokens.fields.ArrayFieldDecl.builder()
.type(typeId.getType())
.id(typeId.getId())
.size(size)
.build();
:};
optionalexpr ::= ASSMNT expr:expr
{:
RESULT = tokens.expr.OptionalExpr.shownExpr(expr);
:}
|
{:
RESULT = tokens.expr.OptionalExpr.hidden();
:};
methoddecl ::= typeid:typeId LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.argDeclList(argDeclList)
.fieldDecls(fieldDecls)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.argDeclList(argDeclList)
.fieldDecls(fieldDecls)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.argDeclList(argDeclList)
.fieldDecls(fieldDecls)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.argDeclList(argDeclList)
.fieldDecls(fieldDecls)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.fieldDecls(fieldDecls)
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.fieldDecls(fieldDecls)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.fieldDecls(fieldDecls)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
fielddecls:fieldDecls stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.fieldDecls(fieldDecls)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.argDeclList(argDeclList)
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.argDeclList(argDeclList)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.argDeclList(argDeclList)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS argdecllist:argDeclList RIGHT_PARENTHESIS LEFT_BRACKET
stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.argDeclList(argDeclList)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.optionalSemi(optionalSemi)
.build();
:}
| typeid:typeId LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withType(typeId.getType()))
.id(typeId.getId())
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.optionalSemi(optionalSemi)
.build();
:}
| VOID ID:id LEFT_PARENTHESIS RIGHT_PARENTHESIS LEFT_BRACKET
stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.methods.MethodDecl.builder()
.returnType(tokens.methods.ReturnType.withoutType())
.id(id)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:};
optionalsemi ::= SEMI
{:
RESULT = tokens.lexeme.OptionalLexeme.shown(tokens.lexeme.Lexeme.of(";"));
:}
|
{:
RESULT = tokens.lexeme.OptionalLexeme.hidden();
:};
type ::= INT_PRIMITIVE
{:
RESULT = tokens.lexeme.Types.INTLIT;
:}
| CHAR_PRIMITIVE
{:
RESULT = tokens.lexeme.Types.CHARLIT;
:}
| BOOL_PRIMITIVE
{:
RESULT = tokens.lexeme.Types.BOOLLIT;
:}
| FLOAT_PRIMITIVE
{:
RESULT = tokens.lexeme.Types.FLOATLIT;
:};
argdecllist ::= argdecl:argDecl PARAMETER_SEPARATOR argdecllist:argDeclList
{:
RESULT = tokens.methods.args.argdecls.ArgDeclList.builder()
.argDecl(argDecl)
.argDeclList(argDeclList)
.build();
:}
| argdecl:argDecl
{:
RESULT = tokens.methods.args.argdecls.ArgDeclList.builder()
.argDecl(argDecl)
.build();
:};
argdecl ::= typeid:typeId
{:
RESULT = tokens.methods.args.argdecls.RegularArgDecl.builder()
.type(typeId.getType())
.id(typeId.getId())
.build();
:}
| typeid:typeId LEFT_SQUARE_PARENTHESIS RIGHT_SQUARE_PARENTHESIS
{:
RESULT = tokens.methods.args.argdecls.ArrayArgDecl.builder()
.type(typeId.getType())
.id(typeId.getId())
.build();
:};
stmts ::= stmts:stmts stmt:stmt
{:
RESULT = tokens.stmts.Stmts.builder()
.stmt(stmt)
.stmts(stmts)
.build();
:}
| stmt:stmt
{:
RESULT = tokens.stmts.Stmts.builder()
.stmt(stmt)
.build();
:};
stmt ::= IF LEFT_PARENTHESIS expr:expr RIGHT_PARENTHESIS stmt:stmt
{:
RESULT = tokens.stmts.IfStmt.builder()
.expr(expr)
.stmt(stmt)
.ifEnd(tokens.stmts.IfEnd.end())
.build();
:}
| IF LEFT_PARENTHESIS expr:expr RIGHT_PARENTHESIS stmt:ifStmt ELSE stmt:elseStmt
{:
RESULT = tokens.stmts.IfStmt.builder()
.expr(expr)
.stmt(ifStmt)
.ifEnd(tokens.stmts.IfEnd.withStmt(elseStmt))
.build();
:}
| WHILE LEFT_PARENTHESIS expr:expr RIGHT_PARENTHESIS stmt:stmt
{:
RESULT = tokens.stmts.WhileStmt.builder()
.expr(expr)
.stmt(stmt)
.build();
:}
| name:name ASSMNT expr:expr SEMI
{:
RESULT = tokens.stmts.ReassignStmt.builder()
.name(name)
.expr(expr)
.build();
:}
| READ LEFT_PARENTHESIS readlist:readList RIGHT_PARENTHESIS SEMI
{:
RESULT = tokens.stmts.ReadStmt.of(readList);
:}
| PRINT LEFT_PARENTHESIS printlist:printList RIGHT_PARENTHESIS SEMI
{:
RESULT = tokens.stmts.PrintStmt.of(printList);
:}
| PRINTLINE LEFT_PARENTHESIS printlinelist:printLineList RIGHT_PARENTHESIS SEMI
{:
RESULT = tokens.stmts.PrintLineStmt.of(printLineList);
:}
| ID:id LEFT_PARENTHESIS RIGHT_PARENTHESIS SEMI
{:
RESULT = tokens.stmts.MethodInvocationStmt.blank(id);
:}
| ID:id LEFT_PARENTHESIS args:args RIGHT_PARENTHESIS SEMI
{:
RESULT = tokens.stmts.MethodInvocationStmt.withArgs(id, args);
:}
| RETURN SEMI
{:
RESULT = tokens.stmts.ReturnStmt.blank();
:}
| RETURN expr:expr SEMI
{:
RESULT = tokens.stmts.ReturnStmt.withExpr(expr);
:}
| name:name DOUBLE_PLUS SEMI
{:
RESULT = tokens.stmts.IncrementStmt.withName(name);
:}
| name:name DOUBLE_MINUS SEMI
{:
RESULT = tokens.stmts.DecrementStmt.withName(name);
:}
| LEFT_BRACKET fielddecls:fieldDecls stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.stmts.BodyStmt.builder()
.fieldDecls(fieldDecls)
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| LEFT_BRACKET fielddecls:fieldDecls RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.stmts.BodyStmt.builder()
.fieldDecls(fieldDecls)
.optionalSemi(optionalSemi)
.build();
:}
| LEFT_BRACKET stmts:stmts RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.stmts.BodyStmt.builder()
.stmts(stmts)
.optionalSemi(optionalSemi)
.build();
:}
| LEFT_BRACKET RIGHT_BRACKET optionalsemi:optionalSemi
{:
RESULT = tokens.stmts.BodyStmt.builder()
.optionalSemi(optionalSemi)
.build();
:};
name ::= ID:id
{:
RESULT = tokens.id.Name.simple(id);
:}
| ID:id LEFT_SQUARE_PARENTHESIS expr:expr RIGHT_SQUARE_PARENTHESIS
{:
RESULT = tokens.id.Name.array(id, expr);
:};
args ::= expr:expr PARAMETER_SEPARATOR args:args
{:
RESULT = tokens.methods.args.Args.builder()
.expr(expr)
.args(args)
.build();
:}
| expr:expr
{:
RESULT = tokens.methods.args.Args.builder()
.expr(expr)
.build();
:};
readlist ::= name:name PARAMETER_SEPARATOR readlist:readlist
{:
RESULT = tokens.methods.args.ReadList.builder()
.name(name)
.readList(readlist)
.build();
:}
| name:name
{:
RESULT = tokens.methods.args.ReadList.builder()
.name(name)
.build();
:};
printlist ::= expr:expr PARAMETER_SEPARATOR printlist:printList
{:
RESULT = tokens.methods.args.PrintList.builder()
.expr(expr)
.printList(printList)
.build();
:}
| expr:expr
{:
RESULT = tokens.methods.args.PrintList.builder()
.expr(expr)
.build();
:};
printlinelist ::= printlist:printList
{:
RESULT = tokens.methods.args.PrintLineList.withPrintList(printList);
:}
|
{:
RESULT = tokens.methods.args.PrintLineList.empty();
:};
expr ::= name:name
{:
RESULT = tokens.expr.Expr.simple(name);
:}
| ID:id LEFT_PARENTHESIS RIGHT_PARENTHESIS
{:
RESULT = tokens.expr.Expr.simpleInvocation(id);
:}
| ID:id LEFT_PARENTHESIS args:args RIGHT_PARENTHESIS
{:
RESULT = tokens.expr.Expr.invocation(id, args);
:}
| INTLIT:intlit
{:
RESULT = tokens.expr.Expr.intlit(intlit);
:}
| CHARLIT:charlit
{:
RESULT = tokens.expr.Expr.charlit(charlit);
:}
| STRLIT:strlit
{:
RESULT = tokens.expr.Expr.strlit(strlit);
:}
| FLOATLIT:floatlit
{:
RESULT = tokens.expr.Expr.floatlit(floatlit);
:}
| BOOL_TRUE
{:
RESULT = tokens.expr.Expr.boollit(true);
:}
| BOOL_FALSE
{:
RESULT = tokens.expr.Expr.boollit(false);
:}
| LEFT_PARENTHESIS expr:expr RIGHT_PARENTHESIS
{:
RESULT = tokens.expr.Expr.parenthesized(expr);
:}
| NOT expr:expr
{:
RESULT = tokens.expr.Expr.not(expr);
:}
| MINUS expr:expr
{:
RESULT = tokens.expr.Expr.minus(expr);
:} %prec UMINUS
| PLUS expr:expr
{:
RESULT = tokens.expr.Expr.plus(expr);
:} %prec UPLUS
| LEFT_PARENTHESIS type:type RIGHT_PARENTHESIS expr:expr
{:
RESULT = tokens.expr.Expr.casting(type, expr);
:}
| binaryop:binaryOp
{:
RESULT = tokens.expr.Expr.binaryOp(binaryOp);
:}
| LEFT_PARENTHESIS expr:expr TERNARY_IF expr:exprTrue TERNARY_ELSE expr:exprFalse RIGHT_PARENTHESIS
{:
RESULT = tokens.expr.Expr.ternary(expr, exprTrue, exprFalse);
:};
binaryop ::= expr:expr1 TIMES expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "*", expr2);
:}
| expr:expr1 DIVIDE expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "/", expr2);
:}
| expr:expr1 PLUS expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "+", expr2);
:}
| expr:expr1 MINUS expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "-", expr2);
:}
| expr:expr1 LESS_THAN expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "<", expr2);
:}
| expr:expr1 GREATER_THAN expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, ">", expr2);
:}
| expr:expr1 LESS_THAN_OR_EQUAL_TO expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "<=", expr2);
:}
| expr:expr1 GREATER_THAN_OR_EQUAL_TO expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, ">=", expr2);
:}
| expr:expr1 EQUAL_TO expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "==", expr2);
:}
| expr:expr1 NOT_EQUAL_TO expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "<>", expr2);
:}
| expr:expr1 DOUBLE_OR expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "||", expr2);
:}
| expr:expr1 DOUBLE_AND expr:expr2
{:
RESULT = tokens.expr.BinaryOp.of(expr1, "&&", expr2);
:};
typeid ::= type:type ID:id
{:
RESULT = tokens.id.TypeId.of(type, id);
:};