-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathParser.cup
318 lines (278 loc) · 9.27 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
//Mark Klara
//CS 1622 - Project 3
/* Parser.cup */
import java_cup.runtime.*;
import java.util.*;
import syntaxtree.*;
import visitor.*;
import helper.*;
/* Preliminaries to use the scanner. */
scan with {:
return lexer.next_token();
:};
parser code {: Lexer lexer;
boolean errorDetected;
public java_cup.runtime.Symbol currentSym;
public Parser(Lexer lex) { super(lex); lexer = lex; errorDetected = false; }
public void syntax_error(Symbol cur_token) {
errorDetected = true;
System.err.println("Parse error at line " + cur_token.left + ", column " + cur_token.right); }
:};
/* Terminals */
terminal Token PERIOD, COMMA, SEMICOLON, LEFTCURLY, RIGHTCURLY, LEFTPAREN, RIGHTPAREN, LEFTBRACKET, RIGHTBRACKET;
terminal Token AND, LESSTHAN, EXCLAMATION, STAR, MINUS, PLUS, ASSIGNMENT;
terminal Token BOOLEAN, CLASS, ELSE, EXTENDS, FALSE, IF, INT, LENGTH, MAIN, NEW, PUBLIC, RETURN, STATIC, STRING, PRINTLN, THIS, TRUE, VOID, WHILE;
terminal Token IDENTIFIER;
terminal Token INTEGER;
/* Nonterminals */
non terminal Program program;
non terminal MainClass mainClass;
non terminal ClassDecl classDeclaration;
non terminal ClassDeclList classDeclarationChain;
non terminal VarDecl varDeclaration;
non terminal VarDeclList varDeclarationChain;
non terminal MethodDecl methodDeclaration;
non terminal MethodDeclList methodDeclarationChain;
non terminal Type type;
non terminal FormalList typeChain;
non terminal FormalList typeChainParen;
non terminal Statement statement;
non terminal StatementList statementChain;
non terminal Exp expression;
non terminal ExpList expressionChain;
non terminal Identifier identifier;
/* Precedence */
precedence right ASSIGNMENT;
precedence left AND;
precedence left LESSTHAN;
precedence left PLUS, MINUS;
precedence left STAR;
precedence right NEW;
precedence right EXCLAMATION;
precedence left PERIOD, LEFTBRACKET, RIGHTBRACKET, LEFTPAREN, RIGHTPAREN;
/* Start Symbol */
start with program;
program ::= mainClass:mc classDeclarationChain:cdc
{: RESULT = new Program(mc, cdc); :};
mainClass ::= CLASS identifier:id1 LEFTCURLY PUBLIC STATIC VOID MAIN LEFTPAREN STRING LEFTBRACKET RIGHTBRACKET identifier:id2 RIGHTPAREN LEFTCURLY statement:stmt RIGHTCURLY RIGHTCURLY
{: RESULT = new MainClass(id1, id2, stmt); :}
|
CLASS identifier:id1 LEFTCURLY PUBLIC STATIC VOID MAIN LEFTPAREN STRING LEFTBRACKET RIGHTBRACKET identifier:id2 RIGHTPAREN LEFTCURLY RIGHTCURLY RIGHTCURLY
{:
RESULT = new MainClass(id1, id2, null);
:}
|
error RIGHTCURLY RIGHTCURLY
;
classDeclaration ::= CLASS identifier:id LEFTCURLY varDeclarationChain:vdc methodDeclarationChain:mdc RIGHTCURLY
{: RESULT = new ClassDeclSimple(id, vdc, mdc); :}
|
CLASS identifier:id1 EXTENDS identifier:id2 LEFTCURLY varDeclarationChain:vdc methodDeclarationChain:mdc RIGHTCURLY
{: RESULT = new ClassDeclExtends(id1, id2, vdc, mdc); :}
|
error RIGHTCURLY
;
classDeclarationChain ::= classDeclaration:cd classDeclarationChain:cdc
{:
if(cdc == null)
cdc = new ClassDeclList();
cdc.addElement(cd);
RESULT = cdc;
:}
|
{: RESULT = new ClassDeclList(); :}
;
varDeclaration ::= type:t identifier:id SEMICOLON
{: RESULT = new VarDecl(t, id); :}
|
type error SEMICOLON
|
error identifier SEMICOLON
;
varDeclarationChain ::= varDeclarationChain:vdc varDeclaration:vd
{:
if(vdc == null)
vdc = new VarDeclList();
vdc.addElement(vd);
RESULT = vdc;
:}
|
{: RESULT = new VarDeclList(); :}
;
methodDeclaration ::= PUBLIC type:t identifier:id LEFTPAREN RIGHTPAREN LEFTCURLY varDeclarationChain:vdc statementChain:sc RETURN expression:e1 SEMICOLON RIGHTCURLY
{: FormalList list = new FormalList();
RESULT = new MethodDecl(t, id, list, vdc, sc, e1);
:}
|
PUBLIC type:t identifier:id LEFTPAREN type:t2 identifier:id2 typeChainParen:tc LEFTCURLY varDeclarationChain:vdc statementChain:sc RETURN expression:e1 SEMICOLON RIGHTCURLY
{:
//Wrap the additional type/id in a Formal
Formal temp = new Formal(t2, id2);
//Add the first formal to the list, and then add all others from the TypeChain
FormalList list = new FormalList();
list.addElement(temp);
for(int i = 0; i < tc.size(); i++)
{
list.addElement(tc.elementAt(i));
}
RESULT = new MethodDecl(t, id, list, vdc, sc, e1);
:}
|
error RIGHTCURLY
;
methodDeclarationChain ::= methodDeclaration:md methodDeclarationChain:mdc
{:
if(mdc == null)
mdc = new MethodDeclList();
mdc.addElement(md);
RESULT = mdc;
:}
|
{: RESULT = new MethodDeclList(); :}
;
type ::= INT LEFTBRACKET RIGHTBRACKET
{: RESULT = new IntArrayType(); :}
|
BOOLEAN
{: RESULT = new BooleanType(); :}
|
INT
{: RESULT = new IntegerType(); :}
|
identifier:id
{: RESULT = new IdentifierType(id.toString()); :};
typeChainParen ::= typeChain:tc RIGHTPAREN
{:RESULT = tc; :}
|
error RIGHTPAREN
;
typeChain ::= COMMA type:t identifier:id typeChain:tc
{:
//Wrap type/id in a Formal
Formal temp = new Formal(t, id);
if(tc == null)
tc = new FormalList();
tc.addElement(temp);
RESULT = tc;
:}
|
{: RESULT = new FormalList(); :}
;
statement ::= LEFTCURLY statementChain:sc RIGHTCURLY
{: RESULT = new Block(sc); :}
|
IF LEFTPAREN:l expression:e1 RIGHTPAREN statement:s1 ELSE statement:s2
{: RESULT = new If(e1, s1, s2, l.getLine(), l.getCol()); :}
|
WHILE LEFTPAREN:l expression:e1 RIGHTPAREN statement:s
{: RESULT = new While(e1, s, l.getLine(), l.getCol()); :}
|
PRINTLN LEFTPAREN expression:e1 RIGHTPAREN SEMICOLON
{: RESULT = new Print(e1); :}
|
identifier:id ASSIGNMENT:a expression:e1 SEMICOLON
{: RESULT = new Assign(id, e1, a.getLine(), a.getCol()); :}
|
identifier:id LEFTBRACKET expression:e1 RIGHTBRACKET ASSIGNMENT:a expression:e2 SEMICOLON
{: RESULT = new ArrayAssign(id, e1, e2, a.getLine(), a.getCol()); :}
|
error SEMICOLON
;
statementChain ::= statement:stmt statementChain:sc
{: if(sc == null)
sc = new StatementList();
sc.addElement(stmt);
RESULT = sc;
:}
|
{: RESULT = new StatementList(); :}
;
expression ::= expression:e1 AND:a expression:e2
{: RESULT = new And(e1, e2, a.getLine(), a.getCol()); :}
|
expression:e1 LESSTHAN:l expression:e2
{: RESULT = new LessThan(e1, e2, l.getLine(), l.getCol()); :}
|
expression:e1 PLUS:p expression:e2
{: RESULT = new Plus(e1, e2, p.getLine(), p.getCol()); :}
|
expression:e1 MINUS:m expression:e2
{: RESULT = new Minus(e1, e2, m.getLine(), m.getCol()); :}
|
expression:e1 STAR:s expression:e2
{: RESULT = new Times(e1, e2, s.getLine(), s.getCol()); :}
|
expression:e1 LEFTBRACKET expression:e2 RIGHTBRACKET
{: RESULT = new ArrayLookup(e1, e2); :}
|
expression:e1 PERIOD:p LENGTH
{: RESULT = new ArrayLength(e1, p.getLine(), p.getCol()); :}
|
expression:e1 PERIOD:p identifier:id LEFTPAREN RIGHTPAREN
{:
ExpList ec = new ExpList();
RESULT = new Call(e1, id, ec, p.getLine(), p.getCol());
:}
|
expression:e1 PERIOD:p identifier:id LEFTPAREN expression:e2 expressionChain:ec RIGHTPAREN
{:
//Create a new list and add "e2" as the first argument. Then add any from the expressionChain
ExpList list = new ExpList();
list.addElement(e2);
for(int i = 0; i < ec.size(); i++)
{
list.addElement(ec.elementAt(i));
}
RESULT = new Call(e1, id, list, p.getLine(), p.getCol());
:}
|
INTEGER:i
{:
Integer x = (Integer) i.getValue();
RESULT = new IntegerLiteral(x.intValue(), i.getLine(), i.getCol());
:}
|
MINUS INTEGER:i
{:
Integer x = (Integer) i.getValue();
RESULT = new IntegerLiteral(x.intValue()*-1, i.getLine(), i.getCol());
:}
|
TRUE
{: RESULT = new True(); :}
|
FALSE
{: RESULT = new False(); :}
|
identifier:id
{: RESULT = new IdentifierExp(id.toString(), id.lineNum, id.charNum); :}
|
THIS:t
{: RESULT = new This(t.getLine(), t.getCol()); :}
|
NEW INT LEFTBRACKET expression:e1 RIGHTBRACKET
{: RESULT = new NewArray(e1); :}
|
NEW identifier:id LEFTPAREN RIGHTPAREN
{: RESULT = new NewObject(id); :}
|
EXCLAMATION:e expression:e1
{: RESULT = new Not(e1, e.getLine(), e.getCol()); :}
|
LEFTPAREN expression:e1 RIGHTPAREN
{: RESULT = e1; :};
expressionChain ::= COMMA expression:e1 expressionChain:ec
{: if(ec == null)
ec = new ExpList();
ec.addElement(e1);
RESULT = ec;
:}
|
{: RESULT = new ExpList(); :}
;
identifier ::= IDENTIFIER:id
{:
String s = (String)id.getValue();
RESULT = new Identifier(s, id.getLine(), id.getCol());
:};