|
@@ -0,0 +1,71 @@
|
|
|
|
+%{
|
|
|
|
+
|
|
|
|
+#include <stdio.h>
|
|
|
|
+#include <string.h>
|
|
|
|
+#include <stdlib.h>
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+extern int yylex();
|
|
|
|
+extern int yyparse();
|
|
|
|
+extern FILE* yyin;
|
|
|
|
+extern char* yytext;
|
|
|
|
+
|
|
|
|
+int yydebug=1;
|
|
|
|
+
|
|
|
|
+void yyerror(const char* s);
|
|
|
|
+%}
|
|
|
|
+
|
|
|
|
+%union {
|
|
|
|
+ int iv;
|
|
|
|
+ double fv;
|
|
|
|
+ char* sv;
|
|
|
|
+ int subtok;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+%token IDENTIFIER
|
|
|
|
+
|
|
|
|
+%token SELECT FROM WHERE INSERT INTO VALUES DELETE UPDATE SET JOIN CREATE TABLE
|
|
|
|
+%token AND OR NOT IN
|
|
|
|
+%token INT FLOAT STRING
|
|
|
|
+%token LPAREN RPAREN COMMA
|
|
|
|
+
|
|
|
|
+%token QUIT NEWLINE
|
|
|
|
+
|
|
|
|
+%start statement
|
|
|
|
+
|
|
|
|
+%%
|
|
|
|
+
|
|
|
|
+statement: NEWLINE
|
|
|
|
+ | QUIT {printf("bye!\n"); exit(0); }
|
|
|
|
+ | sql_statement { printf("%s\n", yytext); exit(0);}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+;
|
|
|
|
+
|
|
|
|
+sql_statement: create_statement
|
|
|
|
+
|
|
|
|
+create_statement: CREATE TABLE IDENTIFIER '(' ')' NEWLINE
|
|
|
|
+ {
|
|
|
|
+ printf("CREATE TABLE %s\n", yytext);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+%%
|
|
|
|
+
|
|
|
|
+int main(int ac, char** av) {
|
|
|
|
+ if (ac != 2) {
|
|
|
|
+ printf("Usage: %s <sql>\n", av[0]);
|
|
|
|
+ exit(1);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // yyin = av[1];
|
|
|
|
+ yy_scan_string(av[1]);
|
|
|
|
+
|
|
|
|
+ return yyparse();
|
|
|
|
+
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void yyerror(const char *s) {
|
|
|
|
+ printf("error: %s at %s\n", s, yytext);
|
|
|
|
+ exit(1);
|
|
|
|
+}
|
|
|
|
+
|