Skip to content

Commit

Permalink
sql parser: add a demo to show usage of sql parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkinben committed Jan 29, 2022
1 parent 6a2364e commit fca4d58
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
52 changes: 52 additions & 0 deletions sql-parser/demo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* @file demo.c
* @author sinkinben@outlook.com
* @brief A demo to show the usage of `yyparse()`
* @date 2022-01-28
*
* @copyright Copyright (c) 2022
*
*
* SELECT:
* select * from tblname;
* select c1, c2, c3, c4 from tblname;
* select * from tblid where a != 'ss3' and b > 10 and c <= 9 and d == 'aa';
* select c1, c2, c3,c4 from tblid where a != 'ss3' and b > 10 and c <= 9 and d == 'aa' or e >= 16;
* CREATE:
* create table tbl1 (c1 int(10), c2 char(16), c3 int(32));
* create table mytable (id int(16), msg1 varchar(32), msg2 varchar(64));
* TODO:
* 1) Free the resource, when the SQL parsing processes are finished,
* e.g. schema_list, select_list, and condition_tree.
* 2) The priority between 'AND' and 'OR' is ignored, since I forget how to fix shift/reduce confilcts :-(.
* Run: bison -d -v sqlparser.y, in the 'sqlparser.output', we can see the shift/reduce conflicts
* For example:
* + A AND B OR C => A AND (B OR C), which should be (A AND B) OR C
* + A AND B OR C AND D => A AND (B OR (C AND D)), which should be (A AND B) OR (C AND D)
* + This is a deadly disadvantage for this dummy sql parser.
*/
#include <stdio.h>
#include <stdint.h>
#include "sqlparser.tab.h"

typedef struct yy_buffer_state * YY_BUFFER_STATE;
extern YY_BUFFER_STATE yy_scan_string(const char *);
extern void yy_delete_buffer(YY_BUFFER_STATE);
extern int yyparse();

int main(int argc, char *argv[])
{
size_t len;
char *ptr = NULL;
while (1)
{
printf("tinydb > ");
getline(&ptr, &len, stdin);
ptr[len - 2] = '\0';

YY_BUFFER_STATE buffer = yy_scan_string(ptr);
/* 0 - Success, 1 - Fail */
yyparse();
yy_delete_buffer(buffer);
}
}
9 changes: 6 additions & 3 deletions sql-parser/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ CC=clang -std=gnu17 -Wall -Wno-pointer-to-int-cast
build:
flex sqlex.l
bison -d sqlparser.y
# use 'gcc -fl' if you build on Linux
$(CC) sqlparser.tab.c lex.yy.c -ll

clean:
rm a.out lex.yy.c *.tab.c *.tab.h
rm a.out lex.yy.c *.tab.c *.tab.h

demo: build
# use 'gcc -fl' if you build on Linux
$(CC) sqlparser.tab.c lex.yy.c demo.c -ll
./a.out
8 changes: 3 additions & 5 deletions sql-parser/sqlparser.y
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ int yywrap() { return 1; }

void yyerror(const char *msg) { fprintf(stderr, "error: %s\n", msg); }

int main(int argc, char *argv[]) { yyparse(); }

%}

Expand All @@ -44,7 +43,6 @@ int main(int argc, char *argv[]) { yyparse(); }
// token's value
%token<strval> STRING IDNAME INT CHAR
%token<intval> NUMBER
%token<intval> AND OR

// BNF's value
%type <schema_node> createitem
Expand Down Expand Up @@ -144,13 +142,13 @@ cmp_op:
conditionitem:
columnitem cmp_op NUMBER
{
$$ = alloc_condition(&($1->schema), $2, $3, true);
$$ = alloc_condition((uint64_t)(&($1->schema)), $2, $3, true);
printf("[conditionitem NUMBER]");
print_tree($$, 0);
}
| columnitem cmp_op STRING
{
$$ = alloc_condition(&($1->schema), $2, $3, true);
$$ = alloc_condition((uint64_t)(&($1->schema)), $2, (uint64_t)($3), true);
printf("[conditionitem STRING]");
print_tree($$, 0);
}
Expand All @@ -174,7 +172,7 @@ conditions:
printf("[logic %d] right: ", $2);
print_tree($3, 0);

$$ = alloc_condition($1, $2, $3, false);
$$ = alloc_condition((uint64_t)($1), $2, (uint64_t)($3), false);
}
;

Expand Down

0 comments on commit fca4d58

Please sign in to comment.