-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisp.y
808 lines (755 loc) · 17.2 KB
/
lisp.y
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
%{
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <stack>
#include <cstring>
#include "SymbolTable.h"
using namespace std;
// Classification for operators
#define ARITHMETIC_OP 1
#define LOGICAL_OP 2
#define RELATIONAL_OP 3
#define PLUS 12
#define MULT 13
#define DIV 14
#define SUB 15
#define OR 16
#define AND 17
#define LE 18
#define GE 19
#define LT 20
#define GT 21
#define NE 22
#define EQ 23
#define NOT 24
#define TRUE true
#define nil false
// Error messages
#define UNDEFINED_IDENT "Undefined identifier"
#define MULTIPLY_DEFINED_IDENT "Multiply defined identifier"
#define MUST_BE_INTEGER1 "Arg 1 must be integer"
#define MUST_BE_INTEGER2 "Arg 2 must be integer"
#define MUST_BE_STRING2 "Arg 2 must be string"
#define MUST_BE_INT_OR_STR1 "Arg 1 must be integer or string"
#define MUST_BE_INT_OR_STR2 "Arg 2 must be integer or string"
// Control output (1 to produce designated output; 0 otherwise)
#define OUTPUT_TOKENS 0
#define OUTPUT_PRODUCTIONS 0
#define OUTPUT_ST_MGT 1
// Global variables
int lineNum = 1;
//char* input = [];
stack<SYMBOL_TABLE> scopeStack; // stack of scope hashtables
// Function prototypes
bool isIntCompatible(const int theType);
bool isStrCompatible(const int theType);
bool isIntOrStrCompatible(const int theType);
void beginScope();
void endScope();
void cleanUp();
void prepareToTerminate();
void bail();
TYPE_INFO findEntryInAnyScope(const string theName);
void printRule(const char*, const char*);
int yyerror(const char* s)
{
printf("Line %d: %s\n", lineNum, s);
bail();
}
extern "C" {
int yyparse(void);
int yylex(void);
int yywrap() {return 1;}
}
%}
%union {
char* text;
int num;
TYPE_INFO typeInfo;
int opType;
};
/*
* Token declarations
*/
%token T_LPAREN T_RPAREN
%token T_IF T_LETSTAR T_PRINT T_INPUT
%token T_ADD T_SUB T_MULT T_DIV T_EXIT T_PROGN
%token T_LT T_GT T_LE T_GE T_EQ T_NE T_AND T_OR T_NOT
%token T_INTCONST T_STRCONST T_T T_NIL T_IDENT T_UNKNOWN
%type <text> T_IDENT T_STRCONST
%type <typeInfo> N_EXPR N_PARENTHESIZED_EXPR N_ARITHLOGIC_EXPR
%type <typeInfo> N_CONST N_IF_EXPR N_PRINT_EXPR N_INPUT_EXPR
%type <typeInfo> N_LET_EXPR N_EXPR_LIST
%type <typeInfo> N_PROGN_OR_USERFUNCTCALL N_FUNCT_NAME
%type <typeInfo> N_ACTUAL_PARAMS
%type <num> T_INTCONST
%type <opType> N_BIN_OP N_ARITH_OP N_LOG_OP N_REL_OP T_ADD T_MULT N_UN_OP
%type <opType> T_DIV T_SUB T_AND T_OR T_NOT T_LT T_GT T_GE T_LE T_EQ T_NE
/*
* Starting point.
*/
%start N_START
/*
* Translation rules.
*/
%%
N_START : // epsilon
{
printRule("START", "epsilon");
}
| N_START N_EXPR
{
printRule("START", "START EXPR");
switch ($2.type)
{
case INT :
printf("\n---- Completed parsing ----\n\n");
printf("\nValue of the expression is: %d\n", $2.valINT);
break;
case STR :
printf("\n---- Completed parsing ----\n\n");
printf("\nValue of the expression is: %s\n", $2.valSTR);
break;
case BOOL : printf("\n---- Completed parsing ----\n\n");
printf("\nValue of the expression is: %s\n", $2.valBOOL == false ? "nil" : "t");
break;
default : cout << "UNKNOWN";
}
}
;
N_EXPR : N_CONST
{
printRule("EXPR", "CONST");
$$.type = $1.type;
if($1.type == STR){
$$.valSTR = $1.valSTR;
}
if($1.type == INT){
$$.valINT = $1.valINT;
}
if($1.type == BOOL){
$$.valBOOL = $1.valBOOL;
}
}
| T_IDENT
{
printRule("EXPR", "IDENT");
string ident = string($1);
TYPE_INFO exprTypeInfo =
findEntryInAnyScope(ident);
if (exprTypeInfo.type == UNDEFINED)
yyerror(UNDEFINED_IDENT);
$$.type = exprTypeInfo.type;
switch(exprTypeInfo.type){
case(STR):
$$.valSTR = exprTypeInfo.valSTR;
break;
case(INT):
$$.valINT = exprTypeInfo.valINT;
break;
case(BOOL):
$$.valBOOL = exprTypeInfo.valBOOL;
break;
}
}
| T_LPAREN N_PARENTHESIZED_EXPR T_RPAREN
{
printRule("EXPR", "( PARENTHESIZED_EXPR )");
$$.type = $2.type;
switch($2.type){
case(STR):
$$.valSTR = $2.valSTR;
break;
case(INT):
$$.valINT = $2.valINT;
break;
case(BOOL):
$$.valBOOL = $2.valBOOL;
break;
}
}
;
N_CONST : T_INTCONST
{
printRule("CONST", "INTCONST");
$$.type = INT;
$$.valINT= $1;
}
| T_STRCONST
{
printRule("CONST", "STRCONST");
$$.type = STR;
$$.valSTR = $1;
}
| T_T
{
printRule("CONST", "t");
$$.type = BOOL;
$$.valBOOL = true;
}
| T_NIL
{
printRule("CONST", "nil");
$$.type = BOOL;
$$.valBOOL = false;
}
;
N_PARENTHESIZED_EXPR : N_ARITHLOGIC_EXPR
{
printRule("PARENTHESIZED_EXPR",
"ARITHLOGIC_EXPR");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
| N_IF_EXPR
{
printRule("PARENTHESIZED_EXPR",
"IF_EXPR");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
| N_LET_EXPR
{
printRule("PARENTHESIZED_EXPR",
"LET_EXPR");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
| N_PRINT_EXPR
{
printRule("PARENTHESIZED_EXPR",
"PRINT_EXPR");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
| N_INPUT_EXPR
{
printRule("PARENTHESIZED_EXPR",
"INPUT_EXPR");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
| N_PROGN_OR_USERFUNCTCALL
{
printRule("PARENTHESIZED_EXPR",
"PROGN_OR_USERFUNCTCALL");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
| T_EXIT
{
printRule("PARENTHESIZED_EXPR",
"EXIT");
bail();
}
;
N_PROGN_OR_USERFUNCTCALL : N_FUNCT_NAME N_ACTUAL_PARAMS
{
printRule("PROGN_OR_USERFUNCTCALL ",
"FUNCT_NAME ACTUAL_PARAMS");
// Special case if it was epsilon
if ($2.type == NOT_APPLICABLE){
$$.type = BOOL;
$$.valBOOL = nil;
}
else $$.type = $2.type;
switch($2.type){
case(STR):
$$.valSTR = $2.valSTR;
break;
case(INT):
$$.valINT = $2.valINT;
break;
case(BOOL):
$$.valBOOL = $2.valBOOL;
break;
}
}
;
N_FUNCT_NAME : T_PROGN
{
printRule("FUNCT_NAME",
"PROGN");
$$.type = NOT_APPLICABLE;
}
;
N_ACTUAL_PARAMS : N_EXPR_LIST
{
printRule("ACTUAL_PARAMS",
"EXPR_LIST");
$$.type = $1.type;
$$.valBOOL = $1.valBOOL ;
$$.valINT = $1.valINT;
$$.valSTR = $1.valSTR;
}
| // epsilon
{
printRule("ACTUAL_PARAMS",
"EPSILON");
$$.type = NOT_APPLICABLE;
}
;
N_ARITHLOGIC_EXPR : N_UN_OP N_EXPR
{
printRule("ARITHLOGIC_EXPR",
"UN_OP EXPR");
$$.type = BOOL;
if($1 == NOT){
if($2.valBOOL == TRUE){
$$.valBOOL = nil;
}
else{
$$.valBOOL = TRUE;
}
}
}
| N_BIN_OP N_EXPR N_EXPR
{
printRule("ARITHLOGIC_EXPR",
"BIN_OP EXPR EXPR");
$$.type = BOOL;
if($1 == PLUS || $1 == SUB || $1 == DIV || $1 == MULT){
$$.type = INT;
if (!isIntCompatible($2.type))
yyerror(MUST_BE_INTEGER1);
if (!isIntCompatible($3.type))
yyerror(MUST_BE_INTEGER2);
if ($1 == PLUS){
$$.valINT = $2.valINT + $3.valINT;
}
if($1 == SUB){
$$.valINT = $2.valINT - $3.valINT;
}
if($1 == MULT){
$$.valINT = $2.valINT * $3.valINT;
}
if($1 == DIV){
if($3.valINT == 0){
yyerror("Attempted division by zero");
}
$$.valINT = $2.valINT / $3.valINT;
}}
if($1 == AND || $1 == OR){
if($1 == AND){
if($2.valBOOL == nil && $3.type != nil){
$$.valBOOL = nil;
}
else{
$$.valBOOL = TRUE;
}
}
if($1 == OR){
if($2.valBOOL != nil || $3.type != nil){
$$.valBOOL = TRUE;
}
else{
$$.valBOOL = nil;
}
}
}
if($1 == LE || $1 == GE || $1 == LT || $1 == GT || $1 == EQ || $1 == NE){
if (!isIntOrStrCompatible($2.type))
yyerror(MUST_BE_INT_OR_STR1);
if (!isIntOrStrCompatible($3.type))
yyerror(MUST_BE_INT_OR_STR2);
if (isIntCompatible($2.type) &&
!isIntCompatible($3.type))
yyerror(MUST_BE_INTEGER2);
if (isStrCompatible($2.type) &&
!isStrCompatible($3.type))
yyerror(MUST_BE_STRING2);
if($2.type == STR && $3.type == STR){
if($1 == LT){
$2.valSTR < $3.valSTR ? $$.valBOOL = TRUE : $$.valBOOL = nil;
}
if($1== GT){
$2.valSTR > $3.valSTR ? $$.valBOOL = TRUE :$$.valBOOL = nil;
}
if($1 == LE){
$2.valSTR <=$3.valSTR ? $$.valBOOL = TRUE : $$.valBOOL = nil;
}
if($1 == GE){
$2.valSTR >= $3.valSTR ? $$.valBOOL = TRUE :$$.valBOOL = nil;
}
if($1 == NE){
$2.valSTR != $3.valSTR ? $$.valBOOL = TRUE: $$.valBOOL = nil;
}
if($1 ==EQ){
$2.valSTR == $2.valSTR ? $$.valBOOL = TRUE : $$.valBOOL = nil;
}
}
if($2.type == INT && $3.type == INT){
if($1 == LT){
$2.valINT < $3.valINT ? $$.valBOOL = TRUE : $$.valBOOL = nil;
}
if($1 == GT){
$2.valINT > $3.valINT ? $$.valBOOL = TRUE :$$.valBOOL = nil;
}
if($1 == LE){
$2.valINT <=$3.valINT ? $$.valBOOL = TRUE : $$.valBOOL = nil;
}
if($1 == GE){
$2.valINT >= $3.valINT ? $$.valBOOL = TRUE :$$.valBOOL = nil;
}
if($1 == NE){
$2.valINT != $3.valINT ? $$.valBOOL = TRUE: $$.valBOOL = nil;
}
if($1 ==EQ){
$2.valINT == $2.valINT ? $$.valBOOL = TRUE : $$.valBOOL = nil;
}
}
} // end switch
}
;
N_IF_EXPR : T_IF N_EXPR N_EXPR N_EXPR
{
printRule("IF_EXPR", "if EXPR EXPR EXPR");
$$.type = $3.type;
if($2.valBOOL == nil){
switch($4.type){
case(STR):
$$.valSTR = $4.valSTR;
break;
case(INT):
$$.valINT = $4.valINT;
break;
case(BOOL):
$$.valBOOL = $4.valBOOL;
break;
}
}
else{
switch($3.type){
case(STR):
$$.valSTR = $3.valSTR;
break;
case(INT):
$$.valINT = $3.valINT;
break;
case(BOOL):
$$.valBOOL = $3.valBOOL;
break;
}
}
}
;
N_LET_EXPR : T_LETSTAR T_LPAREN N_ID_EXPR_LIST T_RPAREN
N_EXPR
{
$$.type = $5.type;
$$.valSTR = $5.valSTR;
$$.valINT = $5.valINT;
$$.valBOOL = $5.valBOOL;
}
;
N_ID_EXPR_LIST : /* epsilon */
{
printRule("ID_EXPR_LIST", "epsilon");
}
| N_ID_EXPR_LIST T_LPAREN T_IDENT N_EXPR T_RPAREN
{
printRule("ID_EXPR_LIST",
"ID_EXPR_LIST ( IDENT EXPR )");
string lexeme = string($3);
TYPE_INFO exprTypeInfo = $4;
bool success = scopeStack.top().addEntry
(SYMBOL_TABLE_ENTRY(lexeme,
exprTypeInfo, exprTypeInfo.valINT, exprTypeInfo.valSTR, exprTypeInfo.valBOOL));
if (! success)
yyerror(MULTIPLY_DEFINED_IDENT);
}
;
N_PRINT_EXPR : T_PRINT N_EXPR
{
printRule("PRINT_EXPR", "print EXPR");
$$.type = $2.type;
switch($2.type){
case(STR):
$$.valSTR = $2.valSTR;
printf("%s\n",$2.valSTR);
break;
case(INT):
$$.valINT = $2.valINT;
printf("%d\n",$2.valINT);
break;
case(BOOL):
$$.valBOOL = $2.valBOOL;
printf("%d\n",$2.valBOOL);
break;
}
}
;
N_INPUT_EXPR : T_INPUT
{
printRule("INPUT_EXPR", "input");
char buffer[256];
cin.getline(buffer,256);
if(isdigit(buffer[0]) || (buffer[0] == '+') || (buffer[0] == '-')){
$$.type = INT;
$$.valINT = atoi(buffer);
$$.valSTR = strdup("");
$$.valBOOL = false;
}
else{
$$.type = STR;
$$.valINT = 0;
$$.valSTR = strdup(buffer);
$$.valBOOL = false;
}
}
;
N_EXPR_LIST : N_EXPR N_EXPR_LIST
{
printRule("EXPR_LIST", "EXPR EXPR_LIST");
$$.type = $2.type;
$$.valBOOL = $2.valBOOL;
$$.valSTR = $2.valSTR;
$$.valINT = $2.valINT;
}
| N_EXPR
{
printRule("EXPR_LIST", "EXPR");
$$.type = $1.type;
switch($1.type){
case(STR):
$$.valSTR = $1.valSTR;
break;
case(INT):
$$.valINT = $1.valINT;
break;
case(BOOL):
$$.valBOOL = $1.valBOOL;
break;
}
}
;
N_BIN_OP : N_ARITH_OP
{
printRule("BIN_OP", "ARITH_OP");
$$ = $1;
}
|
N_LOG_OP
{
printRule("BIN_OP", "LOG_OP");
$$ = $1;
}
|
N_REL_OP
{
printRule("BIN_OP", "REL_OP");
$$ = $1;
}
;
N_ARITH_OP : T_ADD
{
printRule("ARITH_OP", "+");
$$ = PLUS;
}
| T_SUB
{
printRule("ARITH_OP", "-");
$$ = SUB;
}
| T_MULT
{
printRule("ARITH_OP", "*");
$$ = MULT;
}
| T_DIV
{
printRule("ARITH_OP", "/");
$$ = DIV;
}
;
N_REL_OP : T_LT
{
printRule("REL_OP", "<");
$$ = LT;
}
| T_GT
{
printRule("REL_OP", ">");
$$ = GT;
}
| T_LE
{
printRule("REL_OP", "<=");
$$ = LE;
}
| T_GE
{
printRule("REL_OP", ">=");
$$ = GE;
}
| T_EQ
{
printRule("REL_OP", "=");
$$ = EQ;
}
| T_NE
{
printRule("REL_OP", "/=");
$$ = NE;
}
;
N_LOG_OP : T_AND
{
printRule("LOG_OP", "and");
$$ = AND;
}
| T_OR
{
printRule("LOG_OP", "or");
$$ = OR;
}
;
N_UN_OP : T_NOT
{
printRule("UN_OP", "not");
$$ = NOT;
}
;
%%
#include "lex.yy.c"
extern FILE *yyin;
bool isIntCompatible(const int theType)
{
return((theType == INT) || (theType == INT_OR_STR) ||
(theType == INT_OR_BOOL) ||
(theType == INT_OR_STR_OR_BOOL));
}
bool isStrCompatible(const int theType)
{
return((theType == STR) || (theType == INT_OR_STR) ||
(theType == STR_OR_BOOL) ||
(theType == INT_OR_STR_OR_BOOL));
}
bool isIntOrStrCompatible(const int theType)
{
return(isStrCompatible(theType) || isIntCompatible(theType));
}
void printRule(const char* lhs, const char* rhs)
{
if (OUTPUT_PRODUCTIONS)
printf("%s -> %s\n", lhs, rhs);
return;
}
void beginScope()
{
scopeStack.push(SYMBOL_TABLE());
}
void endScope()
{
scopeStack.pop();
}
TYPE_INFO findEntryInAnyScope(const string theName)
{
TYPE_INFO info = {UNDEFINED};
if (scopeStack.empty( )) return(info);
info = scopeStack.top().findEntry(theName);
if (info.type != UNDEFINED)
return(info);
else { // check in "next higher" scope
SYMBOL_TABLE symbolTable = scopeStack.top( );
scopeStack.pop( );
info = findEntryInAnyScope(theName);
scopeStack.push(symbolTable); // restore the stack
return(info);
}
}
void cleanUp()
{
if (scopeStack.empty())
return;
else
{
scopeStack.pop();
cleanUp();
}
}
void prepareToTerminate()
{
cleanUp();
cout << endl << "Bye!" << endl;
}
void bail()
{
prepareToTerminate();
exit(1);
}
int main(int argc, char** argv)
{
if(argc < 2){
printf("You must specify a file in the command line!\n");
exit(1);
}
yyin = fopen(argv[1],"r");
do {
yyparse();
} while (!feof(yyin));
prepareToTerminate();
return 0;
}