This repository was archived by the owner on Dec 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminijava.cup
More file actions
179 lines (156 loc) · 5.65 KB
/
minijava.cup
File metadata and controls
179 lines (156 loc) · 5.65 KB
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
package syntax;
import syntax.ast.*; // definition de l'AST
action code {:
// Classes utilitaires pour constructions de paires de listes
class VarOrMeth { // membres de classes = ( fVar | Method) *
ASTList<Var> vars; ASTList<Method> methods;
VarOrMeth () { this.vars = new ASTList<>(); this.methods = new ASTList<>(); }
}
class VarOrStmt { // membre de methodes ou de blocs = ( Var | Stmt ) *
ASTList<Var> vars; ASTList<Stmt> stmts;
VarOrStmt () { this.vars = new ASTList<>(); this.stmts = new ASTList<>(); }
}
:};
terminal SEP, LC, RC, LP, RP, LB, RB, POINT, COMMA ; // ;{}()[].,
terminal CLASS, MAIN, PRINTLN, PUBLIC, STATIC, STRING, VOID ;
terminal Integer LIT_INT ;
terminal String IDENT ;
terminal AND, LESS, PLUS, MINUS, TIMES, NOT;
terminal NEW;
terminal EQ;
terminal Boolean LIT_BOOL;
terminal EXTENDS, BOOLEAN, INT, IF, WHILE, THIS, RETURN, ELSE, LENGTH;
nonterminal Axiom axiom;
nonterminal KlassMain klassMain;
nonterminal Ident ident;
nonterminal Stmt stmt; // production multiregle = Classe Abstraite
nonterminal Expr expr; // production multiregle = Classe Abstraite
nonterminal ASTList<Klass> klassList;
nonterminal Klass klass;
nonterminal Ident parent;
nonterminal VarOrMeth klassBody;
nonterminal Method method;
nonterminal Type type;
nonterminal ASTList<Formal> formalArgs;
nonterminal VarOrStmt methodBody;
nonterminal VarOrStmt methodBodyList;
nonterminal VarOrMeth klassBodyList;
nonterminal ASTList<Formal> formalArgsList;
nonterminal Var variable;
nonterminal ASTList<Expr> args;
nonterminal ASTList<Expr> argsList;
precedence left AND;
precedence left LESS;
precedence left PLUS, MINUS;
precedence left TIMES;
precedence right NOT;
///////////// Productions
axiom ::= klassMain:a klassList:z
{: RESULT = new Axiom(a, z);
RESULT.addPosition(axleft, zxright); :}
;
klassMain ::= CLASS:a ident:id LC
PUBLIC STATIC VOID MAIN
LP STRING LB RB ident:arg RP LC stmt:i RC
RC:z
{: RESULT = new KlassMain(id, arg, i);
RESULT.addPosition(axleft, zxright); :}
;
klassList ::= /* vide */
{: RESULT = new ASTList<>(); :}
| klassList:a klass:b
{: a.add(b); RESULT = a; :}
;
klass ::= CLASS ident:a parent:b LC klassBody:c RC
{: RESULT = new Klass(a,b,c.vars,c.methods); :}
;
parent ::= /* vide */ {: RESULT = new Ident("Object"); :}
| EXTENDS IDENT:a {: RESULT = new Ident(a); :}
;
klassBody ::= /* vide */ {: RESULT = new VarOrMeth(); :}
| klassBodyList:a {: RESULT = a; :}
;
klassBodyList ::= variable:a {: RESULT = new VarOrMeth(); RESULT.vars.add(a); :}
| method:a {: RESULT = new VarOrMeth(); RESULT.methods.add(a); :}
| variable:a klassBodyList:b {: b.vars.add(a); RESULT = b; :}
| method:a klassBodyList:b {: b.methods.add(a); RESULT = b; :}
;
method ::= PUBLIC type:a ident:b LP formalArgs:c RP LC methodBody:d RETURN expr:e SEP RC
{: RESULT = new Method(a,b,c,d.vars, d.stmts, e); :}
;
type ::= BOOLEAN {: RESULT = new Type("boolean"); :}
| INT {: RESULT = new Type("int"); :}
| ident:a {: RESULT = new Type(a.name); :}
;
formalArgs ::= /* vide */ {: new ASTList<Formal>(); :}
| formalArgsList:a {: RESULT = a; :}
;
formalArgsList ::= type:a ident:b {: RESULT = new ASTList<Formal>(); RESULT.add(new Formal(a,b)); :}
| type:a ident:b COMMA formalArgsList:c {: c.add(new Formal(a,b)); RESULT = c; :}
;
methodBody ::= /* vide */ {: new VarOrStmt(); :}
| methodBodyList:a {: RESULT = a; :}
;
methodBodyList ::= variable:a {: RESULT = new VarOrStmt(); RESULT.vars.add(a); :}
| stmt:a {: RESULT = new VarOrStmt(); RESULT.stmts.add(a); :}
| variable:a methodBodyList:b {: b.vars.add(a); RESULT = b; :}
| stmt:a methodBodyList:b {: b.stmts.add(a); RESULT = b; :}
;
variable ::= type:a ident:b SEP {: RESULT = new Var(a,b); :}
;
ident ::= IDENT:a
{: RESULT = new Ident(a);
RESULT.addPosition(axleft,axright); :}
;
stmt ::= PRINTLN:a LP expr:b RP SEP:x
{: RESULT = new StmtPrint(b);
RESULT.addPosition(axleft, xxright); :}
| ident:a EQ expr:b SEP
{: RESULT = new StmtAssign(a,b); :}
| LC methodBody:a RC
{: RESULT = new StmtBlock(a.vars, a.stmts); :}
| IF LP expr:a RP stmt:b ELSE stmt:c
{: RESULT = new StmtIf(a,b,c); :}
| WHILE LP expr:a RP stmt:b
{: RESULT = new StmtWhile(a,b); :}
;
expr ::= LIT_INT:a
{: RESULT = new ExprLiteralInt(a);
RESULT.addPosition(axleft, axright); :}
| LP expr:a RP
{: RESULT = a;
RESULT.addPosition(axleft,axright); :}
| expr:a AND expr:b
{: RESULT = new ExprOpBin(a, main.OPER.AND,b); :}
| expr:a LESS expr:b
{: RESULT = new ExprOpBin(a, main.OPER.LESS,b); :}
| expr:a PLUS expr:b
{: RESULT = new ExprOpBin(a, main.OPER.PLUS,b); :}
| expr:a MINUS expr:b
{: RESULT = new ExprOpBin(a, main.OPER.MINUS ,b); :}
| expr:a TIMES expr:b
{: RESULT = new ExprOpBin(a, main.OPER.TIMES,b); :}
| expr:a NOT expr:b
{: RESULT = new ExprOpBin(a, main.OPER.NOT,b); :}
| expr:a POINT ident:b LP args:c RP
{: RESULT = new ExprCall(a,b,c); :}
| NEW ident:a LP RP
{: RESULT = new ExprNew(a); :}
| ident:a
{: RESULT = new ExprIdent(a); :}
| MINUS LIT_INT:a
{: RESULT = new ExprLiteralInt(-a); :}
| LIT_BOOL:a
{: RESULT = new ExprLiteralBool(a); :}
| NOT expr:a
{: RESULT = new ExprOpUn(main.OPER.NOT,a); :}
| THIS
{: RESULT = new ExprIdent(new Ident("this")); :}
;
args ::= /* vide */
{: RESULT = new ASTList<Expr>(); :}
| argsList:a {: RESULT = a; :}
;
argsList ::= expr:a {: RESULT = new ASTList<Expr>(); RESULT.add(a); :}
| argsList:b COMMA expr:a {: RESULT = b; b.add(a); :}
;