-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
378 lines (350 loc) · 12.1 KB
/
Parser.java
File metadata and controls
378 lines (350 loc) · 12.1 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
package edu.ufl.cise.plc;
import edu.ufl.cise.plc.IToken.Kind;
import edu.ufl.cise.plc.ast.ASTNode;
import edu.ufl.cise.plc.ast.*;
import edu.ufl.cise.plc.ast.Types.Type;
import java.beans.Expression;
import java.util.ArrayList;
import java.util.List;
public class Parser implements IParser{
String code;
List<IToken> tokens = new ArrayList<IToken>();
int position =0;
public Parser(String input){
this.code=input;
}
public ASTNode parse() throws PLCException{
//generate list of tokens using the lexer
ILexer lex=CompilerComponentFactory.getLexer(code);
do{
tokens.add(lex.next());
}while(tokens.get(tokens.size() - 1).getKind() != Kind.EOF);
return program();
}
Expr expression() throws SyntaxException {
if(check(Kind.KW_IF))
return conditionalExpression();
else
return logicalOrExpression();
}
void checkIncomplete(String message) throws SyntaxException{
if(check(Kind.EOF))
throw new SyntaxException(message);
}
//start of assignment 3 changes
Program program() throws SyntaxException{
IToken head=tokens.get(position);
Type type;
String name;
List<NameDef> params = new ArrayList<NameDef>();
List<ASTNode> decsAndStatements = new ArrayList<ASTNode>();
if(check(Kind.KW_VOID)) {
type = Type.toType("void");
}
else if(!check(Kind.TYPE)){
throw new SyntaxException("Program must have a return type");
}
else{
type=Type.toType(head.getText());
}
position++;
if(check(Kind.IDENT)) {
name=tokens.get(position).getText();
}
else{
throw new SyntaxException("Program must have a name");
}
position++;
if(!check(Kind.LPAREN)){
throw new SyntaxException("Expected '(' after program name");
}
position++;
if(check(Kind.TYPE)){
params.add(nameDef());
while(check(Kind.COMMA)) {
position++;
params.add(nameDef());
}
}
if(!check(Kind.RPAREN)){
throw new SyntaxException("Expected ')' after parameters");
}
position++;
while(!check(Kind.EOF)){
if(check(Kind.TYPE, Kind.KW_WRITE, Kind.RETURN, Kind.IDENT)){
if(check(Kind.TYPE)) decsAndStatements.add(declaration());
else decsAndStatements.add(statement());
//running into problems here
if(!check(Kind.SEMI)){
throw new SyntaxException("Expected ;");
}
position++;
}
else throw new SyntaxException("Syntax error: Unexpected token at position " + position);
}
return new Program(head, type, name, params, decsAndStatements);
}
NameDef nameDef() throws SyntaxException{
if(!check(Kind.TYPE)) throw new SyntaxException("Syntax error: Expected a type");
IToken head=tokens.get(position);
Type type=Type.toType(tokens.get(position).getText());
position++;
if(check(Kind.IDENT)){
position++;
return new NameDef(head, head.getText(), tokens.get(position-1).getText());
}
else if(check(Kind.LSQUARE)){
Dimension d=dimension();
if(!check(Kind.IDENT)) throw new SyntaxException("Syntax error: expected identifier after dimension");
position++;
return new NameDefWithDim(head, head.getText(), tokens.get(position-1).getText(), d);
}
else throw new SyntaxException("Invalid name definition");
}
Declaration declaration() throws SyntaxException{
IToken head=tokens.get(position);
NameDef n=nameDef();
if(!check(Kind.ASSIGN, Kind.LARROW))
return new VarDeclaration(head, n, null, null);
else{
IToken op=tokens.get(position);
position++;
return new VarDeclaration(head, n, op, expression());
}
}
ConditionalExpr conditionalExpression() throws SyntaxException {
IToken head=tokens.get(position);
position++;
if(!check(Kind.LPAREN))
throw new SyntaxException("Expected '(' after if statement");
position++;
Expr condition=expression();
if(!check(Kind.RPAREN))
throw new SyntaxException("Expected ')' after conditional expression");
position++;
Expr positive=expression();
if(!check(Kind.KW_ELSE))
throw new SyntaxException("Expected 'else'");
position++;
Expr negative=expression();
if(!check(Kind.KW_FI))
throw new SyntaxException("Expected 'fi'");
position++;
return new ConditionalExpr(head, condition, positive, negative);
}
Expr logicalOrExpression() throws SyntaxException {
IToken head = tokens.get(position);
Expr left=logicalAndExpression();
if(!check(Kind.OR)){
return left;
}
else{
Expr result=left;
while(check(Kind.OR)){
IToken op=tokens.get(position);
position++;
Expr right=logicalAndExpression();
result= new BinaryExpr(head, result, op, right);
}
return result;
}
}
Expr logicalAndExpression() throws SyntaxException {
IToken head = tokens.get(position);
Expr left=comparisonExpression();
if(!check(Kind.AND)){
return left;
}
else{
Expr result=left;
while(check(Kind.AND)){
IToken op=tokens.get(position);
position++;
Expr right=comparisonExpression();
result= new BinaryExpr(head, result, op, right);
}
return result;
}
}
Expr comparisonExpression() throws SyntaxException {
IToken head = tokens.get(position);
Expr left=additiveExpression();
if(!check(Kind.LT, Kind.GT, Kind.EQUALS, Kind.NOT_EQUALS, Kind.LE, Kind.GE)){
return left;
}
else{
Expr result=left;
while(check(Kind.LT, Kind.GT, Kind.EQUALS, Kind.NOT_EQUALS, Kind.LE, Kind.GE)){
IToken op=tokens.get(position);
position++;
Expr right=additiveExpression();
result= new BinaryExpr(head, result, op, right);
}
return result;
}
}
Expr additiveExpression() throws SyntaxException {
IToken head = tokens.get(position);
Expr left=multiplicativeExpression();
if(!check(Kind.PLUS,Kind.MINUS)){
return left;
}
else{
Expr result=left;
while(check(Kind.PLUS,Kind.MINUS)){
IToken op=tokens.get(position);
position++;
Expr right=multiplicativeExpression();
result= new BinaryExpr(head, result, op, right);
}
return result;
}
}
Expr multiplicativeExpression() throws SyntaxException {
IToken head = tokens.get(position);
Expr left=unaryExpression();
if(!check(Kind.TIMES,Kind.DIV,Kind.MOD)){
return left;
}
else{
Expr result=left;
while(check(Kind.TIMES,Kind.DIV,Kind.MOD)){
IToken op=tokens.get(position);
position++;
Expr right=unaryExpression();
result= new BinaryExpr(head, result, op, right);
}
return result;
}
}
Expr unaryExpression() throws SyntaxException {
IToken head = tokens.get(position);
if(check(Kind.BANG,Kind.MINUS,Kind.COLOR_OP,Kind.IMAGE_OP)){
position++;
return new UnaryExpr(head, tokens.get(position-1), unaryExpression());
}
else return unaryExpressionPostfix();
}
Expr unaryExpressionPostfix() throws SyntaxException {
IToken head = tokens.get(position);
Expr left=primaryExpression();
if(check(Kind.LSQUARE)){
PixelSelector selector=pixelSelector();
return new UnaryExprPostfix(head, left, selector);
}
return left;
}
Expr primaryExpression() throws SyntaxException {
IToken t=tokens.get(position);
switch(t.getKind()){
case BOOLEAN_LIT -> {
position++;
return new BooleanLitExpr(t);
}
case STRING_LIT -> {
position++;
return new StringLitExpr(t);
}
case INT_LIT -> {
position++;
return new IntLitExpr(t);
}
case FLOAT_LIT -> {
position++;
return new FloatLitExpr(t);
}
case IDENT -> {
position++;
return new IdentExpr(t);
}
case LPAREN -> {
position++;
Expr inner = expression();
position++;
return inner;
}
case COLOR_CONST -> {
position++;
return new ColorConstExpr(t);
}
case LANGLE -> {
position++;
Expr left = expression();
if(!check(Kind.COMMA)) throw new SyntaxException("Expected ',' in color expression");
position++;
Expr middle=expression();
if(!check(Kind.COMMA)) throw new SyntaxException("Expected ',' in color expression");
position++;
Expr right=expression();
if(!check(Kind.RANGLE)) throw new SyntaxException("Expected '>>' at end of color expression");
position++;
return new ColorExpr(t, left, middle, right);
}
case KW_CONSOLE -> {
position++;
return new ConsoleExpr(t);
}
default -> {
throw new SyntaxException("Unexpected token in primary expression");
}
}
}
PixelSelector pixelSelector() throws SyntaxException {
IToken t=tokens.get(position);
position++;
Expr x=expression();
position++;
Expr y=expression();
position++;
return new PixelSelector(t, x, y);
}
Dimension dimension() throws SyntaxException {
IToken t=tokens.get(position);
position++;
Expr x=expression();
position++;
Expr y=expression();
position++;
return new Dimension(t, x, y);
}
Statement statement() throws SyntaxException{
IToken head = tokens.get(position);
if(check(Kind.KW_WRITE)){
position++;
Expr source=expression();
if(!check(Kind.RARROW)) throw new SyntaxException("Expected '->' in write statement");
position++;
Expr dest=expression();
return new WriteStatement(head, source, dest);
}
else if(check(Kind.RETURN)){
position++;
Expr expr=expression();
return new ReturnStatement(head, expr);
}
else if(check(Kind.IDENT)){
position++;
PixelSelector selector=null;
if(check(Kind.LSQUARE)){
selector=pixelSelector();
}
if(check(Kind.ASSIGN)){
position++;
return new AssignmentStatement(head, head.getText(), selector, expression());
}
else if(check(Kind.LARROW)){
position++;
return new ReadStatement(head, head.getText(), selector, expression());
}
else throw new SyntaxException("Unexpected character in statement");
}
else throw new SyntaxException("Invalid statement");
}
private boolean check(Kind... kinds){
for(Kind k : kinds){
if(position<tokens.size() &&tokens.get(position).getKind()==k)
return true;
}
return false;
}
}