|
@@ -31,14 +31,20 @@ cJSON* jroot;
|
|
|
|
|
|
%token SELECT FROM WHERE INSERT INTO VALUES DELETE UPDATE SET JOIN CREATE TABLE
|
|
|
%token AND OR NOT IN
|
|
|
-%token INT FLOAT STRING
|
|
|
+%token INT_V FLOAT_V STRING_V // 作为 value 出现的
|
|
|
+%token INT_T FLOAT_T STRING_T // 作为 字面量 type 出现的情况
|
|
|
+
|
|
|
%token LPAREN RPAREN COMMA
|
|
|
%token PRIMARY_KEY
|
|
|
|
|
|
%token QUIT NEWLINE
|
|
|
|
|
|
+%type <iv> INT_V
|
|
|
+%type <fv> FLOAT_V
|
|
|
+%type <sv> STRING_V
|
|
|
%type <sv> IDENTIFIER data_type PRIMARY_KEY col_options
|
|
|
-%type <jv> create_definition create_col_list create_table_stmt
|
|
|
+%type <jv> create_definition create_col_list create_table_stmt data_value
|
|
|
+%type <jv> insert_stmt insert_list
|
|
|
|
|
|
%start statement
|
|
|
|
|
@@ -49,7 +55,9 @@ statement: NEWLINE
|
|
|
| sql_statement
|
|
|
;
|
|
|
|
|
|
-sql_statement: create_table_stmt
|
|
|
+sql_statement: create_table_stmt
|
|
|
+ | insert_stmt
|
|
|
+;
|
|
|
|
|
|
create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
|
|
|
{
|
|
@@ -106,10 +114,56 @@ col_options: {$$ = "";}
|
|
|
$$ = $1;
|
|
|
}
|
|
|
|
|
|
-data_type: INT
|
|
|
- | FLOAT
|
|
|
- | STRING
|
|
|
+data_type: INT_T
|
|
|
+ | FLOAT_T
|
|
|
+ | STRING_T
|
|
|
+;
|
|
|
+
|
|
|
+insert_stmt: INSERT INTO IDENTIFIER VALUES '(' insert_list ')' NEWLINE {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "insert");
|
|
|
+ cJSON_AddStringToObject(node, "table_name", $3);
|
|
|
+ cJSON_AddItemToObject(node, "values", $6);
|
|
|
+ cJSON_AddItemToArray(jroot, node);
|
|
|
+ $$=node;
|
|
|
+};
|
|
|
+
|
|
|
+insert_list: data_value {
|
|
|
+ cJSON* node = cJSON_CreateArray();
|
|
|
+ cJSON_AddItemToArray(node, $1);
|
|
|
+ $$=node;
|
|
|
+ // printf("得create_definition %s\n", cJSON_Print($1));
|
|
|
+}
|
|
|
+ | insert_list ',' data_value {
|
|
|
+ // printf("迭代 create_col_list\n %s", cJSON_Print($1));
|
|
|
+ // printf("迭代 create_definition\n %s", cJSON_Print($3));
|
|
|
+
|
|
|
+ cJSON_AddItemToArray($1, $3);
|
|
|
+
|
|
|
+ $$=$1;
|
|
|
+ // printf("迭代\n %s", cJSON_Print($$));
|
|
|
+ }
|
|
|
;
|
|
|
+data_value: INT_V {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "int");
|
|
|
+ cJSON_AddNumberToObject(node, "value", $1);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+ | FLOAT_V {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "float");
|
|
|
+ cJSON_AddNumberToObject(node, "value", $1);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+ | STRING_V {
|
|
|
+ cJSON* node = cJSON_CreateObject();
|
|
|
+ cJSON_AddStringToObject(node, "type", "string");
|
|
|
+ cJSON_AddStringToObject(node, "value", $1);
|
|
|
+ $$=node;
|
|
|
+ }
|
|
|
+;
|
|
|
+
|
|
|
%%
|
|
|
|
|
|
int main(int ac, char** av) {
|