forked from zerofang/myDBMS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql.y
303 lines (264 loc) · 6.44 KB
/
sql.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
%{
#include "sql.h"
char database[64]={0};
int yylex();
int yyparse();
void yyerror (const char *str)
{
fprintf(stderr, "error: %s\n", str); // error function
}
int yywrap()
{
return 1;
}
// main function
int main()
{
printf("SQL>");
return yyparse(); // calling parse funtion
}
%}
%union{
int intval;
char *strval;
struct hyper_items_def *Citemsval;
struct value_def *valueval;
struct item_def *itemval;
struct conditions_def *conval;
struct table_def *tbval;
struct upcon_def *updateval;
}
%token SELECT FROM WHERE AND OR DROP DELETE TABLE CREATE INTO VALUES INSERT UPDATE SET SHOW DATABASE DATABASES TABLES EXIT USE
%token <intval> NUMBER
%token <strval> STRING ID INT CHAR
%type <intval> comparator
%type <Citemsval> hyper_items create_items
%type <valueval> value_list value
%type <itemval> item item_list
%type <conval> condition conditions
%type <tbval> tables
%type <updateval> up_cond up_conds
%left OR
%left AND
%%
/* production for sql grammar */
statements: statements statement | statement
statement: createsql | showsql | selectsql | insertsql | deletesql | updatesql | dropsql | exitsql | usesql
usesql: USE ID ';' '\n' {
printf("\n");
useDB($2);
printf("\nSQL>");
}
showsql: SHOW DATABASES ';' '\n' {
printf("\n");
showDB();
printf("\nSQL>");
}
|SHOW TABLES ';' '\n' {
printf("\n");
showTable();
printf("\nSQL>");
}
createsql: CREATE TABLE ID '(' hyper_items ')' ';' '\n' {
printf("\n");
createTable($3, $5);
printf("\nSQL>");
}
|CREATE DATABASE ID ';' '\n' {
strcpy(database, $3);
printf("\n");
createDB();
printf("\nSQL>");
}
selectsql: SELECT '*' FROM tables ';' '\n'{
printf("\n");
selectWhere(NULL, $4, NULL);
printf("\n");
printf("SQL>");
}
| SELECT item_list FROM tables ';' '\n' {
printf("\n");
selectWhere($2, $4, NULL);
printf("\nSQL>");
}
|SELECT '*' FROM tables WHERE conditions ';' '\n' {
printf("\n");
selectWhere(NULL, $4, $6);
printf("\nSQL>");
}
|SELECT item_list FROM tables WHERE conditions ';' '\n' {
printf("\n");
selectWhere($2, $4, $6);
printf("\nSQL>");
}
deletesql: DELETE FROM ID ';' '\n' {
printf("\n");
deletes($3, NULL);
printf("\n");
printf("SQL>");
}
|DELETE FROM ID WHERE conditions ';' '\n' {
printf("\n");
deletes($3, $5);
printf("\nSQL>");
}
insertsql: INSERT INTO ID VALUES '(' value_list ')' ';' '\n' {
printf("\n");
multiInsert($3, NULL, $6);
printf("\nSQL>");
}
|INSERT INTO ID '(' item_list ')' VALUES '(' value_list ')' ';' '\n' {
printf("\n");
multiInsert($3, $5, $9);
printf("\nSQL>");
}
updatesql: UPDATE ID SET up_conds ';' '\n' {
printf("\n");
updates($2, $4, NULL);
printf("\nSQL>");
}
|UPDATE ID SET up_conds WHERE conditions ';' '\n' {
printf("\n");
updates($2, $4, $6);
printf("\nSQL>");
}
dropsql: DROP TABLE ID ';' '\n' {
printf("\n");
dropTable($3);
printf("\nSQL>");
}
| DROP DATABASE ID ';' '\n' {
printf("\n");
dropDB($3);
printf("\nSQL>");
}
exitsql: EXIT ';' {
printf("\n");
printf("exit with code 0!\n");
exit(0);
}
create_items: ID INT {
$$ = (struct hyper_items_def *)malloc(sizeof(struct hyper_items_def));
$$->field = $1;
$$->type = 0;
$$->next = NULL;
}
| ID CHAR '(' NUMBER ')'{
$$ = (struct hyper_items_def *)malloc(sizeof(struct hyper_items_def));
$$->field = $1;
$$->type = 1;
$$->next = NULL;
}
hyper_items: create_items {
$$ = $1;
}
| hyper_items ',' create_items {
$$ = $3;
$$->next = $1;
}
item: ID {
$$ = (struct item_def *)malloc(sizeof(struct item_def));
$$->field = $1;
$$->pos = NULL;
$$->next = NULL;
}
item_list: item {
$$ = $1;
}
| item_list ',' item{
$$ = $3;
$$->next = $1;
}
value: NUMBER {
$$ = ((struct value_def *)malloc(sizeof(struct value_def)));
$$->value.intkey = $1;
$$->type = 0;
$$->next = NULL;
}
| STRING {
$$ = ((struct value_def *)malloc(sizeof(struct value_def)));
strcpy($$->value.skey, $1);
$$->type = 1;
$$->next = NULL;
}
value_list: value {
$$ = $1;
}
| value_list ',' value {
$$ = $3;
$$->next = $1;
}
comparator: '=' {$$ = 1; }
| '>' {$$ = 2; }
| '<' {$$ = 3; }
| ">=" {$$ = 4; }
| "<=" {$$ = 5; }
| '!' '=' {$$ = 6; }
condition: item comparator NUMBER {
$$ = ((struct conditions_def *)malloc(sizeof(struct conditions_def)));
$$->type = 0;
$$->litem = $1;
$$->intv = $3;
$$->cmp_op = $2;
$$->left = NULL;
$$->right = NULL;
}
| item comparator STRING {
$$ = ((struct conditions_def *)malloc(sizeof(struct conditions_def)));
$$->type = 1;
$$->litem = $1;
$$->strv = $3;
$$->cmp_op = $2;
$$->left = NULL;
$$->right = NULL;
}
conditions: condition {
$$ = $1;
}
|'(' conditions ')' {
$$ = $2;
}
| conditions AND conditions {
$$ = ((struct conditions_def *)malloc(sizeof(struct conditions_def)));
$$->cmp_op = 7;
$$->left = $1;
$$->right = $3;
}
| conditions OR conditions {
$$ = ((struct conditions_def *)malloc(sizeof(struct conditions_def)));
$$->cmp_op = 8;
$$->left = $1;
$$->right = $3;
}
tables: ID {
$$ = ((struct table_def *)malloc(sizeof(struct table_def)));
$$->table = $1;
$$->next = NULL;
}
| tables ',' ID{
$$ = ((struct table_def *)malloc(sizeof(struct table_def)));
$$->table = $3;
$$->next = $1;
}
up_cond: ID '=' NUMBER {
$$ = ((struct upcon_def *)malloc(sizeof(struct upcon_def)));
$$->field = $1;
$$->type = 0;
$$->value.intkey = $3;
$$->next = NULL;
}
| ID '=' STRING {
$$ = ((struct upcon_def *)malloc(sizeof(struct upcon_def)));
$$->field = $1;
$$->type = 1;
strcpy($$->value.skey, $3);
$$->next = NULL;
}
up_conds: up_cond {
$$ = $1;
}
| up_conds ',' up_cond {
$$ = $3;
$$->next = $1;
}
%%