Repository navigation
Expand file tree
/
Copy pathParser.java
More file actions
executable file
·815 lines (723 loc) · 21.9 KB
/
Copy pathParser.java
File metadata and controls
executable file
·815 lines (723 loc) · 21.9 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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/** Authors: Julien Hoachuck, Carolyn Chan, Tiffany Chan, Nelson Johansen
* Title: E to C Translator
**/
import java.util.*;
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 Scan scanner;
Parser(Scan scanner) {
this.scanner = scanner;
System.out.println("#include <stdio.h>");
System.out.println("#include <math.h>");
System.out.println("main() {");
scan();
program();
if( tok.kind != TK.EOF )
parse_error("junk after logical end of program");
}
//required for translator
Token prev;
//variables used to handle special cases in C syntax
int inVar = 0;
int inDo = 0;
int inST = 0;
int inFA = 0;
int inPrint = 0;
int inRoot = 0;
int saveFa1 = 0;
int saveFa2 = 0;
int sqrCount = 0;
int rootCount = 0;
//variables used to hold epxresions for later use in translation
Token itr;
String exprRoot = new String();
String expr1 = new String();
String expr2 = new String();
//translates E code to C code based on Token.kind
public void translate()
{
if((inRoot == 1) && (tok.kind != TK.ROOT))
{
if(is(TK.ID))
{
exprRoot = exprRoot + "x_" + tok.string;
}
else
{
exprRoot = exprRoot + tok.string;
}
return;
}
if(saveFa1 == 1)
{
if(is(TK.ID))
{
expr1 = expr1 + " x_" + tok.string;
}
else
{
expr1 = expr1 + tok.string;
}
}
if(saveFa2 == 1)
{
if(is(TK.ID))
{
expr2 = expr2 + " x_" + tok.string;
}
else
{
expr2 = expr2 + tok.string;
}
}
if(inRoot == 1)
{
if(is(TK.ID))
{
exprRoot = exprRoot + "x_" + tok.string;
}
else
{
exprRoot = exprRoot + tok.string;
}
}
if(tok.kind == TK.VAR)
{
inVar = 1;
}
else if(tok.kind == TK.ID)
{
if(inVar == 1)
{
System.out.println("int x_" + tok.string + " = -12345;");
}
else
{
System.out.print("x_" + tok.string + " ");
}
}
else if(tok.kind == TK.RAV)
{
inVar = 0;
}
else if(is(TK.ASSIGN))
{
System.out.print(" = ");
}
else if(is(TK.NE))
{
System.out.print(" != ");
}
else if(is(TK.EQ))
{
System.out.print(" == ");
}
else if(is(TK.LE) || is(TK.GE) || is(TK.GT) || is(TK.LT)
|| is(TK.DIVIDE) || is(TK.TIMES) ||
is(TK.MINUS) || is(TK.PLUS) || is(TK.RPAREN) || is(TK.LPAREN))
{
System.out.print(" " + tok.string + " ");
}
else if(tok.kind == TK.PRINT)
{
System.out.print("printf(" + "\"%d\\n\", ");
}
else if(tok.kind == TK.NUM)
{
System.out.print(tok.string);
}
else if(tok.kind == TK.IF)
{
System.out.print("if( ");
}
else if(tok.kind == TK.FI)
{
if(inDo == 1)
{
System.out.println("continue;");
}
System.out.println("}//end of if block");
}
else if(tok.kind == TK.DO)
{
inDo = 1;
System.out.println("while(1) {");
System.out.println("if(" );
}
else if(tok.kind == TK.OD)
{
inDo = 0;
System.out.println("continue;");
System.out.println("}//do with last gaurd for do block");
System.out.println("break;");
System.out.println("}//end of do while loop");
}
else if(tok.kind == TK.FA)
{
inFA = 1;
System.out.println("for(");
}
else if(tok.kind == TK.TO)
{
System.out.println("; x_" + itr.string + " <= " );
}
else if(tok.kind == TK.ST)
{
System.out.println("if(");
}
else if(tok.kind == TK.AF)
{
if(inST == 1)
{
System.out.println("} //end of conditional");
}
inFA = 0;
inST = 0;
System.out.println("}//end for loop");
}
else if(tok.kind == TK.ARROW)
{
if(prev.kind == TK.ELSE)
{
System.out.println("{");
}
else if(inFA == 0)
{
System.out.println(") {");
}
else
{
return;
}
}
else if(tok.kind == TK.BOX)
{
if(inDo == 1)
{
System.out.println("continue;");
}
System.out.println("}//end of if statement");
System.out.print("else if( ");
}
else if(tok.kind == TK.ELSE)
{
System.out.print("}");//close block above else
System.out.print(" else");
}
else
{
}
prev = tok;
}
//Symbol Table data structure
HashMap<String,Stack<Vars>> symTable = new HashMap<String,Stack<Vars>>();
//Holds data about which variables were declared at each depth.
HashMap<Integer,ArrayList<String>> currentDepth
= new HashMap<Integer,ArrayList<String>>();
//Data structure to preserve values for printing
ArrayList<PrintingQueue> symTablePreserved = new ArrayList<PrintingQueue>();
//Global variable to allow printing iff program was valid
boolean invalid;
//depth variable
int depth;
//keep track of previous variable for certian statements
Token currVar;
public class mySort implements Comparator<PrintingQueue> {
@Override
public int compare(PrintingQueue pQ1, PrintingQueue pQ2)
{
if(pQ1.depth >= pQ2.depth)
return 1;
else
return -1;
}
}
private void program() {
block();
System.out.print("}//end of program");
if(invalid == false)
printPreserved();
}
private void block() {
if(is(TK.VAR))
{
declarations();
}
invalid = true;
statement_list();
removeSymbols();
}
//updates or adds symbol to symbol table data structure
private void updateSymbols() {
//create a new class for the current declaration of the variable
Vars var = new Vars();
//stack to hold variables
Stack<Vars> stack = new Stack<Vars>();
if(!(symTable.containsKey(tok.string)))
{
var.ID = tok.string;
var.line_declared = tok.lineNumber;
var.nesting_depth = depth;
stack.push(var);
symTable.put(tok.string, stack);
}
else //symTable contains the key and its stack already!!!
{
stack = symTable.get(tok.string);
var.ID = tok.string;
var.line_declared = tok.lineNumber;
var.nesting_depth = depth;
stack.push(var);
symTable.put(tok.string, stack);
}
}
//when variables go out of scope remove them from symbol table
private void removeSymbols() {
//stack to hold variables
Stack<Vars> stack = new Stack<Vars>();
//holds the key values to be poped for current depth
ArrayList<String> temp = new ArrayList<String>();
//holds class being preserved currently
PrintingQueue pQ = new PrintingQueue();
//holds class being worked with
Vars var = new Vars();
temp = currentDepth.get(depth);
if(temp == null)
return;
for(String key: temp)
{
stack = symTable.get(key);
var = stack.pop();
pQ.depth = var.line_declared;
pQ.pQueue.add(var);
if(stack.isEmpty())
symTable.remove(key);
else
symTable.put(key, stack);
}
symTablePreserved.add(pQ);
}
//prints out the variables on symbol table in declared order
private void printPreserved() {
//used to hold Objects coming off stack
Vars var = new Vars();
//Variable to hold queue being printed currently
Queue<Vars> pQ = new LinkedList<Vars>();
//Variables for printing out correct values in ()
int prev = -1;
int counter = 1;
int size = 0;
String current = null;
Collections.sort(symTablePreserved, new mySort());
for(PrintingQueue p : symTablePreserved)
{
pQ = p.pQueue;
while(!(pQ.isEmpty()))
{
size = 0;
var = pQ.remove();
System.err.println(var.ID);
System.err.println(" declared on line " + var.line_declared +
" at nesting depth " + var.nesting_depth);
if(var.assigned.isEmpty())
{
System.err.println(" never assigned");
}
else
{
System.err.print(" assigned to on:");
for(Integer x : var.assigned)
{
if(var.ID != current)
{
prev = -1;
counter = 1;
current = var.ID;
}
if(x == prev && size < (var.assigned.size()-1))
{
counter++;
}
else if(x != prev && counter == 1)
{
System.err.print(" " + x);
}
else if(x != prev && counter > 1)
{
System.err.print("(" + counter + ")");
System.err.print(" " + x);
counter = 1;
}
else
{
counter++;
System.err.print("(" + counter + ")");
}
prev = x;
size++;
}
System.err.println();
}
if(var.used.isEmpty())
{
System.err.println(" never used");
}
else
{
System.err.print(" used on:");
prev = -1;
counter = 1;
size = 0;
current = null;
for(Integer y : var.used)
{
if(var.ID != current)
{
prev = -1;
counter = 1;
current = var.ID;
}
if(y == prev && size < (var.used.size()-1))
{
counter++;
}
else if(y != prev && counter == 1)
{
System.err.print(" " + y);
}
else if(y != prev && counter > 1)
{
System.err.print("(" + counter + ")");
System.err.print(" " + y);
counter = 1;
}
else
{
counter++;
System.err.print("(" + counter + ")");
}
prev = y;
size++;
}
System.err.println();
}//end of print block
} //end of while
}//end of for
}
//when a variable is assigned update its information on symbol table
private void variableAssigned() {
//create a new class for the current declaration of the variable
Vars var = new Vars();
//stack to hold variables
Stack<Vars> stack = new Stack<Vars>();
if(symTable.containsKey(tok.string))
{
stack = symTable.get(tok.string);
var = stack.pop();
var.assigned.add(tok.lineNumber);
stack.push(var);
symTable.put(tok.string, stack);
}
else
{
System.err.println("undeclared variable " + tok.string +" on line "
+ tok.lineNumber);
System.exit(1);
}
}
//when a variable is used update information on symbol table
private void variableUsed(Token t) {
//create a new class for the current declaration of the variable
Vars var = new Vars();
//stack to hold symTable stack
Stack<Vars> stack = new Stack<Vars>();
if(symTable.containsKey(t.string))
{
stack = symTable.get(t.string);
var = stack.pop();
var.used.add(t.lineNumber);
stack.push(var);
symTable.put(t.string, stack);
}
else
{
System.err.println("undeclared variable " + t.string +" on line "
+ t.lineNumber);
System.exit(1);
}
}
private void declarations() {
translate(); //gen code for start of variable declartion
mustbe(TK.VAR);
//redeclartion checking structure and current block variables to pop
//before leaving scope
ArrayList<String> sentinel = new ArrayList<String>();
while( is(TK.ID) ) {
//sentinel holds all variable IDs for current var rav statement.
if(!(sentinel.contains(tok.string)))
{
sentinel.add(tok.string);
updateSymbols();
translate(); //gen code for each ID
}
else
{
System.err.println("variable " + tok.string +
" is redeclared on line " + tok.lineNumber);
}
scan();
}
currentDepth.put(depth, sentinel);
translate(); //gen code for end of variable declartion
mustbe(TK.RAV);
}
private void statement_list() {
while(is(TK.ID) || is(TK.PRINT) || is(TK.IF) || is(TK.DO) || is(TK.FA))
{
invalid = false;
statement();
}
}
private void statement() {
if(is(TK.ID))
{
variableAssigned();
assign();
}
else if(is(TK.PRINT))
{
print();
}
else if(is(TK.IF))
{
depth++;
currentDepth.put(depth, null);
x_if();
depth--;
}
else if(is(TK.DO))
{
depth++;
currentDepth.put(depth, null);
x_do();
depth--;
}
else
{
depth++;
currentDepth.put(depth, null);
x_fa();
depth--;
}
}
private void assign() {
translate(); //gen code for ID
mustbe(TK.ID);
translate(); //gen code for ASSIGN
mustbe(TK.ASSIGN);
expr();
System.out.println(";"); //create code for end of assignment statement
}
private void print() {
inPrint = 1;
translate(); //gen code is printf("%d\n",
mustbe(TK.PRINT);
expr();
System.out.println(");"); //creat code for end of print statement
inPrint = 0;
}
private void x_if() {
translate(); //gen code for if
mustbe(TK.IF);
guarded_commands();
translate(); //gen code for fi
mustbe(TK.FI);
}
private void x_do() {
translate(); //gen code for do
mustbe(TK.DO);
guarded_commands();
translate(); //gen code for od
mustbe(TK.OD);
}
private void x_fa() {
expr1 = "";
expr2 = "";
translate(); //gen code for fa
mustbe(TK.FA);
variableAssigned();
translate(); //gen code for ID of fa
itr = tok; // hold onto the iterator variable to generator for loop
mustbe(TK.ID);
translate(); //gen ASSIGN
mustbe(TK.ASSIGN);
saveFa1 = 1;
expr();
saveFa1 = 0;
translate(); //gen TO
mustbe(TK.TO);
saveFa2 = 1;
expr();
saveFa2 = 0;
System.out.println("; " + "x_" +itr.string + "++" +") {");
System.out.println("if(" + expr1 + " > " + expr2 + ") {");
System.out.println(" break;");
System.out.println("}");
if(is(TK.ST))
{
inST = 1;
translate();// gen ST
mustbe(TK.ST);
expr();
System.out.println(") {");
}
commands();
translate(); // gen AF
mustbe(TK.AF);
}
private void expr() {
simple();
if(is(TK.EQ) || is(TK.LT) || is(TK.GT) || is(TK.NE)
|| is(TK.LE) || is(TK.GE))
{
translate(); //gen code for operators
scan();
simple();
}
}
private void simple() {
term();
while(is(TK.PLUS) || is(TK.MINUS))
{
translate(); //gen code for operators
scan();
term();
}
}
private void term() {
factor();
while(is(TK.TIMES) || is(TK.DIVIDE))
{
translate(); //gen code for operators
scan();
factor();
}
}
private void factor() {
if(is(TK.LPAREN))
{
translate(); //gen code for (
mustbe(TK.LPAREN);
expr();
translate(); //gen code for )
mustbe(TK.RPAREN);
}
else if(is(TK.ID))
{
//hold and check current tok to see if declared
currVar = tok;
if(!(symTable.containsKey(tok.string)))
{
System.err.println("undeclared variable " + tok.string +
" on line " + tok.lineNumber);
System.exit(1);
}
variableUsed(tok);
translate(); //gen code for ID
mustbe(TK.ID);
}
else if(is(TK.NUM))
{
translate(); //gen code for NUM
mustbe(TK.NUM);
}
else if(is(TK.SQR))
{
if(inRoot == 1)
{
exprRoot = exprRoot + "(int) pow(";
}
else
{
System.out.print("(int)");
System.out.print("pow( ");
}
mustbe(TK.SQR);
sqrCount++;
expr();
sqrCount--;
if(inRoot == 1)
{
exprRoot = exprRoot + ",2)";
}else{
System.out.print(",2)");
}
}
else if(is(TK.ROOT))
{
exprRoot = "";
inRoot = 1;
System.out.print("(int)");
System.out.print("sqrt( ");
mustbe(TK.ROOT);
expr();
if((inRoot == 1) && (prev.kind != TK.ROOT))
{
System.out.print(exprRoot + " < 0 ? 0 : " + exprRoot);
inRoot = 0;
}
System.out.print(" )");
}
else
{
parse_error("factor");
}
}
private void guarded_commands() {
guarded_command();
while(is(TK.BOX))
{
translate(); //gen else if code for BOX
mustbe(TK.BOX);
guarded_command();
}
if(is(TK.ELSE))
{
translate(); //gen code for else
mustbe(TK.ELSE);
commands();
}
}
private void guarded_command() {
expr();
commands();
}
private void commands() {
translate(); //gen code for ARROW
mustbe(TK.ARROW);
block();
}
// 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( ! is(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);
}
}