-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCode.y
182 lines (150 loc) · 4.45 KB
/
Code.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
%{
#include <stdio.h>
#include <string.h>
void yyerror(const char *msg);
char* varname[100];
int numarray[100];
int idx = 0;
%}
%code requires {
typedef struct node node;
struct node{
int num;
int boolean;
char* str;
};
}
%union{
int ival;
node nval;
char* sval;
}
%token<ival> NUMBER BOOL_VAL
%token<sval> ID
%token PRINTNUM PRINTBOOL MOD AND OR NOT IF DEFINE
%type<nval> exp variable num_op plus multiply minus divide modulus greater smaller equal logical_op and_op or_op not_op if_exp plus_exps mul_exps equ_exps and_exps or_exps
%%
program : stmts { }
;
stmts : stmt stmts { }
| stmt { }
;
stmt : exp { }
| def_stmt { }
| print_stmt { }
;
print_stmt : '(' PRINTNUM exp ')' { printf("%d\n", $3.num); }
| '(' PRINTBOOL exp ')' {
if($3.boolean == 0){
printf("#f\n");
}
else{
printf("#t\n");
}
}
;
exp : BOOL_VAL { $$.boolean = $1; }
| NUMBER { $$.num = $1; }
| variable { $$.num = $1.num; $$.boolean = $1.boolean; }
| num_op { $$.num = $1.num; $$.boolean = $1.boolean; }
| logical_op { $$.num = $1.num; $$.boolean = $1.boolean; }
| if_exp { $$.num = $1.num; $$.boolean = $1.boolean; }
;
num_op : plus { $$.num = $1.num; $$.boolean = $1.boolean; }
| multiply { $$.num = $1.num; $$.boolean = $1.boolean; }
| minus { $$.num = $1.num; $$.boolean = $1.boolean; }
| divide { $$.num = $1.num; $$.boolean = $1.boolean; }
| modulus { $$.num = $1.num; $$.boolean = $1.boolean; }
| greater { $$.num = $1.num; $$.boolean = $1.boolean; }
| smaller { $$.num = $1.num; $$.boolean = $1.boolean; }
| equal { $$.num = $1.num; $$.boolean = $1.boolean; }
;
plus : '(' '+' exp plus_exps ')' { $$.num = $3.num + $4.num; }
;
multiply : '(' '*' exp mul_exps ')' { $$.num = $3.num * $4.num; }
;
minus : '(' '-' exp exp ')' { $$.num = $3.num - $4.num; }
;
divide : '(' '/' exp exp ')' { $$.num = $3.num / $4.num; }
;
modulus : '(' MOD exp exp ')' { $$.num = $3.num % $4.num; }
;
greater : '(' '>' exp exp ')' { $$.boolean = ($3.num > $4.num); }
;
smaller : '(' '<' exp exp ')' { $$.boolean = ($3.num < $4.num); }
;
equal : '(' '=' exp equ_exps ')' { $$.boolean = ($4.boolean && ($3.num == $4.num)); }
;
plus_exps : exp plus_exps { $$.num = $1.num + $2.num; }
| exp { $$.num = $1.num; }
;
mul_exps : exp mul_exps { $$.num = $1.num * $2.num; }
| exp { $$.num = $1.num; }
;
equ_exps : exp equ_exps { $$.boolean = ($2.boolean && ($1.num == $2.num)); }
| exp { $$.num = $1.num; $$.boolean = 1; }
;
logical_op : and_op { $$.num = $1.num; $$.boolean = $1.boolean; }
| or_op { $$.num = $1.num; $$.boolean = $1.boolean; }
| not_op { $$.num = $1.num; $$.boolean = $1.boolean; }
;
and_op : '(' AND exp and_exps ')' { $$.boolean = ($3.boolean && $4.boolean); }
;
or_op : '(' OR exp or_exps ')' { $$.boolean = ($3.boolean || $4.boolean); }
;
not_op : '(' NOT exp ')' { $$.boolean = !($3.boolean); }
;
and_exps : exp and_exps { $$.boolean = ($1.boolean && $2.boolean); }
| exp { $$.boolean = $1.boolean; }
;
or_exps : exp or_exps { $$.boolean = ($1.boolean || $2.boolean); }
| exp { $$.boolean = $1.boolean; }
;
if_exp : '(' IF exp exp exp ')' {
if($3.boolean == 0) {
$$.boolean = $5.boolean;
$$.num = $5.num;
}
else {
$$.boolean = $4.boolean;
$$.num = $4.num;
}
}
;
def_stmt : '(' DEFINE variable exp ')' {
varname[idx] = $3.str;
numarray[idx++] = $4.num;
}
;
variable : ID {
int i;
for(i = 0; i < idx; i++) {
if(strcmp(varname[i], $1) == 0) {
$$.str = $1;
$$.num = numarray[i];
break;
}
}
if(i >= idx)
$$.str = $1;
}
;
%%
void yyerror(const char *msg){
fprintf(stderr, "%s\n", msg);
}
int main(int argc, char** argv)
{
yyparse();
return 0;
}
/*
sudo mount -t vboxsf share /home/user/Desktop/DomjudgeHW2
bison -d -o Final.tab.c Final.y
gcc -c -g -I.. Final.tab.c
flex -o Final.yy.c Final.l
gcc -c -g -I.. Final.yy.c
gcc -o Final Final.tab.o Final.yy.o -ll
./Final < 01_1.lsp
./smli < 01_1.lsp
*/