From 6a2364e0fc45e8d174292f3cfd401ea8de645ec7 Mon Sep 17 00:00:00 2001 From: sinkinben Date: Sat, 29 Jan 2022 16:21:12 +0800 Subject: [PATCH] sql parser: add EOL token --- sql-parser/sqlex.l | 2 +- sql-parser/sqlparser.y | 43 ++++++++++++++++++++++-------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/sql-parser/sqlex.l b/sql-parser/sqlex.l index a3fe138..47a9e6c 100644 --- a/sql-parser/sqlex.l +++ b/sql-parser/sqlex.l @@ -53,7 +53,7 @@ [A-Za-z][A-Za-z0-9_]* { yylval.strval = strdup(yytext); return IDNAME; } [0-9]+ { yylval.intval = atoi(yytext); return NUMBER; } -\n { return *yytext; } +\n { return EOL; } [ \t]+ { /* ignore and do nothing */ } diff --git a/sql-parser/sqlparser.y b/sql-parser/sqlparser.y index e5cc379..86a4b54 100644 --- a/sql-parser/sqlparser.y +++ b/sql-parser/sqlparser.y @@ -5,13 +5,21 @@ #include #include "schema.h" #include "condition.h" -int yyparse(); -int yylex(); + +extern int yyparse(); +extern int yylex(); void yyerror(const char *); + schema_node_t *schema_list; // create table tbl (`schema_list`) schema_node_t *select_list; // select `select_list` from tbl condition_t *condition_tree; // select `select_list` from tbl where `condition_tree` +int yywrap() { return 1; } + +void yyerror(const char *msg) { fprintf(stderr, "error: %s\n", msg); } + +int main(int argc, char *argv[]) { yyparse(); } + %} %union @@ -31,6 +39,7 @@ condition_t *condition_tree; // select `select_list` from tbl where `condition_ %token GREAT GREAT_EQUAL LESS LESS_EQUAL %token EQUAL NOT_EQUAL %token AND OR +%token EOL // token's value %token STRING IDNAME INT CHAR @@ -49,20 +58,24 @@ condition_t *condition_tree; // select `select_list` from tbl where `condition_ %% -statements: statements statement {} -| statement {} +statements: + statements statement {} +| statement {} ; -statement: selectsql | createsql | '\n' +statement: + EOL {} +| selectsql EOL {} +| createsql EOL {} ; selectsql: - SELECT '*' FROM IDNAME ';' '\n' + SELECT '*' FROM IDNAME ';' { printf("TODO: please select * from [%s] \n", $4); printf("IDNAME = %p\n", $4); } -| SELECT selectitemlist FROM IDNAME ';' '\n' +| SELECT selectitemlist FROM IDNAME ';' { printf("TODO: please select "); list_node_t *pos; @@ -74,14 +87,14 @@ selectsql: printf("from %s \n", $4); init_list_head(&(select_list->entry)); } -| SELECT '*' FROM IDNAME WHERE conditions ';' '\n' +| SELECT '*' FROM IDNAME WHERE conditions ';' { printf("[SELECT] cond ptr = %p, table name = %s \n", $6, $4); print_tree($6, 0); init_list_head(&(select_list->entry)); condition_tree = NULL; } -| SELECT selectitemlist FROM IDNAME WHERE conditions ';' '\n' +| SELECT selectitemlist FROM IDNAME WHERE conditions ';' { printf("---> TODO: please select "); list_node_t *pos; @@ -166,7 +179,7 @@ conditions: ; createsql: - CREATE TABLE IDNAME '(' createitemlist ')' ';' '\n' + CREATE TABLE IDNAME '(' createitemlist ')' ';' { /* TODO: When after creating table, we should free the `createitemlist` */ printf("table name = %s\n", $3); @@ -209,11 +222,6 @@ createitemlist: %% -void yyerror(const char *msg) -{ - fprintf(stderr, "error: %s\n", msg); -} - void __attribute__((constructor)) init() { /* used by create sql, e.g. create table tbl (`schema_list`), @@ -232,8 +240,3 @@ void __attribute__((constructor)) init() */ condition_tree = NULL; } - -int main(int argc, char *argv[]) -{ - yyparse(); -} \ No newline at end of file