forked from aashiks/Slang4Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRDParser.java
More file actions
722 lines (564 loc) · 21.6 KB
/
Copy pathRDParser.java
File metadata and controls
722 lines (564 loc) · 21.6 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package slang4java.lexer;
import java.util.ArrayList;
import slang4java.builders.*;
import slang4java.compilationunits.TModule;
import slang4java.contexts.CompilationContext;
import slang4java.expressions.*;
import slang4java.metainfo.RelationalOperators;
import slang4java.metainfo.SymbolInfo;
import slang4java.metainfo.Token;
import slang4java.metainfo.TypeInfo;
import slang4java.procedures.*;
import slang4java.statements.*;
import slang4java.support.*;
/**
*
* @author aashiks
*/
public class RDParser extends Lexer {
TModuleBuilder prog = null;
public RDParser(String Expr) {
super(Expr);
prog = new TModuleBuilder();
}
private RelationalOperators GetRelationalOperator(Token tok) {
if (tok == Token.TOK_EQ) {
return RelationalOperators.TOK_EQ;
} else if (tok == Token.TOK_NEQ) {
return RelationalOperators.TOK_NEQ;
} else if (tok == Token.TOK_GT) {
return RelationalOperators.TOK_GT;
} else if (tok == Token.TOK_GTE) {
return RelationalOperators.TOK_GTE;
} else if (tok == Token.TOK_LT) {
return RelationalOperators.TOK_LT;
} else {
return RelationalOperators.TOK_LTE;
}
}
public AbstractExpression LExpr(ProcedureBuilder pb) throws Exception {
Token l_token;
AbstractExpression RetValue = Expression(pb);
while (Current_Token == Token.TOK_GT
|| Current_Token == Token.TOK_LT
|| Current_Token == Token.TOK_GTE
|| Current_Token == Token.TOK_LTE
|| Current_Token == Token.TOK_NEQ
|| Current_Token == Token.TOK_EQ) {
l_token = Current_Token;
Current_Token = GetNext();
AbstractExpression e2 = Expression(pb);
RelationalOperators relop = GetRelationalOperator(l_token);
RetValue = new RelationalExpression(relop, RetValue, e2);
}
return RetValue;
}
public AbstractExpression BinaryExpression(ProcedureBuilder pb) throws Exception {
Token l_token;
AbstractExpression RetValue = LExpr(pb);
while (Current_Token == Token.TOK_AND || Current_Token == Token.TOK_OR) {
l_token = Current_Token;
Current_Token = GetNext();
AbstractExpression e2 = LExpr(pb);
RetValue = new LogicalExpression(l_token, RetValue, e2);
}
return RetValue;
}
public AbstractExpression Expression(ProcedureBuilder pb) throws Exception {
Token l_token;
AbstractExpression RetValue = Term(pb);
while (Current_Token == Token.TOK_PLUS || Current_Token == Token.TOK_SUB) {
l_token = Current_Token;
Current_Token = GetToken();
AbstractExpression e1 = Expression(pb);
if (l_token == Token.TOK_PLUS) {
RetValue = new BinaryPlus(RetValue, e1);
} else {
RetValue = new BinaryMinus(RetValue, e1);
}
}
return RetValue;
}
public AbstractExpression Term(ProcedureBuilder pb) throws Exception {
Token l_token;
AbstractExpression RetValue = Factor(pb);
while (Current_Token == Token.TOK_MUL || Current_Token == Token.TOK_DIV) {
l_token = Current_Token;
Current_Token = GetToken();
AbstractExpression e1 = Term(pb);
if (l_token == Token.TOK_MUL) {
RetValue = new Multiply(RetValue, e1);
} else {
RetValue = new Division(RetValue, e1);
}
}
return RetValue;
}
public AbstractExpression Factor(ProcedureBuilder pb) throws Exception {
Token l_token;
AbstractExpression RetValue = null;
if (Current_Token == Token.TOK_NUMERIC) {
RetValue = new NumericConstant(GetNumber());
Current_Token = GetToken();
} else if (Current_Token == Token.TOK_STRING) {
RetValue = new StringLiteral(last_str);
Current_Token = GetToken();
} else if (Current_Token == Token.TOK_BOOL_FALSE
|| Current_Token == Token.TOK_BOOL_TRUE) {
RetValue = new BooleanConstant(
Current_Token == Token.TOK_BOOL_TRUE ? true : false);
Current_Token = GetToken();
} else if (Current_Token == Token.TOK_OPAREN) {
Current_Token = GetToken();
RetValue = BinaryExpression(pb); // Recurse
if (Current_Token != Token.TOK_CPAREN) {
System.out.println("Missing Closing Parenthesis\n");
throw new Exception();
}
Current_Token = GetToken();
} else if (Current_Token == Token.TOK_PLUS || Current_Token == Token.TOK_SUB) {
l_token = Current_Token;
Current_Token = GetToken();
RetValue = Factor(pb);
if (l_token == Token.TOK_PLUS) {
RetValue = new UnaryPlus(RetValue);
} else {
RetValue = new UnaryMinus(RetValue);
}
} else if (Current_Token == Token.TOK_NOT) {
l_token = Current_Token;
Current_Token = GetToken();
RetValue = Factor(pb);
RetValue = new LogicalNot(RetValue);
} else if (Current_Token == Token.TOK_UNQUOTED_STRING) {
///
/// Variables
///
String str = super.last_str;
if (!prog.IsFunction(str)) {
//
// if it is not a function..it ought to
// be a variable...
SymbolInfo inf = pb.GetSymbol(str);
if (inf == null) {
throw new Exception("Undefined symbol");
}
GetNext();
return new Variable(inf);
}
//
// P can be null , if we are parsing a
// recursive function call
//
Procedure p = prog.GetProc(str);
// It is a Function Call
// Parse the function invocation
//
AbstractExpression ptr = ParseCallProc(pb, p);
GetNext();
return ptr;
} else {
System.out.println("Illegal Token");
throw new Exception();
}
return RetValue;
}
public AbstractExpression ParseCallProc(ProcedureBuilder pb, Procedure p) throws Exception {
GetNext();
if (Current_Token != Token.TOK_OPAREN) {
throw new Exception("Opening Parenthesis expected");
}
GetNext();
ArrayList actualparams = new ArrayList();
while (true) {
// Evaluate Each Expression in the
// parameter list and populate actualparams
// list
AbstractExpression exp = BinaryExpression(pb);
// do type analysis
exp.TypeCheck(pb.getContext());
// if , there are more parameters
if (Current_Token == Token.TOK_COMMA) {
actualparams.add(exp);
GetNext();
continue;
}
if (Current_Token != Token.TOK_CPAREN) {
throw new Exception("Expected paranthesis");
} else {
// Add the last parameters
actualparams.add(exp);
break;
}
}
// if p is null , that means it is a
// recursive call. Being a one pass
// compiler , we need to wait till
// the parse process to be over to
// resolve the Procedure.
//
//
if (p != null) {
return new CallExpression(p, actualparams);
} else {
return new CallExpression(pb.getProcedureName(),
true, // recurse !
actualparams,
pb.GetProcedure()._type);
}
}
/// <returns></returns>
public ArrayList Parse(ProcedureBuilder pb) throws Exception {
GetNext(); // Get the Next Token
//
// Parse all the statements
//
return StatementList(pb);
}
public TModule DoParse() {
try {
// ProcedureBuilder p = new ProcedureBuilder("MAIN", new CompilationContext());
// ArrayList stmts = Parse(p);
//
// for (Object s : stmts) {
// p.AddStatement((Statement) s);
// }
//
// Procedure pc = p.GetProcedure();
//
// prog.Add(pc);
// return prog.GetProgram();
GetNext(); // Get The First Valid Token
return ParseFunctions();
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
public TModule ParseFunctions() throws Exception {
while (Current_Token == Token.TOK_FUNCTION) {
ProcedureBuilder b = ParseFunction();
Procedure s = b.GetProcedure();
if (s == null) {
System.out.println("Error While Parsing Functions");
return null;
}
prog.Add(s);
GetNext();
}
//
// Convert the builder into a program
//
return prog.GetProgram();
}
ProcedureBuilder ParseFunction() throws Exception {
//
// Create a Procedure builder Object
//
ProcedureBuilder p = new ProcedureBuilder("", new CompilationContext());
if (Current_Token != Token.TOK_FUNCTION) {
return null;
}
GetNext();
// return type of the Procedure ought to be
// Boolean , Numeric or String
if (!(Current_Token == Token.TOK_VAR_BOOL
|| Current_Token == Token.TOK_VAR_NUMBER
|| Current_Token == Token.TOK_VAR_STRING)) {
return null;
}
//-------- Assign the return type
p.setTypeInfo((Current_Token == Token.TOK_VAR_BOOL)
? TypeInfo.TYPE_BOOL : (Current_Token == Token.TOK_VAR_NUMBER)
? TypeInfo.TYPE_NUMERIC : TypeInfo.TYPE_STRING);
// Parse the name of the Function call
GetNext();
if (Current_Token != Token.TOK_UNQUOTED_STRING) {
return null;
}
p.setProcedureName(this.last_str); // assign the name
// ---------- Opening parenthesis for
// the start of <paramlist>
GetNext();
if (Current_Token != Token.TOK_OPAREN) {
return null;
}
//---- Parse the Formal Parameter list
FormalParameters(p);
if (Current_Token != Token.TOK_CPAREN) {
return null;
}
GetNext();
// --------- Parse the Function code
ArrayList lst = StatementList(p);
if (Current_Token != Token.TOK_END) {
throw new Exception("END expected");
}
// Accumulate all statements to
// Procedure builder
//
for (Object o : lst) {
Statement s = (Statement) o;
p.AddStatement(s);
}
return p;
}
void FormalParameters(ProcedureBuilder pb) throws Exception {
if (Current_Token != Token.TOK_OPAREN) {
throw new Exception("Opening Parenthesis expected");
}
GetNext();
ArrayList lst_types = new ArrayList();
while (Current_Token == Token.TOK_VAR_BOOL
|| Current_Token == Token.TOK_VAR_NUMBER
|| Current_Token == Token.TOK_VAR_STRING) {
SymbolInfo inf = new SymbolInfo();
inf.Type = (Current_Token == Token.TOK_VAR_BOOL)
? TypeInfo.TYPE_BOOL : (Current_Token == Token.TOK_VAR_NUMBER)
? TypeInfo.TYPE_NUMERIC : TypeInfo.TYPE_STRING;
GetNext();
if (Current_Token != Token.TOK_UNQUOTED_STRING) {
throw new Exception("Variable Name expected");
}
inf.SymbolName = this.last_str;
lst_types.add(inf.Type);
pb.AddFormals(inf);
pb.AddLocal(inf);
GetNext();
if (Current_Token != Token.TOK_COMMA) {
break;
}
GetNext();
}
prog.AddFunctionProtoType(pb.getProcedureName(), pb.getTypeInfo(), lst_types);
return;
}
/// The Grammar is
///
/// <stmtlist> := { <statement> }+
///
/// {<statement> := <printstmt> | <printlinestmt>
/// <printstmt> := print <expr >;
///
/// <printlinestmt>:= printline <expr>;
///
/// <Expr> ::= <Term> | <Term> { + | - } <Expr>
/// <Term> ::= <Factor> | <Factor> {*|/} <Term>
/// <Factor>::= <number> | ( <expr> ) | {+|-} <factor>
///
private ArrayList StatementList(ProcedureBuilder pb) throws Exception {
ArrayList arr = new ArrayList();
while ((Current_Token != Token.TOK_ELSE)
&& (Current_Token != Token.TOK_ENDIF)
&& (Current_Token != Token.TOK_WEND)
&& (Current_Token != Token.TOK_END)) {
Statement temp = Statement(pb);
if (temp != null) {
arr.add(temp);
}
}
return arr;
}
private Statement Statement(ProcedureBuilder pb) throws Exception {
Statement retval = null;
switch (this.Current_Token) {
case TOK_VAR_STRING:
case TOK_VAR_NUMBER:
case TOK_VAR_BOOL:
retval = ParseVariableDeclStatement(pb);
GetNext();
return retval;
case TOK_PRINT:
retval = ParsePrintStatement(pb);
GetNext();
break;
case TOK_PRINTLN:
retval = ParsePrintLNStatement(pb);
GetNext();
break;
case TOK_UNQUOTED_STRING:
retval = ParseAssignmentStatement(pb);
GetNext();
return retval;
case TOK_IF:
retval = ParseIfStatement(pb);
GetNext();
return retval;
case TOK_WHILE:
retval = ParseWhileStatement(pb);
GetNext();
return retval;
case TOK_RETURN:
retval = ParseReturnStatement(pb);
GetNext();
return retval;
default:
throw new Exception("Invalid statement");
}
return retval;
}
/// Parse the Print Staement .. The grammar is
/// PRINT <expr> ;
/// Once you are in this subroutine , we are expecting
/// a valid expression ( which will be compiled ) and a
/// semi collon to terminate the line..
/// Once Parse Process is successful , we create a PrintStatement
/// Object..
private Statement ParsePrintStatement(ProcedureBuilder pb) throws Exception {
GetNext();
AbstractExpression a = BinaryExpression(pb);
a.TypeCheck(pb.getContext());
if (Current_Token != Token.TOK_SEMI) {
throw new Exception("; is expected");
}
return new PrintStatement(a);
}
/// Parse the PrintLine Staement .. The grammar is
/// PRINTLINE <expr> ;
/// Once you are in this subroutine , we are expecting
/// a valid expression ( which will be compiled ) and a
/// semi collon to terminate the line..
/// Once Parse Process is successful , we create a PrintLineStatement
/// Object..
private Statement ParsePrintLNStatement(ProcedureBuilder pb) throws Exception {
GetNext();
AbstractExpression a = Expression(pb);
a.TypeCheck(pb.getContext());
if (Current_Token != Token.TOK_SEMI) {
throw new Exception("; is expected");
}
return new PrintLineStatement(a);
}
public Statement ParseVariableDeclStatement(ProcedureBuilder pb) throws Exception {
//--- Save the Data type
Token tok = Current_Token;
// --- Skip to the next token , the token ought
// to be a Variable name ( UnQouted String )
GetNext();
if (Current_Token == Token.TOK_UNQUOTED_STRING) {
SymbolInfo symb = new SymbolInfo();
symb.SymbolName = super.last_str;
symb.Type = (tok == Token.TOK_VAR_BOOL)
? TypeInfo.TYPE_BOOL : (tok == Token.TOK_VAR_NUMBER)
? TypeInfo.TYPE_NUMERIC : TypeInfo.TYPE_STRING;
//---------- Skip to Expect the SemiColon
GetNext();
if (Current_Token == Token.TOK_SEMI) {
// ----------- Add to the Symbol Table
// for type analysis
pb.getSymbolTable().Add(symb);
// --------- return the Object of type
// --------- VariableDeclStatement
// This will just store the Variable name
// to be looked up in the above table
return new VariableDeclStatement(symb);
} else {
CSyntaxErrorLog.AddLine("; expected");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, ", or ; expected", SaveIndex());
}
} else {
CSyntaxErrorLog.AddLine("invalid variable declaration");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, ", or ; expected", SaveIndex());
}
}
public Statement ParseAssignmentStatement(ProcedureBuilder pb) throws Exception {
//
// Retrieve the variable and look it up in
// the symbol table ..if not found throw exception
//
String variable = super.last_str;
SymbolInfo s = pb.getSymbolTable().Get(variable);
if (s == null) {
CSyntaxErrorLog.AddLine("Variable not found " + last_str);
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, "Variable not found", SaveIndex());
}
//------------ The next token ought to be an assignment
// expression....
GetNext();
if (Current_Token != Token.TOK_ASSIGN) {
CSyntaxErrorLog.AddLine("= expected");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, "= expected", SaveIndex());
}
//-------- Skip the token to start the expression
// parsing on the RHS
GetNext();
AbstractExpression exp = BinaryExpression(pb);
//------------ Do the type analysis ...
if (exp.TypeCheck(pb.getContext()) != s.Type) {
throw new Exception("Type mismatch in assignment");
}
// -------------- End of statement ( ; ) is expected
if (Current_Token != Token.TOK_SEMI) {
CSyntaxErrorLog.AddLine("; expected");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, " ; expected", -1);
}
// return an instance of AssignmentStatement node..
// s => Symbol info associated with variable
// exp => to evaluated and assigned to symbol_info
return new AssignmentStatement(s, exp);
}
public Statement ParseIfStatement(ProcedureBuilder pb) throws Exception {
GetNext();
ArrayList true_part;
true_part = null;
ArrayList false_part;
false_part = null;
AbstractExpression exp = BinaryExpression(pb); // Evaluate Expression
if (pb.TypeCheck(exp) != TypeInfo.TYPE_BOOL) {
throw new Exception("Expects a boolean expression");
}
if (Current_Token != Token.TOK_THEN) {
CSyntaxErrorLog.AddLine(" Then Expected");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, "Then Expected", SaveIndex());
}
GetNext();
true_part = StatementList(pb);
if (Current_Token == Token.TOK_ENDIF) {
return new IfStatement(exp, true_part, false_part);
}
if (Current_Token != Token.TOK_ELSE) {
throw new Exception("ELSE expected");
}
GetNext();
false_part = StatementList(pb);
if (Current_Token != Token.TOK_ENDIF) {
throw new Exception("END IF EXPECTED");
}
return new IfStatement(exp, true_part, false_part);
}
public Statement ParseWhileStatement(ProcedureBuilder pb) throws Exception {
GetNext();
AbstractExpression exp = BinaryExpression(pb);
if (pb.TypeCheck(exp) != TypeInfo.TYPE_BOOL) {
throw new Exception("Expects a boolean expression");
}
ArrayList body = StatementList(pb);
if ((Current_Token != Token.TOK_WEND)) {
CSyntaxErrorLog.AddLine("Wend Expected");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, "Wend Expected", SaveIndex());
}
return new WhileStatement(exp, body);
}
public Statement ParseReturnStatement(ProcedureBuilder pb) throws Exception {
GetNext();
AbstractExpression exp = BinaryExpression(pb);
if (Current_Token != Token.TOK_SEMI) {
CSyntaxErrorLog.AddLine("; expected");
CSyntaxErrorLog.AddLine(GetCurrentLine(SaveIndex()));
throw new CParserException(-100, " ; expected", -1);
}
pb.TypeCheck(exp);
return new ReturnStatement(exp);
}
}