-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproyecto.cup
667 lines (590 loc) · 15.6 KB
/
proyecto.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
package proyecto;
import java_cup.runtime.*;
import AST.*;
parser code {:
Scanner scanner;
public AST raiz;
public editor.EditorFrame frame;
public parser(java.io.Reader input){
scanner = new Scanner(input);
}
public String errores(int sys) {
if (sys == 2) {
return ("INT");
} else if (sys == 3) {
return ("STRING");
} else if (sys == 4) {
return ("ASIGN");
} else if (sys == 5) {
return ("MULT");
} else if (sys == 6) {
return ("SUMA");
} else if (sys == 7) {
return ("PARENi");
} else if (sys == 8) {
return ("PARENd");
} else if (sys == 9) {
return ("PUNTOyCOMA");
} else if (sys == 10) {
return ("COMA");
} else if (sys == 11) {
return ("NUM");
} else if (sys == 12) {
return ("STRING");
} else if (sys == 13) {
return ("myIDENTIFIER");
} else {
return ("error");
}
}
public void syntax_error(Symbol cur_token) {
report_error("Unexpected symbol \"" + cur_token.value.toString() + "\"", cur_token);
}
public void report_error(String message, Object info) {
StringBuilder m = new StringBuilder();
if (info instanceof java_cup.runtime.Symbol) {
// m.append("(").append(errores(((java_cup.runtime.Symbol) info).sym)).append(")");
m.append("Line ").append(((java_cup.runtime.Symbol) info).left).append(", Column ").append(((java_cup.runtime.Symbol) info).right);
if (((java_cup.runtime.Symbol) info).value != null) {
// m.append(". Lexema: ").append(((java_cup.runtime.Symbol) info).value.toString());
}
}
m.append(". ").append(message);
System.out.println(m);
if (frame != null)
frame.reportError(m.toString());
}
public void report_fatal_error(String message, Object info) {
report_error(message, info);
throw new RuntimeException("Error Fatal de Sintaxis");
}
public void report_fatal_error(String message, Object info) {
report_error(message, info);
throw new RuntimeException("Error Fatal de Sintaxis");
}
:}
non terminal AGoal aGoal;
non terminal ATypeDecl aTypeDecl;
non terminal TypeDeclaration typeDeclaration;
non terminal AMainClass mainClass;
non terminal AClassDeclaration aClassDeclaration;
non terminal AVarDecl aVarDecl;
non terminal AMethodDecl aMethodDecl;
non terminal AClassExtendsDeclaration classExtendsDeclaration;
non terminal AVarDeclaration aVarDeclaration;
non terminal AMethodDeclaration aMethodDeclaration;
non terminal AFormalParameterBlock aFormalParameterBlock;
non terminal AStatementList aStatementList;
non terminal FormalParameterList formalParameterList;
non terminal AFormalParameter aFormalParameter;
non terminal AFormalParameterRest aFormalParameterRest;
non terminal Type type;
non terminal SimpleType simpleType;
non terminal AnArrayType arrayType;
non terminal Statement statement;
non terminal ABlock aBlock;
non terminal AnAssignmentStatement anAssignmentStatement;
non terminal AnArrayAssignmentStatement anArrayAssignmentStatement;
non terminal AnIfStatement anIfStatement;
non terminal AWhileStatement aWhileStatement;
non terminal APrintStatement aPrintStatement;
non terminal Expression expression;
non terminal AnAndExpression anAndExpression;
non terminal AMenorQueExpression aMenorQueExpression;
non terminal AMayorQueExpression aMayorQueExpression;
non terminal APlusExpression aPlusExpression;
non terminal AMinusExpression aMinusExpression;
non terminal ADivExpression aDivExpression;
non terminal ATimesExpression aTimesExpression;
non terminal AnArrayLookup anArrayLookup;
non terminal AnArrayLength anArrayLength;
non terminal AMessageSend aMessageSend;
non terminal AnExpressionListBlock anExpressionListBlock;
non terminal ExpressionList expressionList;
non terminal AnExpressionRest anExpressionRest;
non terminal PrimaryExpression primaryExpression;
non terminal AnArrayAllocationExpression anArrayAllocationExpression;
non terminal AnAllocationExpression anAllocationExpression;
non terminal ANotExpression aNotExpression;
non terminal ABracketExpression aBracketExpression;
non terminal ConstantExpression constantExpression;
non terminal ASwitchStatement aSwitchStatement;
non terminal ACaseBlock aCaseBlock;
non terminal ADefaultBlock aDefaultBlock;
non terminal ACaseBlockList aCaseBlockList;
non terminal ASwitchBlock aSwitchBlock;
non terminal ABreakStatement aBreakStatement;
// tipos Symbol consulta
terminal CLASS, LLAVEi, PUBLIC, STATIC, VOID, MAIN, PARENi, STRING, CORCHETEi, CORCHETEd, PARENd, LLAVEd, EXTENDS, PUNTOyCOMA, RETURN, COMA, CHAR, BOOLEAN, INT, ASIGN, IF, ELSE, WHILE, SYSTEM_OUT_PRINTln, AND, MENOR_QUE,MAYOR_QUE, SUMA, RESTA, MULT, DIV, PUNTO, LENGHT, TRUE, FALSE, THIS, NEW, NOT, BREAK, CASE, DOSPUNTOS, SWITCH, DEFAULT ;
terminal String myIDENTIFIER;
terminal String mySTRING_LITERAL;
terminal String myCHAR_LITERAL;
terminal String myINTEGER_LITERAL;
precedence left CORCHETEi;
precedence left AND;
precedence left MENOR_QUE;
precedence left MAYOR_QUE;
precedence left SUMA;
precedence left RESTA;
precedence left MULT;
precedence left DIV;
precedence left PUNTO;
precedence left COMA;
precedence left myIDENTIFIER;
start with aGoal;
aGoal ::= mainClass:mc aTypeDecl:td
{:
parser.raiz = new AGoal(mc,td);
:};
aTypeDecl ::= aTypeDecl:td typeDeclaration:tdecl
{:
RESULT = new ATypeDecl(td,tdecl);
:}
|
{:
RESULT = null;
:}
;
mainClass ::= CLASS myIDENTIFIER:id1 LLAVEi PUBLIC STATIC VOID MAIN PARENi STRING CORCHETEi CORCHETEd myIDENTIFIER:id2 PARENd LLAVEi aPrintStatement:ps LLAVEd LLAVEd
{:
Symbol ident1 = new Symbol(sym.myIDENTIFIER,id1left,id1right,id1);
Symbol ident2 = new Symbol(sym.myIDENTIFIER,id2left,id2right,id2);
RESULT = new AMainClass(ident1, ident2, ps);
:}
;
typeDeclaration ::= aClassDeclaration:cd
{:
RESULT = new ATypeClassDeclaration(cd);
:}
| classExtendsDeclaration:ced
{:
RESULT = new ATypeClassExtendsDeclaration(ced);
:}
;
aClassDeclaration ::= CLASS myIDENTIFIER:id LLAVEi aVarDecl:vd aMethodDecl:md LLAVEd
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AClassDeclaration(ident, vd, md);
:}
;
classExtendsDeclaration ::= CLASS myIDENTIFIER:id1 EXTENDS myIDENTIFIER:id2 LLAVEi aVarDecl:vd aMethodDecl:md LLAVEd
{:
Symbol ident1 = new Symbol(sym.myIDENTIFIER,id1left,id1right,id1);
Symbol ident2 = new Symbol(sym.myIDENTIFIER,id2left,id2right,id2);
RESULT = new AClassExtendsDeclaration(ident1, ident2, vd, md);
:}
;
aVarDeclaration ::= type:t myIDENTIFIER:id PUNTOyCOMA
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AVarDeclaration(t,ident);
:}
;
aMethodDeclaration ::= PUBLIC type:t myIDENTIFIER:id PARENi aFormalParameterBlock:fpb PARENd LLAVEi aVarDecl:vd aStatementList:sl RETURN expression:e PUNTOyCOMA LLAVEd
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AMethodDeclaration(t, ident, fpb, vd, sl, e);
:}
;
formalParameterList ::= aFormalParameter:fp
{:
RESULT = new AFormalParameterList(fp);
:}
| formalParameterList:fpl aFormalParameterRest:fpr
{:
RESULT = new AFormalParameterListRest(fpl, fpr);
:}
;
aFormalParameter ::= type:t myIDENTIFIER:id
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AFormalParameter(t,ident);
:}
;
aFormalParameterRest ::= COMA formalParameterList:fpl
{:
RESULT = new AFormalParameterRest(fpl);
:}
;
type ::= simpleType:st
{:
RESULT = new ATypeSimple(st);
:}
| arrayType:at
{:
RESULT = new ATypeArray (at);
:}
| myIDENTIFIER
{:
RESULT = new ATypeIdentifier();
:}
;
simpleType ::= CHAR
{:
RESULT = new ASimpleTypeChar();
:}
| BOOLEAN
{:
RESULT = new ASimpleTypeBool();
:}
| INT
{:
RESULT = new ASimpleTypeInt();
:}
;
arrayType ::= simpleType:st CORCHETEi CORCHETEd
{:
RESULT = new AnArrayType(st);
:}
;
statement ::= aBlock:b
{:
RESULT = new AStatementBlock(b);
:}
| anAssignmentStatement:as
{:
RESULT = new AStatementAssignment(as);
:}
| anArrayAssignmentStatement:aas
{:
RESULT = new AStatementArrayAssignment(aas);
:}
| anIfStatement:is
{:
RESULT = new AStatementIf(is);
:}
| aWhileStatement:ws
{:
RESULT = new AStatementWhile(ws);
:}
| aSwitchStatement:ss
{:
RESULT = new AStatementSwitch(ss);
:}
| aPrintStatement:ps
{:
RESULT = new AStatementPrint(ps);
:}
;
aBlock ::= LLAVEi aStatementList:sl LLAVEd
{:
RESULT = new ABlock(sl);
:}
;
anAssignmentStatement ::= myIDENTIFIER:id ASIGN expression:e PUNTOyCOMA
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AnAssignmentStatement(ident,e);
:}
;
anArrayAssignmentStatement ::= myIDENTIFIER:id CORCHETEi expression:e1 CORCHETEd ASIGN expression:e2 PUNTOyCOMA
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AnArrayAssignmentStatement(ident,e1,e2);
:}
;
anIfStatement ::= IF PARENi expression:e PARENd statement:s1 ELSE statement:s2
{:
RESULT = new AnIfStatement(e,s1,s2);
:}
;
aWhileStatement ::= WHILE PARENi expression:e PARENd statement:s
{:
RESULT = new AWhileStatement(e,s);
:}
;
aPrintStatement ::= SYSTEM_OUT_PRINTln PARENi expression:e PARENd PUNTOyCOMA
{:
RESULT = new APrintStatement(e);
:}
;
expression ::= anAndExpression:ae
{:
RESULT = new AnExpressionAnd(ae);
:}
| aMenorQueExpression:menorqe
{:
RESULT = new AnExpressionMenor(menorqe);
:}
| aMayorQueExpression:mayorqe
{:
RESULT = new AnExpressionMayor(mayorqe);
:}
| aPlusExpression: pluse
{:
RESULT = new AnExpressionPlus(pluse);
:}
| aMinusExpression: me
{:
RESULT = new AnExpressionMinus(me);
:}
| aDivExpression: de
{:
RESULT = new AnExpressionDiv(de);
:}
| aTimesExpression: te
{:
RESULT = new AnExpressionTimes(te);
:}
| anArrayLookup: alookup
{:
RESULT = new AnExpressionArrayLookup(alookup);
:}
| anArrayLength: alenght
{:
RESULT = new AnExpressionArrayLenght(alenght);
:}
| aMessageSend: ms
{:
RESULT = new AnExpressionMessage(ms);
:}
| primaryExpression:prime
{:
RESULT = new AnExpressionPrimary(prime);
:}
;
anAndExpression ::= primaryExpression:pe1 AND primaryExpression:pe2
{:
RESULT = new AnAndExpression(pe1,pe2);
:}
;
aMenorQueExpression ::= primaryExpression:pe1 MENOR_QUE primaryExpression:pe2
{:
RESULT = new AMenorQueExpression(pe1,pe2);
:}
;
aMayorQueExpression ::= primaryExpression:pe1 MAYOR_QUE primaryExpression:pe2
{:
RESULT = new AMayorQueExpression(pe1,pe2);
:}
;
aPlusExpression ::= primaryExpression:pe1 SUMA primaryExpression:pe2
{:
RESULT = new APlusExpression(pe1,pe2);
:}
;
aMinusExpression ::= primaryExpression:pe1 RESTA primaryExpression:pe2
{:
RESULT = new AMinusExpression(pe1, pe2);
:}
;
aDivExpression ::= primaryExpression:pe1 DIV primaryExpression:pe2
{:
RESULT = new ADivExpression(pe1,pe2);
:}
;
aTimesExpression ::= primaryExpression:pe1 MULT primaryExpression:pe2
{:
RESULT = new ATimesExpression(pe1,pe2);
:}
;
anArrayLookup ::= primaryExpression:pe1 CORCHETEi primaryExpression:pe2 CORCHETEd
{:
RESULT = new AnArrayLookup(pe1,pe2);
:}
;
anArrayLength ::= primaryExpression:pe PUNTO LENGHT
{:
RESULT = new AnArrayLength(pe);
:}
;
aMessageSend ::= primaryExpression:pe PUNTO myIDENTIFIER:id PARENi anExpressionListBlock:elb PARENd
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AMessageSend(pe,ident,elb);
:}
;
expressionList ::= expression:e anExpressionRest:er
{:
RESULT = new AnExpressionList(e, er);
:}
;
anExpressionRest ::= anExpressionRest:er COMA expression:e
{:
RESULT = new AnExpressionRest(er, e);
:}
|
{:
RESULT = null;
:}
;
primaryExpression ::= myINTEGER_LITERAL:il
{:
Symbol int_lit = new Symbol(sym.myINTEGER_LITERAL,illeft,ilright,il);
RESULT = new APrimaryInteger(int_lit);
:}
| myCHAR_LITERAL:cl
{:
Symbol char_lit = new Symbol(sym.myCHAR_LITERAL,clleft,clright,cl);
RESULT = new APrimaryChar(char_lit);
:}
| mySTRING_LITERAL:sl
{:
Symbol string_lit = new Symbol(sym.mySTRING_LITERAL,slleft,slright,sl);
RESULT = new APrimaryString(string_lit);
:}
| TRUE
{:
RESULT = new APrimaryTrue();
:}
| FALSE
{:
RESULT = new APrimaryFalse();
:}
| myIDENTIFIER:id
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new APrimaryIdentifier(ident);
:}
| THIS
{:
RESULT = new APrimaryThis();
:}
| anArrayAllocationExpression:aae
{:
RESULT = new APrimaryArray(aae);
:}
| anAllocationExpression:ae
{:
RESULT = new APrimaryAllocationExpression(ae);
:}
| aNotExpression:ne
{:
RESULT = new APrimaryNotExpression(ne);
:}
| aBracketExpression:be
{:
RESULT = new APrimaryBracket(be);
:}
;
constantExpression ::= myINTEGER_LITERAL:il
{:
Symbol int_lit = new Symbol(sym.myINTEGER_LITERAL,illeft,ilright,il);
RESULT = new AConstantInteger(int_lit);
:}
| myCHAR_LITERAL:cl
{:
Symbol char_lit = new Symbol(sym.myCHAR_LITERAL,clleft,clright,cl);
RESULT = new AConstantChar(char_lit);
:}
| mySTRING_LITERAL:sl
{:
Symbol string_lit = new Symbol(sym.mySTRING_LITERAL,slleft,slright,sl);
RESULT = new AConstantString(string_lit);
:}
| TRUE
{:
RESULT = new AConstantTrue();
:}
| FALSE
{:
RESULT = new AConstantFalse();
:}
;
anArrayAllocationExpression ::= NEW simpleType:st CORCHETEi expression:e CORCHETEd
{:
RESULT = new AnArrayAllocationExpression(st,e);
:}
;
anAllocationExpression ::= NEW myIDENTIFIER:id PARENi PARENd
{:
Symbol ident = new Symbol(sym.myIDENTIFIER,idleft,idright,id);
RESULT = new AnAllocationExpression(ident);
:}
;
aNotExpression ::= NOT expression:e
{:
RESULT = new ANotExpression(e);
:}
;
aBracketExpression ::= PARENi expression:e PARENd
{:
RESULT = new ABracketExpression(e);
:}
;
aVarDecl ::= aVarDecl:vd aVarDeclaration:vdec
{:
RESULT = new AVarDecl(vd, vdec);
:}
|
{:
RESULT = null;
:}
;
aMethodDecl ::= aMethodDecl:md aMethodDeclaration:mdec
{:
RESULT = new AMethodDecl(md,mdec);
:}
|
{:
RESULT = null;
:}
;
aStatementList ::= aStatementList:sl statement:s
{:
RESULT = new AStatementList(sl,s);
:}
|
{:
RESULT = null;
:}
;
anExpressionListBlock ::= expressionList:el
{:
RESULT = new AnExpressionListBlock(el);
:}
|
{:
RESULT = null;
:}
;
aFormalParameterBlock ::= formalParameterList:fpl
{:
RESULT = new AFormalParameterBlock(fpl);
:}
|
{:
RESULT = null;
:}
;
aSwitchStatement ::= SWITCH PARENi expression:e PARENd LLAVEi aSwitchBlock:sb LLAVEd
{:
RESULT = new ASwitchStatement(e,sb);
:};
aCaseBlock ::= CASE constantExpression:ce DOSPUNTOS aStatementList:sl aBreakStatement:bs
{:
RESULT = new ACaseBlock(ce,sl,bs);
:}
;
aDefaultBlock ::= DEFAULT DOSPUNTOS aStatementList:sl aBreakStatement:bs
{:
RESULT = new ADefaultBlock(sl,bs);
:}
|
{:
RESULT = null;
:}
;
aCaseBlockList ::= aCaseBlockList:cbl aCaseBlock:cb
{:
RESULT = new ACaseBlockList(cbl,cb);
:}
|
{:
RESULT = null;
:}
;
aSwitchBlock ::= aCaseBlockList:cbl aDefaultBlock:db
{:
RESULT = new ASwitchBlock(cbl,db);
:}
;
aBreakStatement ::= BREAK PUNTOyCOMA
{:
RESULT = new ABreakStatement();
:}
|
{:
RESULT = null;
:}
;