Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.17 KB | None | 0 0
  1. %{
  2.     #define YYERROR_VERBOSE
  3.     #define YYSTYPE double
  4.     #include <stdio.h>
  5.     #include <stdlib.h>
  6.     #include <iostream>
  7.     #include <string>
  8.     #include "../../src/Parser/Headers/ParserLogic.h"
  9.     ParserLogic logicApi;
  10.  
  11.     extern int yylineno;
  12.     extern int ch;
  13.     extern char *yytext;
  14.     int yylex();
  15.     int yyerror(const char *s);
  16.     BigResponse response;
  17. %}
  18.  
  19. %token <string> STRING OTHER SEMICOLON COMMA DDLCREATE DDLSHOW DDLDROP BRACKET TYPE CONSTRAINT DMLINSERT VALUES COMP
  20. NUMBER WHERE EQUALLY FROM DQLSELECT DMLDELETE DMLUPDATE SET ALL VALNULL FLOATNUM SIGN VALSTR LOGIC NOT NOTEQUALLY DIV
  21. %token <string> STRINGG
  22. %token <string> INT
  23. %token <string> NAME
  24.  
  25. %type<string> id
  26.  
  27.  
  28. %union{
  29.     //char string[256];
  30.     int number;
  31.     char *string;
  32. }
  33.  
  34.  
  35.  
  36. %%
  37.  
  38. request:
  39.     id
  40. ;
  41.  
  42. id:
  43.     STRINGG { $$ = $1;}
  44. //    actions SEMICOLON {
  45. //      response = logicApi.finish();
  46. //      printf("FINISH\n");
  47. //    }
  48. //    |
  49. //    request request
  50. ;
  51.  
  52. actions:
  53.     DDLCREATE STRING inner_expr {
  54.         logicApi.addActionName($1);
  55.         logicApi.addTableName($2);
  56.     }
  57.     |
  58.     DDLDROP STRING{
  59.         logicApi.addActionName($1);
  60.         logicApi.addTableName($2);
  61.     }
  62.     |
  63.     DDLSHOW STRING{
  64.         logicApi.addActionName($1);
  65.         logicApi.addTableName($2);
  66.     }
  67.     |
  68.     DMLINSERT insert_where {
  69.         logicApi.addActionName($1);
  70.     printf("INSERT\n");
  71.     }
  72.     |
  73.     table_select {
  74.         logicApi.addActionName("select");
  75.  
  76.         printf("SELECT\n");
  77.     }
  78.     |
  79.     table_delete {
  80.         logicApi.addActionName("delete");
  81.         printf("DELETE\n");
  82.     }
  83.     |
  84.     DMLUPDATE STRING table_update {
  85.         logicApi.addActionName($1);
  86.         logicApi.addTableName($2);
  87.         printf("%s, %s\n", $1, $2);
  88.     }
  89.  
  90. table_update:
  91.     update_set
  92.     |
  93.     table_update WHERE where
  94.  
  95. update_set:
  96.     SET STRING EQUALLY VALSTR {
  97.         logicApi.addColumn($2);
  98.         logicApi.addValue($4);
  99.         printf("UPCOL = %s, UPVALSTR = %s\n", $2, $4);
  100.     }
  101.     |
  102.     SET STRING EQUALLY NUMBER {
  103.         logicApi.addColumn($2);
  104.     logicApi.addValue($4);
  105.         printf("UPCOL = %s, UPVALNUM = %s\n", $2, $4);
  106.     }
  107.     |
  108.     SET STRING EQUALLY VALNULL {
  109.         logicApi.addColumn($2);
  110.         logicApi.addValue($4);
  111.         printf("UPCOL = %s, UPVALNULL = %s\n", $2, $4);
  112.     }
  113.     |
  114.     SET STRING EQUALLY FLOATNUM {
  115.         logicApi.addColumn($2);
  116.         logicApi.addValue($4);
  117.         printf("UPCOL = %s, UPVALFLOAT = %s\n", $2, $4);
  118.     }
  119.     |
  120.     update_set COMMA STRING EQUALLY VALSTR {
  121.         logicApi.addColumn($3);
  122.         logicApi.addValue($5);
  123.         printf("UPCOL = %s, UPVALSTR = %s\n", $3, $5);
  124.     }
  125.     |
  126.     update_set COMMA STRING EQUALLY NUMBER {
  127.     logicApi.addColumn($3);
  128.         logicApi.addValue($5);
  129.         printf("UPCOL = %s, UPVALNUM = %s\n", $3, $5);
  130.     }
  131.     |
  132.     update_set COMMA STRING EQUALLY VALNULL {
  133.         logicApi.addColumn($3);
  134.         logicApi.addValue($5);
  135.         printf("UPCOL = %s, UPVALNULL = %s\n", $3, $5);
  136.     }
  137.     |
  138.     update_set COMMA STRING EQUALLY FLOATNUM {
  139.         logicApi.addColumn($3);
  140.         logicApi.addValue($5);
  141.         printf("UPCOL = %s, UPVALFLOAT = %s\n", $3, $5);
  142.     }
  143.  
  144. table_delete:
  145.     DMLDELETE STRING {
  146.         logicApi.addTableName($2);
  147.         printf("TABLE = %s\n", $2);
  148.     }
  149.     |
  150.     DMLDELETE STRING WHERE where {
  151.         logicApi.addTableName($2);
  152.         printf("TABLE = %s\n", $2);
  153.     }
  154.  
  155. table_select:
  156.     col_select FROM STRING{
  157.         logicApi.addTableName($3);
  158.         printf("TABLE = %s\n", $3);
  159.     }
  160.     |
  161.     table_select WHERE where
  162.  
  163. col_select:
  164.     DQLSELECT STRING {
  165.         logicApi.addSelectColumn($2);
  166.         printf("COL = %s\n", $2);
  167.     }
  168.     |
  169.     DQLSELECT ALL {
  170.         logicApi.addSelectColumn($2);
  171.         printf("COLALL = %s\n", $2);
  172.     }
  173.     |
  174.     col_select COMMA STRING {
  175.         logicApi.addSelectColumn($3);
  176.         printf("COL = %s\n", $3);
  177.     }
  178.     |
  179.     col_select COMMA ALL {
  180.         logicApi.addSelectColumn($3);
  181.         printf("COLALL = %s\n", $3);
  182.     }
  183.  
  184. insert_where:
  185.     insert
  186.  
  187. where:
  188.    expr3|
  189.     NOT where {
  190.         logicApi.expression.addLogicOperator($1);
  191.         printf("NOT\n");
  192.     }|
  193.     where LOGIC expr3 {
  194.         logicApi.expression.addLogicOperator($2);
  195.         printf("%s\n", $2);
  196.     }
  197.  
  198. expr3:
  199.     expr2|
  200.     expr3 NOTEQUALLY expr2 {
  201.         //logicApi.expression.addColumn($2);
  202.         printf("%s\n", $2);
  203.     }|
  204.     expr3 EQUALLY expr2 {
  205.         logicApi.expression.addOperand($2);
  206.         printf("%s\n", $2);
  207.     }|
  208.     expr3 NOTEQUALLY expr2 {
  209.         //logicApi.expression.addColumn($2);
  210.         printf("%s \n", $2);
  211.     }|
  212.     expr3 COMP expr2 {
  213.         //logicApi.expression.addColumn($2);
  214.         printf("%s \n", $2);
  215.     }
  216.  
  217. expr2:
  218.     expr1|
  219.     expr2 SIGN expr1 {
  220.         logicApi.expression.addOperand($2);
  221.         printf("%s", $2);
  222.     }
  223.  
  224. expr1:
  225.     expr|
  226.     expr1 ALL expr {
  227.         logicApi.expression.addOperand($2);
  228.         printf("%s", $2);
  229.     }|
  230.     expr1 DIV expr {
  231.         logicApi.expression.addOperand($2);
  232.         printf("%s", $2);
  233.     }
  234.  
  235.  
  236. expr:
  237.     STRING {
  238.         logicApi.expression.addOperand($1);
  239.         printf("%s\n", $1);
  240.     }
  241.     |
  242.     NUMBER {
  243.         logicApi.expression.addOperand($1);
  244.         printf("N=%s \n", $1);
  245.     }
  246.     |
  247.     FLOATNUM {
  248.         logicApi.expression.addOperand($1);
  249.         printf("%s\n", $1);
  250.     }
  251.     |
  252.     BRACKET where BRACKET {
  253.             printf("%s %s\n", $1, $3);
  254.     }
  255.  
  256. //where:
  257. //    WHERE STRING EQUALLY VALSTR {
  258. //      logicApi.addCondition($2, $3, $4);
  259. //        printf("WHERE %s %s %s\n", $2, $3, $4);
  260. //    }
  261. //    |
  262. //    WHERE STRING EQUALLY NUMBER {
  263. //      logicApi.addCondition($2, $3, $4);
  264. //        printf("WHERE %s %s %s\n", $2, $3, $4);
  265. //    }
  266. //    |
  267. //    WHERE STRING EQUALLY FLOATNUM {
  268. //      logicApi.addCondition($2, $3, $4);
  269. //        printf("WHERE %s %s %s\n", $2, $3, $4);
  270. //    }
  271. //    |
  272. //    WHERE STRING EQUALLY VALNULL {
  273. //      logicApi.addCondition($2, $3, $4);
  274. //        printf("WHERE %s %s %s\n", $2, $3, $4);
  275. //    }
  276.  
  277. insert:
  278.     STRING values {
  279.         logicApi.addTableName($1);
  280.     printf("TABLE = %s\n", $1);
  281.     }
  282.     | STRING BRACKET STRING {
  283.         logicApi.addTableName($1);
  284.         logicApi.addColumn($3);
  285.         printf("TABLE = %s COL = %s\n", $1, $3);
  286.     }
  287.     | insert COMMA STRING {
  288.         logicApi.addColumn($3);
  289.         printf("COL = %s\n", $3);
  290.     }
  291.     | insert BRACKET
  292.     | insert values
  293.  
  294. values:
  295.     VALUES BRACKET
  296.     |
  297.     values VALSTR {
  298.         logicApi.addValue($2);
  299.         std::cout << $2 << std::endl;
  300.         printf("VALSTR = %s\n", $2);
  301.     }
  302.     |
  303.     values NUMBER {
  304.         logicApi.addValue($2);
  305.         printf("VALNUM = %s\n", $2);
  306.     }
  307.     |
  308.     values VALNULL {
  309.         logicApi.addValue($2);
  310.         printf("VALNULL = %s\n", $2);
  311.     }
  312.     |
  313.     values FLOATNUM {
  314.         logicApi.addValue($2);
  315.         printf("VALFLOAT = %s\n", $2);
  316.     }
  317.     |
  318.     values COMMA VALSTR {
  319.         logicApi.addValue($3);
  320.          printf("VALSTR = %s\n", $3);
  321.     }
  322.     |
  323.     values COMMA NUMBER {
  324.         logicApi.addValue($3);
  325.         printf("VALNUM = %s\n", $3);
  326.     }
  327.     |
  328.     values COMMA VALNULL {
  329.         logicApi.addValue($3);
  330.         printf("VALNULL = %s\n", $3);
  331.     }
  332.     |
  333.     values COMMA FLOATNUM {
  334.         logicApi.addValue($3);
  335.         printf("VALFLOAT = %s\n", $3);
  336.     }
  337.     |
  338.     values BRACKET
  339.  
  340. inner_expr:
  341.     BRACKET STRING TYPE {
  342.     logicApi.addColumn($2, $3);
  343.     }
  344.     | inner_expr CONSTRAINT {
  345.         logicApi.addConstraint($2);
  346.     }
  347.     | inner_expr COMMA STRING TYPE {
  348.     logicApi.addColumn($3, $4);
  349.     }
  350.     | inner_expr BRACKET;
  351.  
  352. %%
  353.  
  354. void set_input_string(const char* in);
  355. void end_string_scan(void);
  356.  
  357.  
  358. BigResponse parse_request(const char* in) {
  359.   ch = 0;
  360.   logicApi.start();
  361.   response.clear();
  362.  
  363.   set_input_string(in);
  364.   int res = yyparse();
  365.   end_string_scan();
  366.   response.error.setErrorCode(response.error.getErrorCode() || res);
  367.   return response;
  368. }
  369.  
  370. int yyerror(const char *errmsg){
  371.     std::string str = std::string(errmsg) + " (Str num " + std::to_string(yylineno) + ", sym num " + std::to_string(ch) +"): "+ std::string(yytext);
  372.     response.error.setErrorMsg(str);
  373.     //fprintf(stderr, "%s (Str num %d, sym num %d): %s\n", errmsg, yylineno, ch, yytext);
  374.  
  375.     return 0;
  376. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement