-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.cup
798 lines (711 loc) · 24.3 KB
/
parser.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
package pasito.syntax;
import java.io.FileInputStream;
import java.util.HashMap;
import java.io.IOException;
import java.nio.file.Paths;
import java_cup.runtime.ComplexSymbolFactory;
import java.io.File;
import java.util.*;
//Importes das classses abstratas
import pasito.ast.Program;
import pasito.ast.declaration.*;
import pasito.ast.expression.*;
import pasito.ast.methodSpecOrInterfaceName.*;
import pasito.ast.signature.*;
import pasito.ast.statement.*;
import pasito.ast.topLevelDecl.*;
import pasito.ast.type.*;
import pasito.ast.element.*;
import pasito.util.*;
parser code {:
protected Lexer lexer;
ErrorRegister ERRO = new ErrorRegister();
:}
/* define how to connect to the scanner! */
init with {:
ComplexSymbolFactory f = new ComplexSymbolFactory();
symbolFactory = f;
File file = new File("input.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
lexer = new Lexer(f,fis);
:};
scan with {: return lexer.next_token(); :};
/* @@@@@@@@@@@@ terminais (tokens retornados pelo analisador léxico, utilizados para criar classe Symbol) @@@@@@@@@@@@ */
terminal String ID;
terminal DEFAULT, FUNC, INTERFACE,FALLTHROUGH, CASE, STRUCT, TYPE, ELSE, SWITCH, IF, CONST, VAR;
terminal FOR, RETURN, RANGE, BOOLEAN;
terminal Boolean TRUE, FALSE;
terminal INT32, FLOAT64, DIGIT;
terminal Integer INT_NUMBER ;
terminal Float FLOAT_NUMBER;
terminal PLUS, MINUS, MULT, DIV, AND, EQ, LT;
terminal NOT;
terminal LPAR, RPAR, LSBRACK, RSBRACK, LBRACK,
RBRACK, ASSIGN, DASSIGN, COMMA, SEMICOLON, DOTDOTDOT, COLON, DOT;
non terminal Program Program;
non terminal List<TopLevelDecl> TopLevelDecl;
non terminal List<Declaration> Declaration;
non terminal FunctionDecl FunctionDecl;
non terminal MethodDecl MethodDecl;
non terminal List<ConstDecl> ConstDecl;
non terminal List<VarDecl> VarDecl;
non terminal List<TypeDecl> TypeDecl;
non terminal Type Type;
non terminal List<TypeDecl> TypeSpecList;
non terminal Type TypeLit;
non terminal ArrayType ArrayType;
non terminal StructType StructType;
non terminal SliceType SliceType;
non terminal FieldDecl FieldDecl;
non terminal List<FieldDecl> FieldDeclList;
non terminal List<MethodSpec> MethodSpecList;
non terminal MethodSpec MethodSpec;
non terminal Signature Signature;
non terminal Pair<List<FormalParameter>, Pair<String,Type>> Parameters, Resul, ParameterList, ParameterDeclList;
non terminal List <Type> TypeList, Types;
non terminal List<String> IdentifierList;
non terminal Expression Expression;
non terminal List<Expression> ExpressionList;
non terminal CompExpression, AddExpression, MultExpression;
non terminal MethodExpr;
non terminal Expression UnaryExpr;
non terminal UnaryOperator unary_op;
non terminal Expression PrimaryExpr;
non terminal Expression Slice;
non terminal Pair<List<Expression>, Boolean> Arguments;
non terminal Boolean VariadicCommaOp;
non terminal Expression Operand;
non terminal CompositeLit CompositeLit;
non terminal FunctionDecl Function;
non terminal Type LiteralType;
non terminal LiteralElement LiteralValue;
non terminal TypeName ReceiverType;
non terminal Element Element;
non terminal List<KeyedElement> ElementList;
non terminal KeyedElement KeyedElement;
non terminal VarDecl VarSpec;
non terminal List<VarDecl> VarSpecList;
non terminal Block Block;
non terminal Statement Statement;
non terminal List<Statement> StatementList;
non terminal Statement SimpleStmt;
non terminal Statement ForStmt;
non terminal Statement IfStmt;
non terminal Block ElseOpt;
/* Precedences */
precedence left AND;
precedence left LT, EQ;
precedence left PLUS, MINUS;
precedence left MULT, DIV;
Program ::= TopLevelDecl : topDecs SEMICOLON Program : prog
{: prog.declarations.addAll(0,topDecs);
RESULT = prog; :}
| {: RESULT = new Program(new LinkedList<>()); :}
;
TopLevelDecl ::= Declaration : decl
{:
List<TopLevelDecl> tld = new LinkedList<>();
for (Declaration d : decl)
tld.add(new Dec(d));
RESULT = tld;
:}
| FunctionDecl : func
{:
List<TopLevelDecl> tld = new LinkedList<>();
tld.add(func);
RESULT = tld;
:}
| MethodDecl : method
{:
List<TopLevelDecl> tld = new LinkedList<>();
tld.add(method);
RESULT = tld;
:}
;
Declaration ::= ConstDecl : constDecl
{:
List<Declaration> dec = new LinkedList<>();
dec.addAll(constDecl);
RESULT = dec;
:}
| VarDecl : varDecl
{:
List<Declaration> dec = new LinkedList<>();
dec.addAll(varDecl);
RESULT = dec;
:}
| TypeDecl : typeDecl
{:
List<Declaration> dec = new LinkedList<>();
dec.addAll(typeDecl);
RESULT = dec;
:}
;
TypeDecl ::= TYPE ID:nm Type :type
{:
List<TypeDecl> td = new LinkedList<>();
td.add(new TypeDecl(nm, type));
RESULT = td;
:}
| TYPE LPAR TypeSpecList : typeDecls RPAR
{:
RESULT = typeDecls;
:}
;
TypeSpecList ::= ID:nm Type : type SEMICOLON TypeSpecList : tyDecs
{:
tyDecs.add(0, new TypeDecl(nm, type));
RESULT = tyDecs;
:}
| {: RESULT = new LinkedList<>(); :}
;
Type ::= ID : id {: RESULT = new TypeName(id); :}
| TypeLit : Tlit
{:
RESULT = Tlit;
:}
| BOOLEAN {: RESULT = new TypeName("bool"); :}
| INT32 {: RESULT = new TypeName("int"); :}
| FLOAT64 {: RESULT = new TypeName("float"); :}
;
TypeLit ::= ArrayType : array
{: RESULT = array; :}
| StructType : struct
{: RESULT = struct; :}
| MULT Type : type
{: RESULT = new PointerType(type); :}
| INTERFACE LBRACK MethodSpecList : methods RBRACK
{:
List<MethodSpecOrInterfaceName> met = new LinkedList<>();
met.addAll(methods);
RESULT = new InterfaceType(met);
:}
| SliceType : slice {: RESULT = slice; :}
;
StructType ::= STRUCT LBRACK FieldDeclList : fieldDec RBRACK
{: RESULT = new StructType(fieldDec); :};
ArrayType ::= LSBRACK Expression: exp RSBRACK Type : type
{:
RESULT = new ArrayType(exp, type);
:}
;
SliceType ::= LSBRACK RSBRACK Type : type
{: RESULT = new SliceType(type); :} ;
FieldDeclList ::= FieldDecl : fieldecl SEMICOLON FieldDeclList : fieldeclList
{:
fieldeclList.add(0,fieldecl);
RESULT = fieldeclList;
:}
| {: RESULT = new LinkedList<>(); :};
FieldDecl ::= ID : name Type : type {: RESULT = new FieldDecl(name,type); :}
| ID : name {: RESULT = new FieldDecl(name,null); :}
| MULT ID: name {: RESULT = new FieldDecl(name,null); :} ;
MethodSpecList ::= MethodSpec : method SEMICOLON MethodSpecList : listMethod
{:
listMethod.add(0,method);
RESULT = listMethod;
:}
| {: RESULT = new LinkedList<>(); :};
MethodSpec ::= ID :id Signature : sig
{: RESULT = new MethodSpec(id,sig); :}
| ID :id
{: RESULT = new MethodSpec(id,null); :}
;
Signature ::= Parameters : par
{:
FormalParameter fp;
if(par.snd != null){
fp = new FormalParameter(par.snd.fst,par.snd.snd);
RESULT = new Signature(par.fst, fp, null);
} else{
System.out.println("FormalParameter: "+ par.fst.get(0).name);
RESULT = new Signature(par.fst, null, null);
}
:}
| Parameters : par Resul : res
{:
FormalParameter fp;
if(par.snd != null){
fp = new FormalParameter(par.snd.fst,par.snd.snd);
RESULT = new Signature(par.fst, fp, res.fst);
}else{
System.out.println("FormalParameter: "+ par.fst.get(0).name);
RESULT = new Signature(par.fst, null, res.fst);
}
:}
;
Resul ::= Parameters : par
{: RESULT = par; :}
| Type : type
{: List<FormalParameter> p = new LinkedList<>();
p.add(new FormalParameter(null,type));
RESULT = new Pair<>(p,null); :}
;
Parameters ::= LPAR RPAR {: RESULT = new Pair<>(null,new Pair(null,null)); :}
| LPAR ParameterList : parList RPAR {: RESULT = parList; :}
| LPAR TypeList : typeList RPAR
{: List<FormalParameter> p = new LinkedList<>();
for(Type t : typeList)
p.add(new FormalParameter(null,t));
RESULT = new Pair<>(p,null);
:} ;
ParameterList ::= ParameterDeclList : parDec
{: RESULT = parDec;
:}
| ID:nm DOTDOTDOT Type :type
{:
RESULT = new Pair<>(null,new Pair(nm,type));
:}
| ID:nm DOTDOTDOT Type :type COMMA
{: RESULT = new Pair<>(null,new Pair(nm,type));
:}
;
ParameterDeclList ::= IdentifierList : lname Type : type COMMA ParameterDeclList : parDecList
{:
List<FormalParameter> p = new LinkedList<>();
for(String s : lname)
p.add(new FormalParameter(s,type));
parDecList.fst.addAll(0,p);
RESULT = parDecList;
:}
| IdentifierList: lname Type : type COMMA
{:
List<FormalParameter> p = new LinkedList<>();
for(String s : lname)
p.add(new FormalParameter(s,type));
RESULT = new Pair<>(p, null);
:}
| IdentifierList : lname Type : type
{:
List<FormalParameter> p = new LinkedList<>();
for(String s : lname)
p.add(new FormalParameter(s,type));
RESULT = new Pair<>(p, null);
:}
| IdentifierList : lname Type : type COMMA ID : id DOTDOTDOT Type : tp COMMA
{:
List<FormalParameter> p = new LinkedList<>();
for(String s : lname)
p.add(new FormalParameter(s,type));
RESULT = new Pair<>(p, new Pair<>(id, tp));
:}
| IdentifierList : lname Type : type COMMA ID : id DOTDOTDOT Type : tp
{:
List<FormalParameter> p = new LinkedList<>();
for(String s : lname)
p.add(new FormalParameter(s,type));
RESULT = new Pair<>(p, new Pair<>(id, tp));
:}
;
TypeList ::= Types : types
{:RESULT = types; :}
| DOTDOTDOT Type: type COMMA
{: List<Type> tp = new LinkedList<>();
tp.add(type);
RESULT = tp;
:}
| DOTDOTDOT Type : type
{: List<Type> tp = new LinkedList<>();
tp.add(type);
RESULT = tp;
:}
;
Types ::= ID COMMA Types : types
{:RESULT = types; :}
| ID COMMA DOTDOTDOT Type : type COMMA
{: List<Type> tp = new LinkedList<>();
tp.add(type);
RESULT = tp;
:}
| ID COMMA DOTDOTDOT Type : type
{: List<Type> tp = new LinkedList<>();
tp.add(type);
RESULT = tp;
:}
| ID COMMA {: RESULT = new LinkedList<>(); :}
| ID {: RESULT = new LinkedList<>(); :}
| TypeLit : typeLit COMMA Types : types
{: types.add(0,typeLit);
RESULT = types;
:}
| TypeLit :tpl COMMA DOTDOTDOT Type:type COMMA
{: List<Type> tp = new LinkedList<>();
tp.add(tpl);
tp.add(type);
RESULT = tp;
:}
| TypeLit :tpl COMMA DOTDOTDOT Type:type
{: List<Type> tp = new LinkedList<>();
tp.add(tpl);
tp.add(type);
RESULT = tp;
:}
| TypeLit :tpl COMMA
{: List<Type> tp = new LinkedList<>();
tp.add(tpl);
RESULT = tp;
:}
| TypeLit :tpl
{: List<Type> tp = new LinkedList<>();
tp.add(tpl);
RESULT = tp;
:}
;
IdentifierList ::= ID:nm COMMA IdentifierList : ids
{: ids.add(0,nm);
RESULT = ids;
:}
| ID : nm
{:
List<String> name = new LinkedList<>();
name.add(nm);
RESULT = name;
:}
;
ConstDecl ::= CONST IdentifierList : ids
{: List<ConstDecl> constDecls = new LinkedList<>();
for (String id: ids)
constDecls.add(new ConstDecl(id, null, null));
RESULT = constDecls;
:}
| CONST IdentifierList : ids ASSIGN ExpressionList : exps
{:
List<ConstDecl> constDecls = new LinkedList<>();
Iterator<String> idIt = ids.iterator();
Iterator<Expression> expIt = exps.iterator();
while ( idIt.hasNext() && expIt.hasNext() )
constDecls.add( new ConstDecl(idIt.next(), null, expIt.next()));
if (idIt.hasNext() || expIt.hasNext())
ERRO.report("erro na declaração de constantes");
RESULT = constDecls;
:}
| CONST IdentifierList : ids Type: type ASSIGN ExpressionList : exps
{:
List<ConstDecl> constDecls = new LinkedList<>();
Iterator<String> idIt = ids.iterator();
Iterator<Expression> expIt = exps.iterator();
while ( idIt.hasNext() && expIt.hasNext() )
constDecls.add( new ConstDecl(idIt.next(), type, expIt.next()));
if (idIt.hasNext() || expIt.hasNext())
ERRO.report("erro na declaração de constantes");
RESULT = constDecls;
:}
;
ExpressionList ::= ExpressionList : exps COMMA Expression : exp
{: exps.add(exp);
RESULT = exps;
:}
| Expression : exp
{:
List<Expression> ex = new LinkedList<>();
ex.add(exp);
RESULT = ex;
:}
;
Expression ::= Expression : exp1 PLUS Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.PLUS,exp1,exp2); :}
| Expression : exp1 MINUS Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.MINUS,exp1,exp2); :}
| Expression : exp1 MULT Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.MULT,exp1,exp2); :}
| Expression : exp1 DIV Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.DIV,exp1,exp2); :}
| Expression : exp1 LT Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.LT,exp1,exp2); :}
| Expression : exp1 EQ Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.EQ,exp1,exp2); :}
| Expression : exp1 AND Expression : exp2
{: RESULT = new BinaryExpression(BinaryOperator.AND,exp1,exp2); :}
| UnaryExpr : uexp
{:RESULT = uexp; :}
;
UnaryExpr ::= PrimaryExpr : pExp
{: RESULT = pExp; // new UnaryExpression(null,pexp); :}
| unary_op :uop UnaryExpr: uexp
{:
RESULT = new UnaryExpression(uop, uexp);
:}
;
unary_op ::= PLUS {: RESULT = UnaryOperator.PLUS; :} | MINUS {: RESULT = UnaryOperator.MINUS; :} | MULT {: RESULT = UnaryOperator.MULT; :} | NOT {: RESULT = UnaryOperator.NOT; :} ;
PrimaryExpr ::= Operand : op {: RESULT = op; :}
| PrimaryExpr : pexp DOT ID : id {:
RESULT = new SelectorExpression(pexp,id);
:}
| PrimaryExpr : uexp LSBRACK Expression : exp RSBRACK
{: IndexExpression index = new IndexExpression(uexp,exp);
RESULT = index;
:}
| PrimaryExpr : exp LSBRACK Slice : slicexp RSBRACK
{: //slicexp.exp = exp; Verificar se é um SliceExpress ou um FullSliceExpress?
if(slicexp instanceof SliceExpression) {
SliceExpression sSlice = (SliceExpression) slicexp;
sSlice.exp = exp;
RESULT = sSlice;
}
if(slicexp instanceof FullSliceExpression) {
FullSliceExpression fSlice = (FullSliceExpression) slicexp;
fSlice.exp = exp;
RESULT = fSlice;
}
:}
| PrimaryExpr : exp LPAR Arguments : arg RPAR // função com argumentos com ou sem variadic
{:
if (arg.snd){
LinkedList<Expression> variList = new LinkedList<Expression>();
variList.addAll(arg.fst);
Expression e = variList.getLast();
variList.removeLast();
RESULT = new CallExpression(exp,variList,e);
}else
RESULT = new CallExpression(exp,arg.fst,null);
:}
| PrimaryExpr : exp LPAR RPAR {: RESULT = exp; :};
Slice ::= Expression : exp1 COLON Expression : exp2
{: RESULT = new SliceExpression(null,exp1,exp2); :}
| Expression : exp1 COLON
{: RESULT = new SliceExpression(null,exp1,null); :}
| COLON Expression : exp2
{: RESULT = new SliceExpression(null,null,exp2); :}
| COLON
{: RESULT = new SliceExpression(null,null,null); :}
| Expression : exp1 COLON Expression : exp2 COLON Expression : exp3
{: RESULT = new FullSliceExpression(null,exp1,exp2,exp3); :}
| COLON Expression : exp2 COLON Expression : exp3
{: RESULT = new FullSliceExpression(null,null,exp2,exp3); :} ;
Arguments ::= ExpressionList:exps VariadicCommaOp: variadic
{:
RESULT = new Pair<>(exps,variadic);;
:};
/*
| TypeLit1 COMMA ExpressionList VariadicCommaOp // with Type is Ambbiguous
| TypeLit1 VariadicCommaOp
| MULT Type COMMA ExpressionList VariadicCommaOp // with Type is Ambbiguous
| MULT Type VariadicCommaOp; //with Type is ambiguous
//btw, Type is correct here? The documentation says nothing about this. */
/**** TIRAMOS Type como primeiro argumento ***********/
VariadicCommaOp ::= DOTDOTDOT {:RESULT = true;:}
| COMMA {:RESULT = false;:}
| DOTDOTDOT COMMA {:RESULT = true;:}
| {:RESULT = false;:};
Operand ::= INT_NUMBER : inte {: RESULT = new IntLiteral(inte); :}
| FLOAT_NUMBER : floa {: RESULT = new FloatLiteral(floa); :}
| TRUE : v {: RESULT = new BooleanLiteral(v); :}
| FALSE : f {: RESULT = new BooleanLiteral(f); :}
| CompositeLit : comp {: RESULT = comp; :}
| FUNC Function : func {: RESULT = new FunctionLiteral(func.sig, func.body); :}
//| MethodExpr
| LPAR Expression : exp RPAR {: RESULT = exp; :}
;
CompositeLit ::= LiteralType : Ltype LiteralValue : Lvalue
{:
RESULT = new CompositeLit(Ltype, Lvalue.keyedElems);
:}
;
LiteralType ::= StructType : struct {: RESULT = struct; :}
| ArrayType : array {: RESULT = array; :}
| LSBRACK DOTDOTDOT RSBRACK Type : type {: RESULT = type; :}
| SliceType : slice {: RESULT = slice; :}
;
LiteralValue ::= LBRACK RBRACK {: RESULT = new LiteralElement(null); :} // Rever se é do tipo LiteralElement ou Lista de KeyElement
| LBRACK ElementList : elemsLit RBRACK
{: RESULT = new LiteralElement(elemsLit); :}
;
ElementList ::= KeyedElement : keyed COMMA ElementList : listElem
{: listElem.add(0,keyed);
RESULT = listElem; :}
| KeyedElement : keyed
{: List<KeyedElement> listElem = new LinkedList<>();
listElem.add(keyed);
RESULT = listElem; :}
| KeyedElement : keyed COMMA
{: List<KeyedElement> listElem = new LinkedList<>();
listElem.add(keyed);
RESULT = listElem; :} ;
KeyedElement ::= Element : elm
{: RESULT = new KeyedElement(null, elm); :}
| Element : elm1 COLON Element : elm2
{:
if(elm1 instanceof ExpressionElement){
ExpressionElement expElem = (ExpressionElement) elm1;
RESULT = new KeyedElement(expElem.exp, elm2);
}else{
ERRO.report("Erro key Element");
RESULT = new KeyedElement(null, elm2);
} :}
;
Element ::= Expression : exp {: RESULT = new ExpressionElement(exp); :}
| LiteralValue : Lvalue {: RESULT = Lvalue; :};
/*
MethodExpr ::= ReceiverType DOT ID ;
*/
//**** MethodExpr will be solved at the static semantics
ReceiverType ::= ID : name {: RESULT = new TypeName(name); :}
| LPAR MULT ID : name LPAR {: RESULT = new TypeName(name); :}
| LPAR ReceiverType : receiver RPAR
{: RESULT = receiver; :};
VarDecl ::= VAR VarSpec : varDecl
{:
LinkedList<VarDecl> decs = new LinkedList<>();
decs.add(varDecl);
RESULT = decs;
:}
| VAR LPAR VarSpecList : varDecls RPAR
{:
RESULT = varDecls;
:}
;
VarSpecList ::= VarSpecList : varDecls1 VarSpec : varDecls2 SEMICOLON
{: varDecls1.add(varDecls2);
RESULT = varDecls1; :}
| {: RESULT = new LinkedList<>(); :}
;
VarSpec ::= IdentifierList : ids Type : type
{:
RESULT = new VarDecl(ids, type, null);
:}
| IdentifierList : ids ASSIGN ExpressionList : exps
{:
List<String> names = new LinkedList<>();
List<Expression> expressions = new LinkedList<>();
Iterator<String> idIt = ids.iterator();
Iterator<Expression> expIt = exps.iterator();
while(idIt.hasNext() && expIt.hasNext()){
names.add(idIt.next());
expressions.add(expIt.next());
}
if(idIt.hasNext() || expIt.hasNext())
System.out.println("Erro de Atribuição");
RESULT = new VarDecl(names, null, expressions);
:}
| IdentifierList :ids Type : type ASSIGN ExpressionList : exps
{:
List<String> names = new LinkedList<>();
List<Expression> expressions = new LinkedList<>();
Iterator<String> idIt = ids.iterator();
Iterator<Expression> expIt = exps.iterator();
while(idIt.hasNext() && expIt.hasNext()){
names.add(idIt.next());
expressions.add(expIt.next());
}
if(idIt.hasNext() || expIt.hasNext())
System.out.println("ERRO de Atribuição");
RESULT = new VarDecl(names, type, expressions);
:}
;
FunctionDecl ::= FUNC ID:nm Function : funDecl
{: funDecl.name = nm;
RESULT = funDecl; :} ;
Function ::= Signature : sig Block : blck
{: RESULT = new FunctionDecl(null, sig, blck); :} ;
Block ::= LBRACK StatementList : stateList RBRACK
{: RESULT = new Block(stateList); :};
StatementList ::= Statement : state SEMICOLON StatementList : stateList
{: stateList.add(0,state);
RESULT = stateList; :}
| {: RESULT = new LinkedList<>(); :} ;
MethodDecl ::= FUNC LPAR ID:id ReceiverType: type RPAR ID:nm Function: function
{:
FormalParameter fp = new FormalParameter(id,type);
RESULT = new MethodDecl(fp, nm, function.sig, function.body);
:}
;
Statement ::= Declaration : dec
{:
for( Declaration d : dec)
RESULT = new DeclarationStm(d);
:}
| SimpleStmt : simpleStm {: RESULT = simpleStm; :}
| RETURN {: RESULT = new ReturnStmt(null); :}
| RETURN ExpressionList : exps {: RESULT = new ReturnStmt(exps); :}
| Block : block {: RESULT = block; :}
| IfStmt : ifStm {: RESULT = ifStm; :}
| ForStmt : forStm {: RESULT = forStm; :} ;
SimpleStmt ::= Expression : exp
{: RESULT = new ExpressionStmt(exp); :}
// Se desejarem adicionar incrementos
| Expression: exp PLUS PLUS {: RESULT = new IncrStmt(exp, true); :}
| Expression: exp MINUS MINUS {: RESULT = new IncrStmt(exp, false); :}
| ExpressionList : exps1 ASSIGN ExpressionList : exps2
{:
RESULT = new Assignment(exps1,exps2);
:}
| /*IdentifierList*/ ExpressionList : exps1 DASSIGN ExpressionList : exps2
{:
List<String> id = new LinkedList<>();
for(Expression i : exps1){
if(i instanceof IdExpression){
IdExpression temp;
temp = (IdExpression) i;
id.add(temp.name);
}
}
RESULT = new ShortVarDecl(id,exps2);
:}
| {:RESULT = new EmptyStmt(); :}; //EmptyStmt
IfStmt ::= IF SimpleStmt : stm SEMICOLON Expression : exp Block: block ElseOpt : elseOpt
{:
if(elseOpt == null)
RESULT = new IfStmt(stm,exp, block);
else
RESULT = new IfElseStmt(stm,exp,block,elseOpt);
:}
| IF Expression : exp Block : block ElseOpt : elseOpt
{:
if(elseOpt == null)
RESULT = new IfStmt(null,exp, block);
else
RESULT = new IfElseStmt(null,exp,block,elseOpt);
:}
;
ElseOpt ::= ELSE IfStmt: ifStm
{:
List<Statement> ifs = new LinkedList<>();
ifs.add(ifStm);
Block b = new Block(ifs);
RESULT = b;
:}
| ELSE Block : block
{:
RESULT = block;
:}
| {: RESULT = null; :};
ForStmt ::= FOR Expression : exp Block : body
{:
RESULT = new ForStmt(null, exp, null, body);
:}
| FOR SimpleStmt:stm1 SEMICOLON SimpleStmt : stm2 SEMICOLON SimpleStmt :stm3 Block : body
{:
// verificar se é stm2 é uma expressao;
if(stm2 instanceof ExpressionStmt){
ExpressionStmt expStm = (ExpressionStmt) stm2;
RESULT = new ForStmt(stm1, expStm.exp, stm3, body);
}else{
System.out.println("ERRO no FOR");
RESULT = new ForStmt(stm1, null, stm3, body);
}
:}
| FOR RANGE Expression: exp Block : body
{:
RESULT = new ForRange(null,exp,body);
:}
| FOR /*IdentifierList*/ ExpressionList : expList DASSIGN RANGE Expression : exp Block : body
{:
RESULT = new ForRange(expList, exp,body);
:}
| FOR ExpressionList: expList ASSIGN RANGE Expression: exp Block: body
{:
RESULT = new ForRange(expList, exp, body);
:}
;
// ForStmt(Statement initStmt, Expression exp, Statement postStmt, Block body)
// ~~ CUP-ECLIPSE:CONFLICT-RES-RR ~~
// After this the order of productions matters
//Separated and moved by CUP Eclipse plugin
Operand ::= ID : id {: RESULT = new IdExpression(id); :};
//Separated and moved by CUP Eclipse plugin
LiteralType ::= ID : type {: RESULT = new TypeName(type); :};