calc.y 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. %{
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <cjson/cJSON.h>
  6. #include <signal.h>
  7. extern int yylex();
  8. extern int yyparse();
  9. extern FILE* yyin;
  10. extern char* yytext;
  11. int yydebug=1;
  12. void yyerror(const char* s);
  13. char* catstr(char* s1, char* s2);
  14. cJSON* jroot;
  15. #define SIMPLE_OP_NODE(res, type, left, right) \
  16. fprintf(stderr, "SIMPLE_OP_NODE type: %s %s %s\n", (type), cJSON_Print(left), cJSON_Print(right)); \
  17. cJSON* node = cJSON_CreateObject(); \
  18. cJSON_AddStringToObject(node, "type", type); \
  19. cJSON_AddItemToObject(node, "left", left); \
  20. cJSON_AddItemToObject(node, "right", right); \
  21. res = node;
  22. #define SIMPLE_OP_NODE_ONLY_LEFT(res, type, left) \
  23. fprintf(stderr, "SIMPLE_OP_NODE_ONLY_LEFT type: %s %s %s\n", (type), cJSON_Print(left)); \
  24. cJSON* node = cJSON_CreateObject(); \
  25. cJSON_AddStringToObject(node, "type", type); \
  26. cJSON_AddItemToObject(node, "left", left); \
  27. res = node;
  28. #define SIMPLE_TYPE_VALUE_OBJECT(res, type_name, json_type_name, value) \
  29. cJSON* node = cJSON_CreateObject(); \
  30. cJSON_AddStringToObject(node, "type", #type_name); \
  31. cJSON_Add##json_type_name##ToObject(node, "value", value); \
  32. res = node;
  33. %}
  34. %union {
  35. int iv;
  36. double fv;
  37. char* sv;
  38. cJSON* jv;
  39. int subtok;
  40. }
  41. %token IDENTIFIER
  42. %token SELECT FROM WHERE INSERT INTO VALUES DELETE UPDATE SET JOIN CREATE TABLE
  43. %token AS ON
  44. %token AND OR NOT IN
  45. %token INT_V FLOAT_V STRING_V // 作为 value 出现的
  46. %token INT_T FLOAT_T STRING_T // 作为 字面量 type 出现的情况
  47. %token LPAREN RPAREN COMMA
  48. %token PRIMARY_KEY
  49. %token QUIT NEWLINE
  50. %type <iv> INT_V
  51. %type <fv> FLOAT_V
  52. %type <sv> STRING_V NOT
  53. %type <sv> IDENTIFIER data_type PRIMARY_KEY col_options bin_cmp_op bin_logical_op unary_compare_op bin_contains_op
  54. %type <jv> create_definition create_col_list create_table_stmt data_value
  55. %type <jv> insert_stmt insert_list
  56. %type <jv> update_stmt update_list single_assign_item
  57. %type <jv> where_condition_item where_conditions identifier identifier_or_const_value the_whole_where_smt
  58. %type <jv> delete_stmt select_stmt select_item select_items
  59. %type <jv> data_value_list identifier_or_const_value_or_const_value_list
  60. %type <jv> search_expr compare_expr single_expr expr where_expr logical_expr negative_expr
  61. // %left OR
  62. // %left AND
  63. // %right NOT
  64. // %nonassoc '=' '>' '<'
  65. // %left '+' '-'
  66. // %left '*' '/'
  67. %start statement
  68. %%
  69. statement: NEWLINE
  70. | QUIT {printf("bye!\n"); exit(0); }
  71. | sql_statement
  72. ;
  73. sql_statement: create_table_stmt
  74. | insert_stmt
  75. | update_stmt
  76. | delete_stmt
  77. | select_stmt
  78. ;
  79. create_table_stmt: CREATE TABLE IDENTIFIER NEWLINE
  80. {
  81. cJSON* node = cJSON_CreateObject();
  82. cJSON_AddStringToObject(node, "type", "create_table");
  83. cJSON_AddStringToObject(node, "table_name", $3);
  84. cJSON_AddItemToObject(node, "cols", cJSON_CreateArray());
  85. cJSON_AddItemToArray(jroot, node);
  86. }
  87. create_table_stmt: CREATE TABLE IDENTIFIER '(' create_col_list ')' NEWLINE {
  88. cJSON* node = cJSON_CreateObject();
  89. cJSON_AddStringToObject(node, "type", "create_table");
  90. cJSON_AddStringToObject(node, "table_name", $3);
  91. cJSON_AddItemToObject(node, "cols", $5);
  92. cJSON_AddItemToArray(jroot, node);
  93. $$=node;
  94. };
  95. create_col_list: create_definition {
  96. cJSON* node = cJSON_CreateArray();
  97. cJSON_AddItemToArray(node, $1);
  98. $$=node;
  99. // printf("得create_definition %s\n", cJSON_Print($1));
  100. }
  101. | create_col_list ',' create_definition {
  102. // printf("迭代 create_col_list\n %s", cJSON_Print($1));
  103. // printf("迭代 create_definition\n %s", cJSON_Print($3));
  104. cJSON_AddItemToArray($1, $3);
  105. $$=$1;
  106. // printf("迭代\n %s", cJSON_Print($$));
  107. }
  108. ;
  109. create_definition: IDENTIFIER data_type col_options {
  110. // fprintf(stderr, "得列 %s %s with options %s: %d\n", $1, $2, $3, strlen($3));
  111. cJSON* node = cJSON_CreateObject();
  112. cJSON_AddStringToObject(node, "type", "create_column");
  113. cJSON_AddStringToObject(node, "column_name", $1);
  114. cJSON_AddStringToObject(node, "data_type", $2);
  115. if (strlen($3) > 0){
  116. cJSON_AddTrueToObject(node, "primary_key");
  117. } else {
  118. cJSON_AddFalseToObject(node, "primary_key");
  119. }
  120. $$ = node;
  121. };
  122. col_options: {$$ = "";}
  123. | PRIMARY_KEY {
  124. // printf("得到主键 %s\n", $1);
  125. $$ = $1;
  126. }
  127. data_type: INT_T
  128. | FLOAT_T
  129. | STRING_T
  130. ;
  131. insert_stmt: INSERT INTO IDENTIFIER VALUES '(' insert_list ')' NEWLINE {
  132. cJSON* node = cJSON_CreateObject();
  133. cJSON_AddStringToObject(node, "type", "insert");
  134. cJSON_AddStringToObject(node, "table_name", $3);
  135. cJSON_AddItemToObject(node, "values", $6);
  136. cJSON_AddItemToArray(jroot, node);
  137. $$=node;
  138. };
  139. insert_list: data_value {
  140. cJSON* node = cJSON_CreateArray();
  141. cJSON_AddItemToArray(node, $1);
  142. $$=node;
  143. // printf("得create_definition %s\n", cJSON_Print($1));
  144. }
  145. | insert_list ',' data_value {
  146. // printf("迭代 create_col_list\n %s", cJSON_Print($1));
  147. // printf("迭代 create_definition\n %s", cJSON_Print($3));
  148. cJSON_AddItemToArray($1, $3);
  149. $$=$1;
  150. // printf("迭代\n %s", cJSON_Print($$));
  151. }
  152. ;
  153. data_value: INT_V {SIMPLE_TYPE_VALUE_OBJECT($$, int, Number, $1);}
  154. | FLOAT_V {SIMPLE_TYPE_VALUE_OBJECT($$, float, Number, $1);}
  155. | STRING_V {SIMPLE_TYPE_VALUE_OBJECT($$, string, String, $1);}
  156. ;
  157. update_stmt: UPDATE IDENTIFIER SET update_list WHERE where_expr NEWLINE {
  158. cJSON* node = cJSON_CreateObject();
  159. cJSON_AddStringToObject(node, "type", "update");
  160. cJSON_AddStringToObject(node, "table_name", $2);
  161. cJSON_AddItemToObject(node, "set", $4);
  162. cJSON_AddItemToObject(node, "where", $6);
  163. cJSON_AddItemToArray(jroot, node);
  164. $$=node;
  165. };
  166. update_list: single_assign_item {
  167. cJSON* node = cJSON_CreateArray();
  168. cJSON_AddItemToArray(node, $1);
  169. $$=node;
  170. }
  171. | update_list ',' single_assign_item {
  172. cJSON_AddItemToArray($1, $3);
  173. $$=$1;
  174. }
  175. ;
  176. single_assign_item: identifier '=' identifier_or_const_value {
  177. cJSON* node = cJSON_CreateObject();
  178. cJSON_AddStringToObject(node, "type", "assign_const");
  179. cJSON_AddItemToObject(node, "left", $1);
  180. cJSON_AddItemToObject(node, "right", $3);
  181. $$=node;
  182. }
  183. ;
  184. the_whole_where_smt: {$$=cJSON_CreateObject();}
  185. | WHERE where_expr {$$=$2;}
  186. ;
  187. // where 后的条件跟 select 中的有本质区别
  188. // select 中的只是常量或者字段名, 而 where 中的是表达式
  189. single_expr: identifier {$$=$1;}
  190. | IDENTIFIER '.' IDENTIFIER {
  191. cJSON* node = cJSON_CreateObject();
  192. cJSON_AddStringToObject(node, "type", "table_field");
  193. cJSON_AddStringToObject(node, "table", $1);
  194. cJSON_AddStringToObject(node, "field", $3);
  195. $$=node;
  196. }
  197. | data_value {$$=$1;}
  198. | '(' single_expr ')' {$$=$2;}
  199. ;
  200. where_expr: logical_expr {$$=$1;};
  201. ;
  202. compare_expr: compare_expr bin_cmp_op compare_expr {SIMPLE_OP_NODE($$, $2, $1, $3);}
  203. | single_expr {$$=$1;}
  204. ;
  205. negative_expr: NOT negative_expr {SIMPLE_OP_NODE_ONLY_LEFT($$, "非", $2);}
  206. | compare_expr {$$=$1;}
  207. ;
  208. logical_expr: negative_expr bin_logical_op negative_expr {SIMPLE_OP_NODE($$, $2, $1, $3);}
  209. | single_expr {$$=$1;}
  210. ;
  211. // expr: compare_expr
  212. // | negative_expr
  213. // | logical_expr
  214. // ;
  215. // compare_expr: expr bin_cmp_op single_expr
  216. // expr: expr bin_cmp_op expr {SIMPLE_OP_NODE($$, $2, $1, $3)}
  217. // | expr bin_cmp_op '(' expr ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
  218. // // | '(' expr ')' {$$=$2;}
  219. // // | NOT expr {SIMPLE_OP_NODE_ONLY_LEFT($$, "非", $2);
  220. // // fprintf(stderr, "非asd %s\n", cJSON_Print($2));
  221. // // }
  222. // ;
  223. // // expr: expr bin_logical_op expr {SIMPLE_OP_NODE($$, $2, $1, $3)}
  224. // expr_list: expr{
  225. // cJSON* node = cJSON_CreateArray();
  226. // cJSON_AddItemToArray(node, $1);
  227. // $$=node;
  228. // }
  229. // | expr_list ',' expr {
  230. // fprintf(stderr, "迭代 expr_list\n %s", cJSON_Print($1));
  231. // cJSON_AddItemToArray($1, $3);
  232. // $$=$1;
  233. // }
  234. // expr: expr bin_contains_op '(' expr_list ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
  235. // | expr bin_contains_op '(' select_stmt ')' {SIMPLE_OP_NODE($$, $2, $1, $4)}
  236. where_conditions: where_condition_item {
  237. $$=$1;
  238. }
  239. | '(' where_condition_item ')' {
  240. $$=$2;
  241. }
  242. | where_conditions bin_logical_op where_condition_item {
  243. cJSON* node = cJSON_CreateObject();
  244. cJSON_AddStringToObject(node, "type", $2);
  245. cJSON_AddItemToObject(node, "left", $1);
  246. cJSON_AddItemToObject(node, "right", $3);
  247. $$=node;
  248. }
  249. | unary_compare_op where_condition_item {
  250. cJSON* node = cJSON_CreateObject();
  251. cJSON_AddStringToObject(node, "type", $1);
  252. cJSON_AddItemToObject(node, "right", $2);
  253. $$=node;
  254. }
  255. | where_conditions bin_logical_op '(' where_conditions ')' {
  256. cJSON* node = cJSON_CreateObject();
  257. cJSON_AddStringToObject(node, "type", $2);
  258. cJSON_AddItemToObject(node, "left", $1);
  259. cJSON_AddItemToObject(node, "right", $4);
  260. $$=node;
  261. }
  262. ;
  263. identifier: IDENTIFIER {
  264. cJSON* node = cJSON_CreateObject();
  265. cJSON_AddStringToObject(node, "type", "identifier");
  266. cJSON_AddStringToObject(node, "value", $1);
  267. $$=node;
  268. }
  269. identifier_or_const_value: identifier {$$=$1;}
  270. | data_value {$$=$1;}
  271. ;
  272. data_value_list: data_value {
  273. cJSON* node = cJSON_CreateArray();
  274. cJSON_AddItemToArray(node, $1);
  275. $$=node;
  276. }
  277. | data_value_list ',' data_value {
  278. cJSON_AddItemToArray($1, $3);
  279. $$=$1;
  280. }
  281. ;
  282. identifier_or_const_value_or_const_value_list: identifier_or_const_value{$$=$1;}
  283. | data_value_list {$$=$1;}
  284. where_condition_item: identifier_or_const_value bin_cmp_op identifier_or_const_value {
  285. cJSON* node = cJSON_CreateObject();
  286. cJSON_AddStringToObject(node, "type", $2);
  287. cJSON_AddItemToObject(node, "left", $1);
  288. cJSON_AddItemToObject(node, "right", $3);
  289. $$=node;
  290. }
  291. | identifier_or_const_value bin_contains_op '(' data_value_list ')' {
  292. cJSON* node = cJSON_CreateObject();
  293. cJSON_AddStringToObject(node, "type", $2);
  294. cJSON_AddItemToObject(node, "left", $1);
  295. cJSON_AddItemToObject(node, "right", $4);
  296. $$=node;
  297. }
  298. ;
  299. bin_cmp_op: '=' {$$ = "相等";}
  300. | '>' {$$ = "大于";}
  301. | '<' {$$ = "小于";}
  302. | ">=" {$$ = "大等";}
  303. | "<=" {$$ = "小等";}
  304. | "!=" {$$ = "不等";}
  305. | IN {$$ = "包含于";}
  306. ;
  307. unary_compare_op: NOT {$$ = "非";};
  308. bin_logical_op: AND {$$ = "且";}
  309. | OR {$$ = "或";}
  310. bin_contains_op: IN {$$ = "包含于";}
  311. | NOT IN {$$ = "不包含于";}
  312. delete_stmt: DELETE FROM IDENTIFIER the_whole_where_smt NEWLINE {
  313. cJSON* node = cJSON_CreateObject();
  314. cJSON_AddStringToObject(node, "type", "delete");
  315. cJSON_AddStringToObject(node, "table_name", $3);
  316. cJSON_AddItemToObject(node, "where", $4);
  317. cJSON_AddItemToArray(jroot, node);
  318. $$=node;
  319. };
  320. select_stmt: SELECT select_items FROM IDENTIFIER the_whole_where_smt NEWLINE {
  321. cJSON* node = cJSON_CreateObject();
  322. cJSON_AddStringToObject(node, "type", "select");
  323. cJSON_AddItemToObject(node, "select_cols", $2);
  324. cJSON_AddStringToObject(node, "table_name", $4);
  325. cJSON_AddItemToObject(node, "where", $5);
  326. $$=node;
  327. cJSON_AddItemToArray(jroot, node);
  328. };
  329. select_items: select_item {
  330. cJSON* node = cJSON_CreateArray();
  331. cJSON_AddItemToArray(node, $1);
  332. $$=node;
  333. }
  334. | select_items ',' select_item {
  335. cJSON_AddItemToArray($1, $3);
  336. $$=$1;
  337. }
  338. select_item: identifier_or_const_value {
  339. cJSON* node = cJSON_CreateObject();
  340. cJSON_AddStringToObject(node, "type", "select_column");
  341. cJSON_AddItemToObject(node, "target", $1);
  342. $$=node;
  343. }
  344. | identifier_or_const_value AS IDENTIFIER {
  345. cJSON* node = cJSON_CreateObject();
  346. cJSON_AddStringToObject(node, "type", "select_column");
  347. cJSON_AddItemToObject(node, "target", $1);
  348. cJSON_AddStringToObject(node, "alias", $3);
  349. $$=node;
  350. }
  351. | '*' {
  352. cJSON* node = cJSON_CreateObject();
  353. cJSON_AddStringToObject(node, "type", "select_all_column");
  354. $$=node;
  355. }
  356. |
  357. ;
  358. %%
  359. int main(int ac, char** av) {
  360. jroot = cJSON_CreateArray();
  361. if (ac != 2) {
  362. printf("Usage: %s <sql>\n", av[0]);
  363. exit(1);
  364. }
  365. // yyin = av[1];
  366. yy_scan_string(av[1]);
  367. int res = yyparse();
  368. printf("%s\n", cJSON_Print(jroot));
  369. return res;
  370. }
  371. void yyerror(const char *s) {
  372. fprintf(stderr, "error: %s at %s\n", s, yytext);
  373. exit(1);
  374. }
  375. char* catstr(char* s1, char* s2) {
  376. char* s = malloc(strlen(s1)+strlen(s2)+1);
  377. strcpy(s, s1);
  378. strcat(s, s2);
  379. return s;
  380. }