-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
446 lines (421 loc) · 10.4 KB
/
Parser.java
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* *** This file is given as part of the programming assignment. *** */
import java.util.*;//needed to import the ArrayList class
public class Parser {
// tok is global to all these parsing methods;
// scan just calls the scanner's scan method and saves the result in tok.
private Token tok; // the current token
private void scan() {
tok = scanner.scan();
}
private ArrayList<SymbolTableItem> sym_table;
private Scan scanner;
Parser(Scan scanner) {
this.scanner = scanner;
scan();
program();
if( tok.kind != TK.EOF )
parse_error("junk after logical end of program");
}
private void enterScope()
{
/*
Function that increments the current scope of the program upon entering a new scope
*/
SymbolTableItem.current_scope++;
}
private void exitScope()
{
/*
Function that decrements the current scope of the program and also updates the symbol table upon exiting a scope
*/
if(!sym_table.isEmpty())
{
while(sym_table.get(sym_table.size()-1).scope_level==SymbolTableItem.current_scope)
{
sym_table.remove(sym_table.size()-1);
}
}
SymbolTableItem.current_scope--;
}
private void program() {
sym_table=new ArrayList<SymbolTableItem>();//creates an empty symbol table for the entire program.
System.out.println("public class My_e2j{");
System.out.println("public static void main(String[] args){");
block();
System.out.println("}");
System.out.println("}");
sym_table.clear();//removes all the elements from the symbol table
}
private void block(){
declaration_list();
statement_list();
}
private void declaration_list() {
// below checks whether tok is in first set of declaration.
// here, that's easy since there's only one token kind in the set.
// in other places, though, there might be more.
// so, you might want to write a general function to handle that.
while( is(TK.DECLARE) ) {
declaration();
}
}
private void declaration() {
mustbe(TK.DECLARE);
boolean isFirst=false;
System.out.println("int");
if(sym_table.isEmpty())
{
sym_table.add(new SymbolTableItem(tok.string));
System.out.println(tok.string+"_"+SymbolTableItem.current_scope);
}
else
{
int flag=0;
for(int i=0;i<sym_table.size();i++)
{
if(sym_table.get(i).variable.equals(tok.string))
{
flag=1;
if(sym_table.get(i).scope_level==SymbolTableItem.current_scope)
{
System.err.println("redeclaration of variable "+tok.string);
isFirst=true;
}
else
{
sym_table.add(new SymbolTableItem(tok.string));
System.out.println(tok.string+"_"+SymbolTableItem.current_scope);
}
break;
}
}
if(flag==0)
{
sym_table.add(new SymbolTableItem(tok.string));
System.out.println(tok.string+"_"+SymbolTableItem.current_scope);
}
}
mustbe(TK.ID);
while( is(TK.COMMA) ) {
scan();
int flag=0;
for(int i=0;i<sym_table.size();i++)
{
if(sym_table.get(i).variable.equals(tok.string))
{
flag=1;
if(sym_table.get(i).scope_level==SymbolTableItem.current_scope)
{
System.err.println("redeclaration of variable "+tok.string);
}
else
{
sym_table.add(new SymbolTableItem(tok.string));
if(!isFirst)
{
System.out.println(",");
}
System.out.println(tok.string+"_"+SymbolTableItem.current_scope);
}
break;
}
}
if(flag==0)
{
sym_table.add(new SymbolTableItem(tok.string));
if(!isFirst)
{
System.out.println(",");
}
System.out.println(tok.string+"_"+SymbolTableItem.current_scope);
}
mustbe(TK.ID);
}
System.out.println(";");
}
private void statement_list() {
//statement_list::={statement}
while(is(TK.ID)||is(TK.TILDE)||is(TK.PRINT)||is(TK.DO)||is(TK.IF)){
statement();
}
}
private void statement()
{
//statement::=assignment|print|do|if
if(is(TK.ID)||is(TK.TILDE))
{
assignment();
}
else if(is(TK.PRINT))
{
print();
}
else if(is(TK.DO))
{
Do();
}
else if(is(TK.IF))
{
If();
}
/*else
{
parse_error("Not a valid statement");
}*/
}
private void print()
{
//print::='!' expr
mustbe(TK.PRINT);
System.out.println("System.out.println(");
expr();
System.out.println(");");
}
private void assignment()
{
//assignment::= ref_id '=' expr
ref_id();
mustbe(TK.ASSIGN);
System.out.println("=");
expr();
System.out.println(";");
}
private void ref_id()
{
//ref_id::=['~'[number]] id
String var_name="";
String num="-1";
int check_scope_level=-1000;
if(is(TK.TILDE))
{
var_name+=tok.string;
scan();
if(is(TK.NUM))
{
var_name+=tok.string;
num=tok.string;
scan();
}
}
var_name+=tok.string;
if(var_name.charAt(0)=='~')
{
int num_int=Integer.parseInt(num);
check_scope_level=SymbolTableItem.current_scope-num_int;
if(check_scope_level<0)
{
System.err.println("no such variable "+var_name+" on line "+tok.lineNumber);
System.exit(1);
}
else
{
if(num.equals("-1"))
{
check_scope_level=0;
}
else
{
check_scope_level=SymbolTableItem.current_scope-num_int;
}
int flag=0;
for(int i=0;i<sym_table.size();i++)
{
if(sym_table.get(i).scope_level==check_scope_level)
{
if(sym_table.get(i).variable.equals(tok.string))
{
flag=1;
break;
}
}
}
if(flag==0)
{
System.err.println("no such variable "+var_name+" on line "+tok.lineNumber);
System.exit(1);
}
}
}
int flag=0;
for(int i=0;i<sym_table.size();i++)
{
if(sym_table.get(i).variable.equals(tok.string))
{
flag=1;
break;
}
}
if(flag==0)
{
System.err.println(tok.string+" is an undeclared variable on line " +tok.lineNumber);
System.exit(1);
}
if(var_name.charAt(0)=='~')
{
if(num.equals("-1"))
{
System.out.println(tok.string+"_0");
}
else
{
if(check_scope_level>=0)
{
System.out.println(tok.string+"_"+check_scope_level);
}
}
}
else
{
for(int i=sym_table.size()-1;i>=0;i--)
{
if(sym_table.get(i).variable.equals(tok.string))
{
System.out.println(tok.string+"_"+sym_table.get(i).scope_level);
break;
}
}
}
mustbe(TK.ID);
}
private void Do()
{
//do::='<' guarded_command '>'
mustbe(TK.DO);
System.out.println("while(");
guarded_command();
mustbe(TK.ENDDO);
}
private void If()
{
//if::='[' guarded_command {'|' guarded_command} [% block] ']'
mustbe(TK.IF);
System.out.println("if(");
guarded_command();
while(is(TK.ELSEIF))
{
System.out.println("else if(");
scan();
guarded_command();
}
if(is(TK.ELSE))
{
System.out.println("else{");
scan();
enterScope();
block();
System.out.println("}");
exitScope();
}
mustbe(TK.ENDIF);
}
private void guarded_command()
{
//guarded_command::= expr ':' block
expr();
System.out.println("<=0){");
enterScope();
mustbe(TK.THEN);
block();
System.out.println("}");
exitScope();
}
private void expr()
{
//expr::=term{addop term}
term();
while(is(TK.PLUS)||is(TK.MINUS))
{
addop();
term();
}
}
private void term()
{
//term::=factor{multop factor}
factor();
while(is(TK.TIMES)||is(TK.DIVIDE))
{
multop();
factor();
}
}
private void factor()
{
//factor::='(' expr ')'|ref_id|number
if(is(TK.LPAREN))
{
System.out.println("(");
scan();
expr();
mustbe(TK.RPAREN);
System.out.println(")");
}
else if(is(TK.ID)||is(TK.TILDE))
{
ref_id();
}
else
{
System.out.println(tok.string);
mustbe(TK.NUM);
}
}
private void addop()
{
//addop::='+'|'-'
if(is(TK.PLUS)||is(TK.MINUS))
{
if(is(TK.PLUS))
{
System.out.println("+");
}
else if(is(TK.MINUS))
{
System.out.println("-");
}
scan();
}
/*else
{
parse_error("Not a valid add operator");
}*/
}
private void multop()
{
//multop::='*'|'/'
if(is(TK.TIMES)||is(TK.DIVIDE))
{
if(is(TK.TIMES))
{
System.out.println("*");
}
else if(is(TK.DIVIDE))
{
System.out.println("/");
}
scan();
}
/*else
{
parse_error("Not a valid mult operator");
}*/
}
// is current token what we want?
private boolean is(TK tk) {
return tk == tok.kind;
}
// ensure current token is tk and skip over it.
private void mustbe(TK tk) {
if( tok.kind != tk ) {
System.err.println( "mustbe: want " + tk + ", got " +
tok);
parse_error( "missing token (mustbe)" );
}
scan();
}
private void parse_error(String msg) {
System.err.println( "can't parse: line "
+ tok.lineNumber + " " + msg );
System.exit(1);
}
}