-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsql.y
More file actions
275 lines (260 loc) · 6.96 KB
/
sql.y
File metadata and controls
275 lines (260 loc) · 6.96 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
%{
#include <ctype.h>
#include <stdio.h>
#include "myarray.h"
#include "lex.yy.c"
#include "dbShell.h"
#include <iostream>
#define YYDEBUG 1
#define YYERROR_VERBOSE
#include <vector>
#include "dbCore.h"
using namespace std;
dbShell shell;
extern
void yyerror(char *s) {
// simple error-message
printf("Error '%s'\n", s);
}
%}
%union {
int intval;
char * chval;
int typeval;
table_field_node * tableField;
table_node * tableNode;
insert_node * insertNode;
values_node * valuesNode;
select_node * selectNode;
condexp_node * condNode;
update_node * updateNode;
set_value * setNode;
}
%type<insertNode> insertsql
%type<chval> table STRING field
%type<valuesNode> values value
%type<tableField> fields fields_star table_fields table_field
%type<intval> NUMBER
%type<selectNode> selectsql deletesql
%type<tableNode> tables
%type<condNode> conditions condition comp_op comp_left comp_right
%type<updateNode> updatesql
%type<setNode> updates update
%token CREATE
%token DATABASE
%token SHOW
%token DATABASES
%token DROP
%token USE
%token TABLE
%token TABLES
%token INSERT
%token INTO
%token VALUES
%token SELECT
%token FROM
%token WHERE
%token UPDATE
%token SET
%token ID
%token CHAR
%token NUMBER
%token INT
%token STRING
%token DELETE
%left OR
%left AND
%%
statements: statements statement {printf("\nmysql>");}
| statement {printf("\nmysql>");}
;
statement: createsql
| selectsql
| insertsql
| deletesql
| updatesql
| dbsql
;
// 数据库操作语句
dbsql: CREATE DATABASE database ';'{shell.CreateDatabase($<chval>3);}
| SHOW DATABASES ';' {shell.ShowDatabases();}
| SHOW TABLES ';' {shell.ShowTables();}
| DROP DATABASE database ';' {shell.DropDatabases($<chval>3);}
| DROP TABLE table ';' {shell.dropTable($<chval>3);}
| USE database ';' {shell.UseDatabase($<chval>3);}
;
database: ID
;
// 建表语句
createsql: CREATE TABLE table '(' fieldsdefinition ')' ';' {shell.CreateTable($3, $<tableField>5);}
;
table: ID {}
;
fieldsdefinition: field_type {$<tableField>$ = $<tableField>1;
}
| field_type ',' fieldsdefinition {$<tableField>1->next = $<tableField>3;
$<tableField>$ = $<tableField>1;}
;
field_type: field type {$<tableField>$ = $<tableField>2;
$<tableField>$->field_name = $1;}
;
field: ID {$$ = $<chval>1}
;
type: CHAR '(' NUMBER ')' {$<tableField>$ = new table_field_node;
$<tableField>$->len = $3;}
| INT {$<tableField>$ = new table_field_node;
$<tableField>$->len = 0;}
;
// 查询语句
selectsql: SELECT fields_star FROM tables ';' {$$ = new select_node;
$$->field = $2;
$$->table = $4;
shell.select($$);
}
| SELECT fields_star FROM tables WHERE conditions ';' {$$ = new select_node;
$$->field = $2;
$$->table = $4;
$$->cons = $6;
shell.select($$);
}
;
fields_star: table_fields { $$ = $1;}
| '*' {$$ = new table_field_node;
$$->field_name = _S("*");
}
;
table_fields: table_field { $$ = $1; }
| table_field ',' table_fields {$$ = $1;
$$->next = $3;}
;
table_field: field {$$ = new table_field_node;
$$->field_name = $1;
}
| table '.' field
;
tables: table {$$ = new table_node;
$$->tableName = $1;}
| table ',' tables {$$ = new table_node;
$$->tableName = $1;
$$->next = $3;
}
;
conditions: condition {$$ = $1;}
| '(' conditions ')' {$$ = $2;}
| conditions AND conditions {$$ = new condexp_node;
$$->left = $1;
$$->right = $3;
$$->op = condexp_node::AND;
$$->type = condexp_node::LOGIC;
}
| conditions OR conditions {$$ = new condexp_node;
$$->left = $1;
$$->right = $3;
$$->op = condexp_node::OR;
$$->type = condexp_node::LOGIC;
}
;
condition: comp_left comp_op comp_right {$$ = $2;
$$->left = $1;
$$->right = $3;
}
;
comp_left: table_field {$$ = new condexp_node;
$$->chval = $1->field_name;
}
;
comp_op: '<' {$$ = new condexp_node; $$->op = condexp_node::B;}
| '>' {$$ = new condexp_node; $$->op = condexp_node::G;}
| '=' {$$ = new condexp_node; $$->op = condexp_node::EQ;}
| '!''=' {$$ = new condexp_node; $$->op = condexp_node::NOT;}
;
comp_right: table_field {$$ = new condexp_node;
$$->chval = $1->field_name;
$$->type = condexp_node::STRING;
}
| NUMBER {$$ = new condexp_node;
$$->intval = $1;
$$->type = condexp_node::INT;
}
| STRING {$$ = new condexp_node;
$$->chval = $1;
$$->type = condexp_node::STRING;
}
;
/// 插入语句
insertsql: INSERT INTO table VALUES '(' values ')' ';' {$$ = new insert_node;
$$->table = new table_node;
$$->table->tableName = $3;
$$->values = $6;
shell.sqlInsert($$);
}
| INSERT INTO table '(' fields ')' VALUES '(' values ')' ';' {$$ = new insert_node;
$$->table = new table_node;
$$->table->tableName = $3;
$$->values = $9;
$$->field = $5;
shell.sqlInsert($$);
}
;
values: value {$$ = $1}
| value ',' values {$$ = $1;
$$->next = $3;
}
;
value: STRING { $$ = new values_node;
$$->type = values_node::STRING;
$$->chval = $1;
}
| NUMBER { $$ = new values_node;
$$->type = values_node::INT;
$$->intval = $1;
}
;
fields: field {$$ = new table_field_node;
$$->field_name = $1;
}
| field ',' fields {$$ = new table_field_node;
$$->field_name = $1;
$$->next = $3;
}
;
//删除语句
deletesql: DELETE FROM table ';' {$$ = new select_node;
$$->table = new table_node;
$$->table->tableName = $3;
$$->cons = nullptr;
shell.sqldelete($$);}
| DELETE FROM table WHERE conditions ';' {$$ = new select_node;
$$->table = new table_node;
$$->table->tableName = $3;
$$->cons = $5;
shell.sqldelete($$);
}
;
//更新语句
updatesql: UPDATE table SET updates WHERE conditions ';' {$$ = new update_node;
$$->table = new table_node;
$$->table->tableName = $2;
$$->cons = $6;
$$->setval = $4;
shell.sqlupdate($$);
} ;
updates: update {$$ = $1
}
| update ',' updates {$$ = $1;
$1->next = $3;
}
;
update: comp_left '=' comp_right {$$ = new set_value;
$$->field_name = $1->chval;
$$->intval = $3->intval;
$$->chval = $3->chval;
}
;
%%
int main(){
printf("\nmysql>");
while(1){
yyparse();
}
}