-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.java
More file actions
executable file
·305 lines (166 loc) · 7.66 KB
/
Parser.java
File metadata and controls
executable file
·305 lines (166 loc) · 7.66 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
/**
* Write a description of class Parser here.
*
* @author (Kelvin Likollari)
* @version (04.06.2021)
*/
public class Parser {
private Lexer lex;
public Parser() {
lex = new Lexer();
}
public Parser (String s)
{
lex = new Lexer (s);
}
public ExpTree parseLine() {
lex.init();
lex.getToken();
ExpTree result = parseExp(true);
if (lex.token==Lexer.where) {
lex.getToken();
ExpTree defs = parseDefList();
result = makeWhereTree(result, defs);
}
return result;
}
public String getLine() {
return lex.getLine();
}
private ExpTree parseExp(boolean idsAllowed) {
ExpTree result = parseTerm(idsAllowed);
{ while (lex.token == Lexer.plus || lex.token == Lexer.minus)
{ int op = lex.token;
lex.getToken();
if (op == Lexer.plus)
result = makePlusTree(result, parseTerm(idsAllowed));
else
result = makeMinusTree(result, parseTerm(idsAllowed));
}
}
return result;
}
private ExpTree parseTerm(boolean idsAllowed)
{ ExpTree result = parseOpd(idsAllowed);
{ while (lex.token==Lexer.times || lex.token==Lexer.div || lex.token==Lexer.mod)
{ int op = lex.token;
lex.getToken();
if (op==Lexer.times)
result = makeTimesTree(result, parseOpd(idsAllowed));
else if (op==Lexer.mod)
result = makeModTree(result, parseOpd(idsAllowed));
else
result = makeDivideTree(result, parseOpd(idsAllowed));
}
}
return result;
}
private ExpTree parseOpd(boolean idsAllowed)
{ ExpTree result;
switch(lex.token)
{ case Lexer.num:
result = makeNumberLeaf(lex.numval);
lex.getToken();
return result;
case Lexer.id:
if (!idsAllowed)
throw new ParseException("identifier not allowed in identifier defintion");
result = makeIdLeaf(lex.idval);
lex.getToken();
return result;
case Lexer.lp:
lex.getToken();
result = parseExp(idsAllowed);
if (lex.token!=Lexer.rp)
throw new ParseException("right parenthesis expected");
lex.getToken();
return result;
default:
throw new ParseException("invalid operand");
}
}
private ExpTree parseDefList()
{ ExpTree result = parseDef();
while (lex.token==Lexer.and)
{ lex.getToken();
result = makeAndTree(result, parseDef());
}
return result;
}
private ExpTree parseDef()
{
if (lex.token!=Lexer.id)
throw new ParseException("definition must start with identifier");
char id = lex.idval;
if (Character.isUpperCase(id))
throw new ParseException("upper-case identifiers cannot be used in defintion list");
lex.getToken();
if (lex.token!=Lexer.eq)
throw new ParseException("'=' expected");
lex.getToken();
return makeDefTree(id, parseExp(false));
}
static ExpTree makeNumberLeaf(int n)
{
return new ExpTree(ExpTree.NUM, n, null, null);
// this method should return a new number leaf with value n created using your constructor
// if you've used the abstract class approach you will probably need something like
// return new NumLeaf(n);
// if you've used an ExpTree class that stores the node kind you will probably need something like
// return new ExpTree(ExpTree.numNode, n , null, null);
}
static ExpTree makeIdLeaf(char c)
{
return new ExpTree(ExpTree.VAL, c, null, null);
// this method should return a new id leaf with value c
}
static ExpTree makePlusTree(ExpTree l, ExpTree r)
{
return new ExpTree(ExpTree.OP, '+', l, r);
// this method should return a new plus node with children l and r created using our constructor
// if you've used the abstract class approach you will probably need something like
// return new OpNode('+', l, r);
// or
// return new PlusNode(l, r);
// if you've used an ExpTree class that stores the node kind you will probably need something like
// return new ExpTree(ExpTree.opMode, '+', l, r);
}
public static ExpTree makeMinusTree(ExpTree l, ExpTree r)
{
return new ExpTree(ExpTree.OP, '-', l, r);
// this method should return a new minus node with children l and r
}
public static ExpTree makeTimesTree(ExpTree l, ExpTree r)
{
return new ExpTree(ExpTree.OP, '*', l, r);
// this method should return a new times node with children l and r
}
public static ExpTree makeDivideTree(ExpTree l, ExpTree r)
{
return new ExpTree(ExpTree.OP, '/', l, r);
// this method should return a new divide node with children l and r
}
public static ExpTree makeModTree(ExpTree l, ExpTree r)
{
return new ExpTree(ExpTree.OP, '%', l, r);
// this method should return a new mod (%) node with children l and r
}
public static ExpTree makeWhereTree(ExpTree l, ExpTree r)
{
// remove the following line if you modify this method; leave it here if you do not attempt part 6
System.out.println("Part 6 not attempted");
return null;
// this method should return a new 'where' node with children l and r
}
public static ExpTree makeAndTree(ExpTree l, ExpTree r)
{
return null;
// this method should return a new 'and' node with children l and r
}
public static ExpTree makeDefTree(char c, ExpTree t)
{
return null;
// this method should return a new definition node with identifier c and child t
// if your definition nodes have 2 children you should put a new id leaf in the left child and use t as the right child
}
}