-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPLXC.cup
262 lines (237 loc) · 5.74 KB
/
PLXC.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
import java_cup.runtime.*;
action code {:
/* Variables */
public static int varTemp = 0;
private TokensTable tokens_table = new TokensTable();
/* Methods */
private static String newVarTemp() {
return "$t" + (varTemp++);
}
private String genTAC(String tac){
String temp = newVarTemp();
PLXC.out.println("\t" + temp + tac);
return temp;
}
private LabelPair genCondition(String fexp, String sexp, String binop, Boolean swap){
LabelPair lp = new LabelPair();
String labels = (swap) ? lp.getLabelFalse() + ";\n\tgoto " + lp.getLabelTrue() :
lp.getLabelTrue() + ";\n\tgoto " + lp.getLabelFalse();
PLXC.out.println("\tif (" + fexp + binop + sexp + ") goto " + labels + ";");
return lp;
}
private String newLabel() {
return Yylex.newLabel();
}
/* Classes */
public static class LabelPair {
private String labelTrue;
private String labelFalse;
public LabelPair(){
labelTrue = Yylex.newLabel();
labelFalse = Yylex.newLabel();
}
public String getLabelTrue() {
return labelTrue;
}
public String getLabelFalse() {
return labelFalse;
}
public void swapLabels() {
String aux = labelTrue;
labelTrue = labelFalse;
labelFalse = aux;
}
}
:}
// TERMINALS
terminal INT;
terminal PRINT, ELSE, WHILE, DO, FOR;
terminal String IF;
terminal OP, CP, OB, CB;
terminal ADD, SUB, MUL, DIV, UMIN, SC, ASIG, COMMA;
terminal DEQ, LT, LEQ, GT, GEQ, NEQ, AND, OR, NOT;
terminal String NUM, ID;
// NON TERMINALS
non terminal Object sent_list, sentence, optionalElse;
non terminal String expr;
non terminal Token declaration;
non terminal LabelPair cond;
// PRECEDENCES
precedence left OR;
precedence left AND;
precedence right NOT;
precedence left ADD, SUB;
precedence left DIV, MUL;
precedence right UMIN;
precedence left ELSE;
// GRAMMAR
sent_list ::= sentence
| sent_list sentence
;
sentence ::= expr:e SC
{:
RESULT = e;
:}
| PRINT OP expr:e CP SC
{:
PLXC.out.println("\tprint " + e + ";");
:}
| IF:labEnd OP cond:c CP
{:
PLXC.out.println(c.getLabelTrue() + ":");
:}
sentence
{:
PLXC.out.println("\tgoto " + labEnd + ";");
PLXC.out.println(c.getLabelFalse() + ":");
:}
optionalElse
{:
PLXC.out.println(labEnd + ":");
:}
| OB sent_list CB
| WHILE:labIniWhile
{:
PLXC.out.println(labIniWhile + ":");
:}
OP cond:c CP
{:
PLXC.out.println(c.getLabelTrue() + ":");
:}
sentence
{:
PLXC.out.println("\tgoto " + labIniWhile + ";");
PLXC.out.println(c.getLabelFalse() + ":");
:}
| DO:labIniDo
{:
PLXC.out.println(labIniDo + ":");
:}
sentence WHILE OP cond:c CP SC
{:
PLXC.out.println(c.getLabelTrue() + ":");
PLXC.out.println("\tgoto " + labIniDo + ";");
PLXC.out.println(c.getLabelFalse() + ":");
:}
| FOR:labIniFor OP expr
{:
PLXC.out.println(labIniFor + ":");
:}
SC cond:c
{:
String labIncrFor = Yylex.newLabel();
PLXC.out.println(labIniFor + "_" + ":");
:}
SC expr
{:
PLXC.out.println("\tgoto " + labIniFor + ";");
PLXC.out.println(c.getLabelTrue() + ":");
:}
CP sentence
{:
PLXC.out.println("\tgoto " + labIniFor + "_" + ";");
PLXC.out.println(c.getLabelFalse() + ":");
:}
| declaration SC
;
optionalElse ::= ELSE sentence
| /*epsilon*/
;
expr ::= expr:e1 ADD expr:e2
{:
RESULT = genTAC(" = " + e1 + " + " + e2 + ";");
:}
| expr:e1 SUB expr:e2
{:
RESULT = genTAC(" = " + e1 + " - " + e2 + ";");
:}
| expr:e1 MUL expr:e2
{:
RESULT = genTAC(" = " + e1 + " * " + e2 + ";");
:}
| expr:e1 DIV expr:e2
{:
RESULT = genTAC(" = " + e1 + " / " + e2 + ";");
:}
| OP expr:e CP
{:
RESULT = e;
:}
| SUB expr:e
{:
RESULT = genTAC(" = -" + e + ";");
:} %prec UMIN
| ID:id
{:
Token t = tokens_table.extract(id);
RESULT = (t != null) ? t.getName() : null;
:}
| NUM:num
{:
RESULT = num;
:}
| ID:id ASIG expr:e
{:
PLXC.out.println("\t" + id + " = " + e + ";");
RESULT = id;
:}
;
cond ::= expr:e1 DEQ expr:e2
{:
RESULT = genCondition(e1, e2, " == ", false);
:}
| expr:e1 NEQ expr:e2
{:
RESULT = genCondition(e1, e2, " == ", true);
:}
| expr:e1 LT expr:e2
{:
RESULT = genCondition(e1, e2, " < ", false);
:}
| expr:e1 LEQ expr:e2
{:
RESULT = genCondition(e2, e1, " < ", true);
:}
| expr:e1 GT expr:e2
{:
RESULT = genCondition(e2, e1, " < ", false);
:}
| expr:e1 GEQ expr:e2
{:
RESULT = genCondition(e1, e2, " < ", true);
:}
| cond:c1 AND
{:
PLXC.out.println(c1.getLabelTrue() + ":");
:}
cond:c2
{:
PLXC.out.println(c1.getLabelFalse() + ":");
PLXC.out.println("\tgoto " + c2.getLabelFalse() + ";");
RESULT = c2;
:}
| cond:c1 OR
{:
PLXC.out.println(c1.getLabelFalse() + ":");
:}
cond:c2
{:
PLXC.out.println(c1.getLabelTrue() + ":");
PLXC.out.println("\tgoto " + c2.getLabelTrue() + ";");
RESULT = c2;
:}
| NOT cond:c
{:
c.swapLabels();
RESULT = c;
:}
;
declaration ::= INT ID:id
{:
Token t = tokens_table.add(id, "int");
:}
| declaration COMMA ID:id
{:
Token t = tokens_table.add(id, "int");
:}
;