-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.cup
More file actions
382 lines (325 loc) · 14.9 KB
/
parser.cup
File metadata and controls
382 lines (325 loc) · 14.9 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
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
package com.jc;
import java_cup.runtime.*;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.lang.Object;
import java.util.*;
import com.jc.node.*;
parser code {:
protected Lexer lexer;
private FileInputStream fis;
private HashMap<String, String> symbolTable;
private HashMap<String, String> tempTable;
private Node tree;
private int tempSequence;
public Parser(String filename) {
symbolTable = new HashMap<>();
tempTable = new HashMap<>();
tempSequence = 1;
File file = new File(filename);
try {
fis = new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
}
public Node getTree() {
return tree;
}
public HashMap<String, String> getSymbolTable() {
return symbolTable;
}
public HashMap<String, String> getTempTable() {
return tempTable;
}
private void declare(String name, String type) throws Exception {
if (symbolTable.containsKey(name)) {
throw new Exception(String.format("Variable already declared: '%s'", name));
}
symbolTable.put(name, type);
}
private String newTemp(String type) {
String tempName = "t" + tempSequence++;
tempTable.put(tempName, type);
return tempName;
}
private void verifyVar(String name) throws Exception {
if (!symbolTable.containsKey(name)) {
throw new Exception(String.format("Variable not already declared: '%s'", name));
}
}
private String getVarType(String name) throws Exception {
verifyVar(name);
return symbolTable.get(name);
}
private Variable newVariableExpr(String name, String type) {
Variable variable = new Variable();
variable.name = name;
variable.type = type;
return variable;
}
private Literal newLiteralExpr(String type, Object value) {
Literal literal = new Literal();
literal.type = type;
literal.value = value;
return literal;
}
private Operation newOperationExpr(String op, Expression left, Expression right) throws Exception {
Operation operation = new Operation();
operation.type = widen(left.type, right.type);
operation.operation = op;
operation.temp = newTemp(operation.type);
return (Operation) pushBackChildren(operation, left, right);
}
private Operation newIncrementOperationExpr(String op, String varName) throws Exception {
Increment increment = new Increment();
increment.variable = varName;
increment.operation = op;
increment.type = getVarType(varName);
increment.temp = newTemp(increment.type);
return (Operation) pushBackChildren(increment, newVariableExpr(varName, getVarType(varName)));
}
private Operation newRelOperationExpr(String op, Expression left, Expression right) throws Exception {
Operation operation = new Operation();
operation.type = Constants.T_BOOL;
operation.operation = op;
operation.temp = newTemp(operation.type);
return (Operation) pushBackChildren(operation, left, right);
}
private Operation newUnaryOperationExpr(String op, Expression exp) throws Exception {
Operation operation = new Operation();
operation.type = exp.type;
operation.operation = op;
operation.temp = newTemp(operation.type);
return (Operation) pushBackChildren(operation, exp);
}
private Assignment newAssignmentNode(String name, Expression exp) throws Exception {
verifyVar(name);
Assignment assignment = new Assignment();
assignment.variable = name;
return (Assignment) pushBackChildren(assignment, exp);
}
private Declaration newDeclarationNode(String name, String type) {
Declaration declaration = new Declaration();
declaration.type = type;
declaration.name = name;
return declaration;
}
private Node newDeclarationNode(String name, String type, Expression value) throws Exception {
Declaration declaration = newDeclarationNode(name, type);
Assignment assignment = newAssignmentNode(name, value);
Node node = new Node();
return pushBackChildren(node, declaration, assignment);
}
private Print newPrintNode(Expression expression) {
Print print = new Print();
return (Print) pushBackChildren(print, expression);
}
private Conditional newConditionalNode(Expression test, Node block, Node conditionalAlt) {
Conditional conditional = new Conditional();
conditional.test = test;
conditional = (Conditional) pushBackChildren(conditional, block);
if (conditionalAlt != null) {
conditional = (Conditional) pushBackChildren(conditional, conditionalAlt);
}
return conditional;
}
private WhileLoop newWhileLoopNode(Expression test, Node block) {
WhileLoop whileLoop = new WhileLoop();
whileLoop.test = test;
whileLoop = (WhileLoop) pushBackChildren(whileLoop, block);
return whileLoop;
}
private DoWhileLoop newDoWhileLoopNode(Expression test, Node block) {
DoWhileLoop doWhileLoop = new DoWhileLoop();
doWhileLoop.test = test;
doWhileLoop = (DoWhileLoop) pushBackChildren(doWhileLoop, block);
return doWhileLoop;
}
private ForLoop newForLoopNode(Node declaration, Expression test, Node increment, Node block) {
ForLoop forLoop = new ForLoop();
forLoop.declaration = declaration;
forLoop.test = test;
forLoop.increment = increment;
return (ForLoop) pushBackChildren(forLoop, block);
}
private String widen(String type1, String type2) throws Exception {
if (type1.equals(Constants.T_REAL) || type2.equals(Constants.T_REAL)) return Constants.T_REAL;
if (type1.equals(Constants.T_INT) || type2.equals(Constants.T_INT)) return Constants.T_INT;
if (type1.equals(Constants.T_BOOL) || type2.equals(Constants.T_BOOL)) return Constants.T_BOOL;
return "";
}
private void verifyAritTypes(String type1, String type2) throws Exception {
if (!type1.equals(Constants.T_REAL) && !type1.equals(Constants.T_INT) ||
!type2.equals(Constants.T_REAL) && !type2.equals(Constants.T_INT)) {
throw new Exception(String.format("Invalid operation with types '%s' and '%s'", type1, type2));
}
}
public void verifyLogicTypes(String type1, String type2) throws Exception {
if (!type1.equals(Constants.T_BOOL) || !type2.equals(Constants.T_BOOL)) {
throw new Exception(String.format("Invalid operation with types '%s' and '%s'", type1, type2));
}
}
public void verifyUnaryLogicTypes(String type) throws Exception {
if (!type.equals(Constants.T_BOOL)) {
throw new Exception(String.format("Invalid unary operation with type '%s'", type));
}
}
private void verifyAssignTypes(String type1, String type2) throws Exception {
if (!type1.equals(type2)) {
throw new Exception(String.format(
"Cannot assign expression with type '%s' in variable with type '%s'", type2, type1));
}
}
private void verifyBooleanType(String type) throws Exception {
if (!type.equals(Constants.T_BOOL)) {
throw new Exception(String.format("Invalid type '%s', must be 'bool'", type));
}
}
private void verifyRelationalTypes(String type1, String type2) throws Exception {
if (!type1.equals(type2)) {
throw new Exception(String.format(
"Cannot compare expressions with types '%s' and '%s'", type2, type1));
}
}
private void verifyUnaryAritType(String type) throws Exception {
if (!type.equals(Constants.T_INT) && !type.equals(Constants.T_REAL)) {
throw new Exception(String.format("Cannot use operation with type '%s'", type));
}
}
private Node pushBackChildren(Node parent, Node... children) {
if (parent.children == null) {
parent.children = new ArrayList<>();
}
parent.children.addAll(List.of(children));
return parent;
}
private Node pushFrontChild(Node parent, Node child) {
if (parent.children == null) {
parent.children = new ArrayList<>();
}
parent.children.add(0, child);
return parent;
}
private String trimString(String string) {
return string.substring(1, string.length()-1);
}
:};
init with {:
ComplexSymbolFactory factory = new ComplexSymbolFactory();
symbolFactory = factory;
lexer = new Lexer(factory, fis);
:};
scan with {:
return lexer.yylex();
:};
terminal String ID;
terminal String TYPE;
terminal Double REAL;
terminal Integer INT;
terminal String STRING;
terminal Boolean BOOL;
terminal ASSIGN;
terminal ADD;
terminal SUB;
terminal MULT;
terminal DIV;
terminal POW;
terminal INC;
terminal DEC;
terminal OR;
terminal AND;
terminal NOT;
terminal EQ;
terminal NEQ;
terminal GT;
terminal GTE;
terminal LT;
terminal LTE;
terminal SMC; // semicolon
terminal L_PTH; // open parenthesis
terminal R_PTH; // close parenthesis
terminal L_CRL; // left curly brackets
terminal R_CRL; // right curly brackets
terminal PRINT;
terminal IF;
terminal ELSE;
terminal WHILE;
terminal DO;
terminal FOR;
non terminal Node S;
non terminal Node _COMMANDS;
non terminal Node _COMMAND;
non terminal Node _DECL;
non terminal Node _ASSIGN;
non terminal Expression _EXP;
non terminal Node _PRINT;
non terminal Node _CONDITIONAL;
non terminal Node _CONDITIONAL_ALT;
non terminal Node _WHILE_LOOP;
non terminal Node _FOR_LOOP;
precedence left EQ, NEQ, GT, GTE, LT, LTE;
precedence left ADD, SUB;
precedence left MULT, DIV;
precedence left POW;
precedence left OR;
precedence left AND;
precedence left NOT;
S ::= _COMMANDS:cmds {: tree = cmds; RESULT = cmds; :}
;
_COMMANDS ::= _COMMAND:cmd SMC _COMMANDS:cmds {: RESULT = pushFrontChild(cmds, cmd); :}
| _CONDITIONAL:cond _COMMANDS:cmds {: RESULT = pushFrontChild(cmds, cond); :}
| _WHILE_LOOP:loop _COMMANDS:cmds {: RESULT = pushFrontChild(cmds, loop); :}
| _FOR_LOOP:loop _COMMANDS:cmds {: RESULT = pushFrontChild(cmds, loop); :}
| {: RESULT = new Node(); :}
;
_COMMAND ::= _DECL:node {: RESULT = node; :}
| _ASSIGN:node {: RESULT = node; :}
| _PRINT:node {: RESULT = node; :}
| _EXP:e {: RESULT = e; :}
;
_PRINT ::= PRINT L_PTH _EXP:e R_PTH {: RESULT = newPrintNode(e); :};
_DECL ::= TYPE:t ID:id {: declare(id, t); RESULT = newDeclarationNode(id, t); :}
| TYPE:t ID:id ASSIGN _EXP:e {:
declare(id, t); verifyAssignTypes(t, e.type); RESULT = newDeclarationNode(id, t, e); :}
;
_ASSIGN ::= ID:id ASSIGN _EXP:e {: verifyAssignTypes(getVarType(id), e.type); RESULT = newAssignmentNode(id, e); :};
_EXP ::= ID:id {: RESULT = newVariableExpr(id, getVarType(id)); :}
| REAL:val {: RESULT = newLiteralExpr(Constants.T_REAL, val); :}
| INT:val {: RESULT = newLiteralExpr(Constants.T_INT, val); :}
| BOOL:val {: RESULT = newLiteralExpr(Constants.T_BOOL, val); :}
| STRING:val {: RESULT = newLiteralExpr(Constants.T_STR, trimString(val)); :}
| _EXP:e1 ADD _EXP:e2 {: verifyAritTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_ADD, e1, e2); :}
| _EXP:e1 SUB _EXP:e2 {: verifyAritTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_SUB, e1, e2); :}
| _EXP:e1 MULT _EXP:e2 {: verifyAritTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_MULT, e1, e2); :}
| _EXP:e1 DIV _EXP:e2 {: verifyAritTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_DIV, e1, e2); :}
| _EXP:e1 POW _EXP:e2 {: verifyAritTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_POW, e1, e2); :}
| ID:id INC {: verifyUnaryAritType(getVarType(id)); RESULT = newIncrementOperationExpr(Constants.OP_INC, id); :}
| ID:id DEC {: verifyUnaryAritType(getVarType(id)); RESULT = newIncrementOperationExpr(Constants.OP_DEC, id); :}
| _EXP:e1 OR _EXP:e2 {: verifyLogicTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_OR, e1, e2); :}
| _EXP:e1 AND _EXP:e2 {: verifyLogicTypes(e1.type, e2.type); RESULT = newOperationExpr(Constants.OP_AND, e1, e2); :}
| NOT _EXP:e {: verifyUnaryLogicTypes(e.type); RESULT = newUnaryOperationExpr(Constants.OP_NOT, e); :}
| _EXP:e1 EQ _EXP:e2 {: verifyRelationalTypes(e1.type, e2.type); RESULT = newRelOperationExpr(Constants.OP_EQ, e1, e2); :}
| _EXP:e1 NEQ _EXP:e2 {: verifyRelationalTypes(e1.type, e2.type); RESULT = newRelOperationExpr(Constants.OP_NEQ, e1, e2); :}
| _EXP:e1 GT _EXP:e2 {: verifyRelationalTypes(e1.type, e2.type); RESULT = newRelOperationExpr(Constants.OP_GT, e1, e2); :}
| _EXP:e1 GTE _EXP:e2 {: verifyRelationalTypes(e1.type, e2.type); RESULT = newRelOperationExpr(Constants.OP_GTE, e1, e2); :}
| _EXP:e1 LT _EXP:e2 {: verifyRelationalTypes(e1.type, e2.type); RESULT = newRelOperationExpr(Constants.OP_LT, e1, e2); :}
| _EXP:e1 LTE _EXP:e2 {: verifyRelationalTypes(e1.type, e2.type); RESULT = newRelOperationExpr(Constants.OP_LTE, e1, e2); :}
| L_PTH _EXP:e R_PTH {: RESULT = e; :}
;
_CONDITIONAL ::= IF L_PTH _EXP:e R_PTH L_CRL _COMMANDS:cmds R_CRL _CONDITIONAL_ALT:alt {:
verifyBooleanType(e.type); RESULT = newConditionalNode(e, cmds, alt); :}
;
_CONDITIONAL_ALT ::= ELSE L_CRL _COMMANDS:cmds R_CRL {: RESULT = cmds; :}
| ELSE _CONDITIONAL:cond {: RESULT = cond; :}
|
;
_WHILE_LOOP ::= WHILE L_PTH _EXP:e R_PTH L_CRL _COMMANDS:cmds R_CRL {:
verifyBooleanType(e.type); RESULT = newWhileLoopNode(e, cmds); :}
| DO L_CRL _COMMANDS:cmds R_CRL WHILE L_PTH _EXP:e R_PTH {:
verifyBooleanType(e.type); RESULT = newDoWhileLoopNode(e, cmds); :}
;
_FOR_LOOP ::= FOR L_PTH _DECL:decl SMC _EXP:e SMC _COMMAND:cmd R_PTH
L_CRL _COMMANDS:cmds R_CRL {:
verifyBooleanType(e.type); RESULT = newForLoopNode(decl, e, cmd, cmds); :};