Repository navigation
Expand file tree
/
Copy pathbison_parser.y
More file actions
779 lines (640 loc) · 17.6 KB
/
Copy pathbison_parser.y
File metadata and controls
779 lines (640 loc) · 17.6 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
%{
/**
* bison_parser.y
* defines bison_parser.h
* outputs bison_parser.c
*
* Grammar File Spec: http://dinosaur.compilertools.net/bison/bison_6.html
*
*/
/*********************************
** Section 1: C Declarations
*********************************/
#include "bison_parser.h"
#include "flex_lexer.h"
#include <stdio.h>
using namespace hsql;
int yyerror(YYLTYPE* llocp, SQLParserResult** result, yyscan_t scanner, const char *msg) {
SQLParserResult* list = new SQLParserResult();
list->isValid = false;
list->errorMsg = strdup(msg);
list->errorLine = llocp->first_line;
list->errorColumn = llocp->first_column;
*result = list;
return 0;
}
%}
/*********************************
** Section 2: Bison Parser Declarations
*********************************/
// Specify code that is included in the generated .h and .c files
%code requires {
// %code requires block
#include "../sql/statements.h"
#include "../SQLParserResult.h"
#include "parser_typedef.h"
// Auto update column and line number
#define YY_USER_ACTION \
yylloc->first_line = yylloc->last_line; \
yylloc->first_column = yylloc->last_column; \
for(int i = 0; yytext[i] != '\0'; i++) { \
yylloc->total_column++; \
if(yytext[i] == '\n') { \
yylloc->last_line++; \
yylloc->last_column = 0; \
} \
else { \
yylloc->last_column++; \
} \
}
}
// Define the names of the created files
%output "bison_parser.cpp"
%defines "bison_parser.h"
// Tell bison to create a reentrant parser
%define api.pure full
// Prefix the parser
%define api.prefix {hsql_}
%define api.token.prefix {SQL_}
%define parse.error verbose
%locations
%initial-action {
// Initialize
@$.first_column = 0;
@$.last_column = 0;
@$.first_line = 0;
@$.last_line = 0;
@$.total_column = 0;
@$.placeholder_id = 0;
};
// Define additional parameters for yylex (http://www.gnu.org/software/bison/manual/html_node/Pure-Calling.html)
%lex-param { yyscan_t scanner }
// Define additional parameters for yyparse
%parse-param { hsql::SQLParserResult** result }
%parse-param { yyscan_t scanner }
/*********************************
** Define all data-types (http://www.gnu.org/software/bison/manual/html_node/Union-Decl.html)
*********************************/
%union {
double fval;
int64_t ival;
char* sval;
uint uval;
bool bval;
hsql::SQLStatement* statement;
hsql::SelectStatement* select_stmt;
hsql::ImportStatement* import_stmt;
hsql::CreateStatement* create_stmt;
hsql::InsertStatement* insert_stmt;
hsql::DeleteStatement* delete_stmt;
hsql::UpdateStatement* update_stmt;
hsql::DropStatement* drop_stmt;
hsql::PrepareStatement* prep_stmt;
hsql::ExecuteStatement* exec_stmt;
hsql::TableRef* table;
hsql::Expr* expr;
hsql::OrderDescription* order;
hsql::OrderType order_type;
hsql::LimitDescription* limit;
hsql::ColumnDefinition* column_t;
hsql::GroupByDescription* group_t;
hsql::UpdateClause* update_t;
hsql::SQLParserResult* stmt_list;
std::vector<char*>* str_vec;
std::vector<hsql::TableRef*>* table_vec;
std::vector<hsql::ColumnDefinition*>* column_vec;
std::vector<hsql::UpdateClause*>* update_vec;
std::vector<hsql::Expr*>* expr_vec;
}
/*********************************
** Token Definition
*********************************/
%token <sval> IDENTIFIER STRING
%token <fval> FLOATVAL
%token <ival> INTVAL
%token <uval> NOTEQUALS LESSEQ GREATEREQ
/* SQL Keywords */
%token DEALLOCATE PARAMETERS INTERSECT TEMPORARY TIMESTAMP
%token DISTINCT NVARCHAR RESTRICT TRUNCATE ANALYZE BETWEEN
%token CASCADE COLUMNS CONTROL DEFAULT EXECUTE EXPLAIN
%token HISTORY INTEGER NATURAL PREPARE PRIMARY SCHEMAS
%token SPATIAL VIRTUAL BEFORE COLUMN CREATE DELETE DIRECT
%token DOUBLE ESCAPE EXCEPT EXISTS GLOBAL HAVING IMPORT
%token INSERT ISNULL OFFSET RENAME SCHEMA SELECT SORTED
%token TABLES UNIQUE UNLOAD UPDATE VALUES AFTER ALTER CROSS
%token DELTA GROUP INDEX INNER LIMIT LOCAL MERGE MINUS ORDER
%token OUTER RIGHT TABLE UNION USING WHERE CALL DATE DESC
%token DROP FILE FROM FULL HASH HINT INTO JOIN LEFT LIKE
%token LOAD NULL PART PLAN SHOW TEXT TIME VIEW WITH ADD ALL
%token AND ASC CSV FOR INT KEY NOT OFF SET TBL TOP AS BY IF
%token IN IS OF ON OR TO
/*********************************
** Non-Terminal types (http://www.gnu.org/software/bison/manual/html_node/Type-Decl.html)
*********************************/
%type <stmt_list> statement_list
%type <statement> statement preparable_statement
%type <exec_stmt> execute_statement
%type <prep_stmt> prepare_statement
%type <select_stmt> select_statement select_with_paren select_no_paren select_clause
%type <import_stmt> import_statement
%type <create_stmt> create_statement
%type <insert_stmt> insert_statement
%type <delete_stmt> delete_statement truncate_statement
%type <update_stmt> update_statement
%type <drop_stmt> drop_statement
%type <sval> table_name opt_alias alias file_path
%type <bval> opt_not_exists opt_distinct
%type <uval> import_file_type opt_join_type column_type
%type <table> from_clause table_ref table_ref_atomic table_ref_name
%type <table> join_clause join_table table_ref_name_no_alias
%type <expr> expr scalar_expr unary_expr binary_expr function_expr star_expr expr_alias placeholder_expr
%type <expr> column_name literal int_literal num_literal string_literal
%type <expr> comp_expr opt_where join_condition opt_having
%type <order> opt_order
%type <limit> opt_limit
%type <order_type> opt_order_type
%type <column_t> column_def
%type <update_t> update_clause
%type <group_t> opt_group
%type <str_vec> ident_commalist opt_column_list
%type <expr_vec> expr_list select_list literal_list
%type <table_vec> table_ref_commalist
%type <update_vec> update_clause_commalist
%type <column_vec> column_def_commalist
/******************************
** Token Precedence and Associativity
** Precedence: lowest to highest
******************************/
%left OR
%left AND
%right NOT
%right '=' EQUALS NOTEQUALS LIKE
%nonassoc '<' '>' LESS GREATER LESSEQ GREATEREQ
%nonassoc NOTNULL
%nonassoc ISNULL
%nonassoc IS /* sets precedence for IS NULL, etc */
%left '+' '-'
%left '*' '/' '%'
%left '^'
/* Unary Operators */
%right UMINUS
%left '[' ']'
%left '(' ')'
%left '.'
%%
/*********************************
** Section 3: Grammar Definition
*********************************/
// Defines our general input.
input:
statement_list opt_semicolon {
*result = $1;
}
;
statement_list:
statement { $$ = new SQLParserResult($1); }
| statement_list ';' statement { $1->addStatement($3); $$ = $1; }
;
statement:
prepare_statement {
$1->setPlaceholders(yyloc.placeholder_list);
yyloc.placeholder_list.clear();
$$ = $1;
}
| preparable_statement
;
preparable_statement:
select_statement { $$ = $1; }
| import_statement { $$ = $1; }
| create_statement { $$ = $1; }
| insert_statement { $$ = $1; }
| delete_statement { $$ = $1; }
| truncate_statement { $$ = $1; }
| update_statement { $$ = $1; }
| drop_statement { $$ = $1; }
| execute_statement { $$ = $1; }
;
/******************************
* Prepared Statement
******************************/
prepare_statement:
PREPARE IDENTIFIER ':' preparable_statement {
$$ = new PrepareStatement();
$$->name = $2;
$$->query = new SQLParserResult($4);
}
| PREPARE IDENTIFIER '{' statement_list opt_semicolon '}' {
$$ = new PrepareStatement();
$$->name = $2;
$$->query = $4;
}
;
execute_statement:
EXECUTE IDENTIFIER {
$$ = new ExecuteStatement();
$$->name = $2;
}
| EXECUTE IDENTIFIER '(' literal_list ')' {
$$ = new ExecuteStatement();
$$->name = $2;
$$->parameters = $4;
}
;
/******************************
* Import Statement
******************************/
import_statement:
IMPORT FROM import_file_type FILE file_path INTO table_name {
$$ = new ImportStatement((ImportStatement::ImportType) $3);
$$->filePath = $5;
$$->tableName = $7;
}
;
import_file_type:
CSV { $$ = ImportStatement::kImportCSV; }
;
file_path:
string_literal { $$ = $1->name; }
;
/******************************
* Create Statement
* CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)
* CREATE TABLE students FROM TBL FILE 'test/students.tbl'
******************************/
create_statement:
CREATE TABLE opt_not_exists table_name FROM TBL FILE file_path {
$$ = new CreateStatement(CreateStatement::kTableFromTbl);
$$->ifNotExists = $3;
$$->tableName = $4;
$$->filePath = $8;
}
| CREATE TABLE opt_not_exists table_name '(' column_def_commalist ')' {
$$ = new CreateStatement(CreateStatement::kTable);
$$->ifNotExists = $3;
$$->tableName = $4;
$$->columns = $6;
}
;
opt_not_exists:
IF NOT EXISTS { $$ = true; }
| /* empty */ { $$ = false; }
;
column_def_commalist:
column_def { $$ = new std::vector<ColumnDefinition*>(); $$->push_back($1); }
| column_def_commalist ',' column_def { $1->push_back($3); $$ = $1; }
;
column_def:
IDENTIFIER column_type {
$$ = new ColumnDefinition($1, (ColumnDefinition::DataType) $2);
}
;
column_type:
INT { $$ = ColumnDefinition::INT; }
| INTEGER { $$ = ColumnDefinition::INT; }
| DOUBLE { $$ = ColumnDefinition::DOUBLE; }
| TEXT { $$ = ColumnDefinition::TEXT; }
;
/******************************
* Drop Statement
* DROP TABLE students;
* DEALLOCATE PREPARE stmt;
******************************/
drop_statement:
DROP TABLE table_name {
$$ = new DropStatement(DropStatement::kTable);
$$->name = $3;
}
| DEALLOCATE PREPARE IDENTIFIER {
$$ = new DropStatement(DropStatement::kPreparedStatement);
$$->name = $3;
}
;
/******************************
* Delete Statement / Truncate statement
* DELETE FROM students WHERE grade > 3.0
* DELETE FROM students <=> TRUNCATE students
******************************/
delete_statement:
DELETE FROM table_name opt_where {
$$ = new DeleteStatement();
$$->tableName = $3;
$$->expr = $4;
}
;
truncate_statement:
TRUNCATE table_name {
$$ = new DeleteStatement();
$$->tableName = $2;
}
;
/******************************
* Insert Statement
* INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)
* INSERT INTO employees SELECT * FROM stundents
******************************/
insert_statement:
INSERT INTO table_name opt_column_list VALUES '(' literal_list ')' {
$$ = new InsertStatement(InsertStatement::kInsertValues);
$$->tableName = $3;
$$->columns = $4;
$$->values = $7;
}
| INSERT INTO table_name opt_column_list select_no_paren {
$$ = new InsertStatement(InsertStatement::kInsertSelect);
$$->tableName = $3;
$$->columns = $4;
$$->select = $5;
}
;
opt_column_list:
'(' ident_commalist ')' { $$ = $2; }
| /* empty */ { $$ = NULL; }
;
/******************************
* Update Statement
* UPDATE students SET grade = 1.3, name='Felix Fürstenberg' WHERE name = 'Max Mustermann';
******************************/
update_statement:
UPDATE table_ref_name_no_alias SET update_clause_commalist opt_where {
$$ = new UpdateStatement();
$$->table = $2;
$$->updates = $4;
$$->where = $5;
}
;
update_clause_commalist:
update_clause { $$ = new std::vector<UpdateClause*>(); $$->push_back($1); }
| update_clause_commalist ',' update_clause { $1->push_back($3); $$ = $1; }
;
update_clause:
IDENTIFIER '=' literal {
$$ = new UpdateClause();
$$->column = $1;
$$->value = $3;
}
;
/******************************
* Select Statement
******************************/
select_statement:
select_with_paren
| select_no_paren
;
select_with_paren:
'(' select_no_paren ')' { $$ = $2; }
| '(' select_with_paren ')' { $$ = $2; }
;
select_no_paren:
select_clause opt_order opt_limit {
$$ = $1;
$$->order = $2;
$$->limit = $3;
}
| select_clause set_operator select_clause opt_order opt_limit {
// TODO: allow multiple unions (through linked list)
// TODO: capture type of set_operator
// TODO: might overwrite order and limit of first select here
$$ = $1;
$$->unionSelect = $3;
$$->order = $4;
$$->limit = $5;
}
;
set_operator:
UNION
| INTERSECT
| EXCEPT
;
select_clause:
SELECT opt_distinct select_list from_clause opt_where opt_group {
$$ = new SelectStatement();
$$->selectDistinct = $2;
$$->selectList = $3;
$$->fromTable = $4;
$$->whereClause = $5;
$$->groupBy = $6;
}
;
opt_distinct:
DISTINCT { $$ = true; }
| /* empty */ { $$ = false; }
;
select_list:
expr_list
;
from_clause:
FROM table_ref { $$ = $2; }
;
opt_where:
WHERE expr { $$ = $2; }
| /* empty */ { $$ = NULL; }
;
opt_group:
GROUP BY expr_list opt_having {
$$ = new GroupByDescription();
$$->columns = $3;
$$->having = $4;
}
| /* empty */ { $$ = NULL; }
;
opt_having:
HAVING expr { $$ = $2; }
| /* empty */ { $$ = NULL; }
opt_order:
ORDER BY expr opt_order_type { $$ = new OrderDescription($4, $3); }
| /* empty */ { $$ = NULL; }
;
opt_order_type:
ASC { $$ = kOrderAsc; }
| DESC { $$ = kOrderDesc; }
| /* empty */ { $$ = kOrderAsc; }
;
opt_limit:
LIMIT int_literal { $$ = new LimitDescription($2->ival, kNoOffset); delete $2; }
| LIMIT int_literal OFFSET int_literal { $$ = new LimitDescription($2->ival, $4->ival); delete $2; delete $4; }
| /* empty */ { $$ = NULL; }
;
/******************************
* Expressions
******************************/
expr_list:
expr_alias { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| expr_list ',' expr_alias { $1->push_back($3); $$ = $1; }
;
literal_list:
literal { $$ = new std::vector<Expr*>(); $$->push_back($1); }
| literal_list ',' literal { $1->push_back($3); $$ = $1; }
;
expr_alias:
expr opt_alias {
$$ = $1;
$$->alias = $2;
}
;
expr:
'(' expr ')' { $$ = $2; }
| scalar_expr
| unary_expr
| binary_expr
| function_expr
;
scalar_expr:
column_name
| star_expr
| literal
;
unary_expr:
'-' expr { $$ = Expr::makeOpUnary(Expr::UMINUS, $2); }
| NOT expr { $$ = Expr::makeOpUnary(Expr::NOT, $2); }
;
binary_expr:
comp_expr
| expr '-' expr { $$ = Expr::makeOpBinary($1, '-', $3); }
| expr '+' expr { $$ = Expr::makeOpBinary($1, '+', $3); }
| expr '/' expr { $$ = Expr::makeOpBinary($1, '/', $3); }
| expr '*' expr { $$ = Expr::makeOpBinary($1, '*', $3); }
| expr '%' expr { $$ = Expr::makeOpBinary($1, '%', $3); }
| expr '^' expr { $$ = Expr::makeOpBinary($1, '^', $3); }
| expr AND expr { $$ = Expr::makeOpBinary($1, Expr::AND, $3); }
| expr OR expr { $$ = Expr::makeOpBinary($1, Expr::OR, $3); }
| expr LIKE expr { $$ = Expr::makeOpBinary($1, Expr::LIKE, $3); }
| expr NOT LIKE expr { $$ = Expr::makeOpBinary($1, Expr::NOT_LIKE, $4); }
;
comp_expr:
expr '=' expr { $$ = Expr::makeOpBinary($1, '=', $3); }
| expr NOTEQUALS expr { $$ = Expr::makeOpBinary($1, Expr::NOT_EQUALS, $3); }
| expr '<' expr { $$ = Expr::makeOpBinary($1, '<', $3); }
| expr '>' expr { $$ = Expr::makeOpBinary($1, '>', $3); }
| expr LESSEQ expr { $$ = Expr::makeOpBinary($1, Expr::LESS_EQ, $3); }
| expr GREATEREQ expr { $$ = Expr::makeOpBinary($1, Expr::GREATER_EQ, $3); }
;
function_expr:
IDENTIFIER '(' opt_distinct expr ')' { $$ = Expr::makeFunctionRef($1, $4, $3); }
;
column_name:
IDENTIFIER { $$ = Expr::makeColumnRef($1); }
| IDENTIFIER '.' IDENTIFIER { $$ = Expr::makeColumnRef($1, $3); }
;
literal:
string_literal
| num_literal
| placeholder_expr
;
string_literal:
STRING { $$ = Expr::makeLiteral($1); }
;
num_literal:
FLOATVAL { $$ = Expr::makeLiteral($1); }
| int_literal
;
int_literal:
INTVAL { $$ = Expr::makeLiteral($1); }
;
star_expr:
'*' { $$ = new Expr(kExprStar); }
;
placeholder_expr:
'?' {
$$ = Expr::makePlaceholder(yylloc.total_column);
yyloc.placeholder_list.push_back($$);
}
;
/******************************
* Table
******************************/
table_ref:
table_ref_atomic
| table_ref_atomic ',' table_ref_commalist {
$3->push_back($1);
auto tbl = new TableRef(kTableCrossProduct);
tbl->list = $3;
$$ = tbl;
}
;
table_ref_atomic:
table_ref_name
| '(' select_statement ')' alias {
auto tbl = new TableRef(kTableSelect);
tbl->select = $2;
tbl->alias = $4;
$$ = tbl;
}
| join_clause
;
table_ref_commalist:
table_ref_atomic { $$ = new std::vector<TableRef*>(); $$->push_back($1); }
| table_ref_commalist ',' table_ref_atomic { $1->push_back($3); $$ = $1; }
;
table_ref_name:
table_name opt_alias {
auto tbl = new TableRef(kTableName);
tbl->name = $1;
tbl->alias = $2;
$$ = tbl;
}
;
table_ref_name_no_alias:
table_name {
$$ = new TableRef(kTableName);
$$->name = $1;
}
;
table_name:
IDENTIFIER
| IDENTIFIER '.' IDENTIFIER
;
alias:
AS IDENTIFIER { $$ = $2; }
| IDENTIFIER
;
opt_alias:
alias
| /* empty */ { $$ = NULL; }
/******************************
* Join Statements
******************************/
join_clause:
join_table opt_join_type JOIN join_table ON join_condition
{
$$ = new TableRef(kTableJoin);
$$->join = new JoinDefinition();
$$->join->type = (JoinType) $2;
$$->join->left = $1;
$$->join->right = $4;
$$->join->condition = $6;
}
;
opt_join_type:
INNER { $$ = kJoinInner; }
| OUTER { $$ = kJoinOuter; }
| LEFT { $$ = kJoinLeft; }
| RIGHT { $$ = kJoinRight; }
| /* empty, default */ { $$ = kJoinInner; }
;
join_table:
'(' select_statement ')' alias {
auto tbl = new TableRef(kTableSelect);
tbl->select = $2;
tbl->alias = $4;
$$ = tbl;
}
| table_ref_name;
join_condition:
expr
;
/******************************
* Misc
******************************/
opt_semicolon:
';'
| /* empty */
;
ident_commalist:
IDENTIFIER { $$ = new std::vector<char*>(); $$->push_back($1); }
| ident_commalist ',' IDENTIFIER { $1->push_back($3); $$ = $1; }
;
%%
/*********************************
** Section 4: Additional C code
*********************************/
/* empty */