jgarzik / sqlfun Goto Github PK
View Code? Open in Web Editor NEWModern SQL parser using Bison (Yacc) and Flex
License: Other
Modern SQL parser using Bison (Yacc) and Flex
License: Other
For example, the following should recognize "a b" as a column name:
echo "select [a b] from d;" | ./sql
To fix this, in sql.l, add a rule as follows:
[A-Za-z][A-Za-z0-9_]* { yylval->strval = strdup(yytext);
return NAME; }
/* add the next line as a new rule */
\[(\\.|[^\]\n])*\] |
`[^`/\\.\n]+` { yylval->strval = strdup(yytext+1);
yylval->strval[yyleng-2] = 0;
return NAME; }
Thanks for putting this together. I have that Flex+Bison book too!
The following will create a seg fault:
echo "select [ab] from d;" | ./sql
The reason is that in sql.l
the call to yyerror is missing a parameter. The fix is to replace all occurrences of yyerror(yylloc, 0, pstate
with yyerror(yylloc, 0, pstate
EIther of the following two inputs fail:
select null from t;
select (1) from t;
These are easily fixed by adding the following right-hand productions to expr
:
NULLX
'(' expr ')'
along with the corresponding output actions.
Rather than a pull request, here's a full git patch for fixing these issues, though you may want to modify sqlp_null()
and sqlp_expr_paren()
. Could you please merge into master?
diff --git a/exec.c b/exec.c
index d1d11d0..e918842 100644
--- a/exec.c
+++ b/exec.c
@@ -174,6 +174,11 @@ void sqlp_bool(struct psql_state *pstate, int val)
boolout("BOOL", val);
}
+void sqlp_null(struct psql_state *pstate)
+{
+ strout("CONST", "NULL"); /* probably this should be a different function */
+}
+
void sqlp_call(struct psql_state *pstate, int n_args, const char *name)
{
json_t *obj = json_object();
@@ -397,6 +402,11 @@ void sqlp_expr_is_in(struct psql_state *pstate, int val)
boolout("EXPR-IS-IN", val);
}
+void sqlp_expr_paren(struct psql_state *pstate)
+{
+ opout("PARENS"); /* probably this should be a different function */
+}
+
void sqlp_expr_op(struct psql_state *pstate, enum sqlp_expr_ops op)
{
strout("EXPR-OP", op_names[op]);
diff --git a/sql-parser.h b/sql-parser.h
index c78c34e..32829bb 100644
--- a/sql-parser.h
+++ b/sql-parser.h
@@ -67,6 +67,7 @@ extern void sqlp_alias(struct psql_state *pstate, const char *alias);
extern void sqlp_assign(struct psql_state *pstate, const char *db_name, const char *name);
extern void sqlp_assign_at(struct psql_state *pstate, const char *name);
extern void sqlp_bool(struct psql_state *pstate, int val);
+extern void sqlp_null(struct psql_state *pstate);
extern void sqlp_call(struct psql_state *pstate, int n_args, const char *name);
extern void sqlp_call_date(struct psql_state *pstate, int n_args, enum sqlp_expr_ops op);
extern void sqlp_call_trim_opts(struct psql_state *pstate, int trim_opts);
@@ -104,6 +105,7 @@ extern void sqlp_expr_cmp(struct psql_state *pstate, int comp);
extern void sqlp_expr_cmp_sel(struct psql_state *pstate, int sel_type, int comp);
extern void sqlp_expr_is_bool(struct psql_state *pstate, int val);
extern void sqlp_expr_is_in(struct psql_state *pstate, int val);
+extern void sqlp_expr_paren(struct psql_state *pstate);
extern void sqlp_fieldname(struct psql_state *pstate, const char *db_name, const char *name);
extern void sqlp_float(struct psql_state *pstate, float val);
extern void sqlp_group_by_list(struct psql_state *pstate, int n_list, int opts);
diff --git a/sql.y b/sql.y
index 5d149df..a5a28db 100644
--- a/sql.y
+++ b/sql.y
@@ -848,6 +848,7 @@ expr: NAME { sqlp_name(pstate, $1); free($1); }
| INTNUM { sqlp_number(pstate, $1); }
| APPROXNUM { sqlp_float(pstate, $1); }
| BOOL { sqlp_bool(pstate, $1); }
+ | NULLX { sqlp_null(pstate); }
;
expr: expr '+' expr { sqlp_expr_op(pstate, SEO_ADD); }
@@ -959,6 +960,8 @@ expr: CURRENT_TIMESTAMP { sqlp_now(pstate); };
expr: BINARY expr %prec UMINUS { sqlp_expr_op(pstate, SEO_STRTOBIN); }
;
+expr: '(' expr ')' { sqlp_expr_paren(pstate); }
+
%%
void
root@mask:~/sqlfun# make && make check
: -o sql.c sql.l
false -vd sql.y
make: *** [sql.tab.c] Error 1
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.