未验证 提交 28eee7d5 编写于 作者: 羽飞's avatar 羽飞 提交者: GitHub

opt lex and yacc (#123)

上级 da389bf8
...@@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC}) ...@@ -24,7 +24,7 @@ FOREACH (F ${ALL_SRC})
ENDFOREACH (F) ENDFOREACH (F)
SET(LIBRARIES common pthread dl event_pthreads event jsoncpp) SET(LIBRARIES common pthread dl event_pthreads event libjsoncpp.a)
# 指定目标文件位置 # 指定目标文件位置
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin) SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin)
......
#!/bin/bash
flex --outfile lex_sql.cpp --header-file=lex_sql.h lex_sql.l
`which bison` -d --output yacc_sql.cpp yacc_sql.y
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
#define yyHEADER_H 1 #define yyHEADER_H 1
#define yyIN_HEADER 1 #define yyIN_HEADER 1
#line 6 "lex.yy.h" #line 6 "lex_sql.h"
#line 8 "lex_sql.h"
#define YY_INT_ALIGNED short int #define YY_INT_ALIGNED short int
...@@ -49,7 +51,6 @@ typedef int16_t flex_int16_t; ...@@ -49,7 +51,6 @@ typedef int16_t flex_int16_t;
typedef uint16_t flex_uint16_t; typedef uint16_t flex_uint16_t;
typedef int32_t flex_int32_t; typedef int32_t flex_int32_t;
typedef uint32_t flex_uint32_t; typedef uint32_t flex_uint32_t;
typedef uint64_t flex_uint64_t;
#else #else
typedef signed char flex_int8_t; typedef signed char flex_int8_t;
typedef short int flex_int16_t; typedef short int flex_int16_t;
...@@ -160,7 +161,7 @@ struct yy_buffer_state ...@@ -160,7 +161,7 @@ struct yy_buffer_state
/* Number of characters read into yy_ch_buf, not including EOB /* Number of characters read into yy_ch_buf, not including EOB
* characters. * characters.
*/ */
yy_size_t yy_n_chars; int yy_n_chars;
/* Whether we "own" the buffer - i.e., we know we created it, /* Whether we "own" the buffer - i.e., we know we created it,
* and can realloc() it to grow it, and should free() it to * and can realloc() it to grow it, and should free() it to
...@@ -204,7 +205,7 @@ void yypop_buffer_state (yyscan_t yyscanner ); ...@@ -204,7 +205,7 @@ void yypop_buffer_state (yyscan_t yyscanner );
YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner ); YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size ,yyscan_t yyscanner );
YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ,yyscan_t yyscanner ); YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str ,yyscan_t yyscanner );
YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len ,yyscan_t yyscanner ); YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,int len ,yyscan_t yyscanner );
void *yyalloc (yy_size_t ,yyscan_t yyscanner ); void *yyalloc (yy_size_t ,yyscan_t yyscanner );
void *yyrealloc (void *,yy_size_t ,yyscan_t yyscanner ); void *yyrealloc (void *,yy_size_t ,yyscan_t yyscanner );
...@@ -260,7 +261,7 @@ FILE *yyget_out (yyscan_t yyscanner ); ...@@ -260,7 +261,7 @@ FILE *yyget_out (yyscan_t yyscanner );
void yyset_out (FILE * out_str ,yyscan_t yyscanner ); void yyset_out (FILE * out_str ,yyscan_t yyscanner );
yy_size_t yyget_leng (yyscan_t yyscanner ); int yyget_leng (yyscan_t yyscanner );
char *yyget_text (yyscan_t yyscanner ); char *yyget_text (yyscan_t yyscanner );
...@@ -333,9 +334,9 @@ extern int yylex \ ...@@ -333,9 +334,9 @@ extern int yylex \
#undef YY_DECL #undef YY_DECL
#endif #endif
#line 88 "lex_sql.l" #line 103 "lex_sql.l"
#line 340 "lex.yy.h" #line 341 "lex_sql.h"
#undef yyIN_HEADER #undef yyIN_HEADER
#endif /* yyHEADER_H */ #endif /* yyHEADER_H */
...@@ -2,9 +2,15 @@ ...@@ -2,9 +2,15 @@
#include<string.h> #include<string.h>
#include<stdio.h> #include<stdio.h>
/**
* flex 代码包含三个部分,使用 %% 分隔
* 第一个部分的代码是C代码,flex会原样复制到目标文件中
* 第二个部分是规则部分,使用正则表达式定义一系列规则
* 第三个部分还是C代码,flex 会复制此段代码
*/
struct ParserContext; struct ParserContext;
#include "yacc_sql.tab.h" #include "yacc_sql.hpp"
extern int atoi(); extern int atoi();
extern double atof(); extern double atof();
...@@ -21,6 +27,8 @@ extern double atof(); ...@@ -21,6 +27,8 @@ extern double atof();
%option noyywrap %option noyywrap
%option bison-bridge %option bison-bridge
%option reentrant %option reentrant
/* 不区分大小写 */
%option case-insensitive
WHITE_SAPCE [\ \t\b\f] WHITE_SAPCE [\ \t\b\f]
DIGIT [0-9]+ DIGIT [0-9]+
...@@ -30,61 +38,68 @@ DOT \. ...@@ -30,61 +38,68 @@ DOT \.
QUOTE [\'\"] QUOTE [\'\"]
%x STR %x STR
/* 规则匹配的优先级:*/
/* 1. 匹配的规则长的优先 */
/* 2. 写在最前面的优先 */
/* yylval 就可以认为是 yacc 中 %union 定义的结构体(union 结构) */
%% %%
{WHITE_SAPCE} // ignore whitespace {WHITE_SAPCE} // ignore whitespace
\n ; \n ;
[\-]?{DIGIT}+ yylval->number=atoi(yytext); RETURN_TOKEN(NUMBER); [\-]?{DIGIT}+ yylval->number=atoi(yytext); RETURN_TOKEN(NUMBER);
[\-]?{DIGIT}+{DOT}{DIGIT}+ yylval->floats=(float)(atof(yytext)); RETURN_TOKEN(FLOAT); [\-]?{DIGIT}+{DOT}{DIGIT}+ yylval->floats=(float)(atof(yytext)); RETURN_TOKEN(FLOAT);
";" RETURN_TOKEN(SEMICOLON); ";" RETURN_TOKEN(SEMICOLON);
{DOT} RETURN_TOKEN(DOT); {DOT} RETURN_TOKEN(DOT);
"*" RETURN_TOKEN(STAR); "*" RETURN_TOKEN(STAR);
[Ee][Xx][Ii][Tt] RETURN_TOKEN(EXIT); EXIT RETURN_TOKEN(EXIT);
[Hh][Ee][Ll][Pp] RETURN_TOKEN(HELP); HELP RETURN_TOKEN(HELP);
[Dd][Ee][Ss][Cc] RETURN_TOKEN(DESC); DESC RETURN_TOKEN(DESC);
[Cc][Rr][Ee][Aa][Tt][Ee] RETURN_TOKEN(CREATE); CREATE RETURN_TOKEN(CREATE);
[Dd][Rr][Oo][Pp] RETURN_TOKEN(DROP); DROP RETURN_TOKEN(DROP);
[Tt][Aa][Bb][Ll][Ee] RETURN_TOKEN(TABLE); TABLE RETURN_TOKEN(TABLE);
[Tt][Aa][Bb][Ll][Ee][Ss] RETURN_TOKEN(TABLES); TABLES RETURN_TOKEN(TABLES);
[Ii][Nn][Dd][Ee][Xx] RETURN_TOKEN(INDEX); INDEX RETURN_TOKEN(INDEX);
[Oo][Nn] RETURN_TOKEN(ON); ON RETURN_TOKEN(ON);
[Ss][Hh][Oo][Ww] RETURN_TOKEN(SHOW); SHOW RETURN_TOKEN(SHOW);
[Ss][Yy][Nn][Cc] RETURN_TOKEN(SYNC); SYNC RETURN_TOKEN(SYNC);
[Ss][Ee][Ll][Ee][Cc][Tt] RETURN_TOKEN(SELECT); SELECT RETURN_TOKEN(SELECT);
[Ff][Rr][Oo][Mm] RETURN_TOKEN(FROM); FROM RETURN_TOKEN(FROM);
[Ww][Hh][Ee][Rr][Ee] RETURN_TOKEN(WHERE); WHERE RETURN_TOKEN(WHERE);
[Aa][Nn][Dd] RETURN_TOKEN(AND); AND RETURN_TOKEN(AND);
[Ii][Nn][Ss][Ee][rR][tT] RETURN_TOKEN(INSERT); INSERT RETURN_TOKEN(INSERT);
[Ii][Nn][Tt][Oo] RETURN_TOKEN(INTO); INTO RETURN_TOKEN(INTO);
[Vv][Aa][Ll][Uu][Ee][Ss] RETURN_TOKEN(VALUES); VALUES RETURN_TOKEN(VALUES);
[Dd][Ee][Ll][Ee][Tt][Ee] RETURN_TOKEN(DELETE); DELETE RETURN_TOKEN(DELETE);
[Uu][Pp][Dd][Aa][Tt][Ee] RETURN_TOKEN(UPDATE); UPDATE RETURN_TOKEN(UPDATE);
[Ss][Ee][Tt] RETURN_TOKEN(SET); SET RETURN_TOKEN(SET);
[Bb][Ee][Gg][Ii][Nn] RETURN_TOKEN(TRX_BEGIN); BEGIN RETURN_TOKEN(TRX_BEGIN);
[Cc][Oo][Mm][Mm][Ii][Tt] RETURN_TOKEN(TRX_COMMIT); COMMIT RETURN_TOKEN(TRX_COMMIT);
[Rr][Oo][Ll][Ll][Bb][Aa][Cc][Kk] RETURN_TOKEN(TRX_ROLLBACK); ROLLBACK RETURN_TOKEN(TRX_ROLLBACK);
[Ii][Nn][Tt] RETURN_TOKEN(INT_T); INT RETURN_TOKEN(INT_T);
[Cc][Hh][Aa][Rr] RETURN_TOKEN(STRING_T); CHAR RETURN_TOKEN(STRING_T);
[Ff][Ll][Oo][Aa][Tt] RETURN_TOKEN(FLOAT_T); FLOAT RETURN_TOKEN(FLOAT_T);
[Ll][Oo][Aa][Dd] RETURN_TOKEN(LOAD); LOAD RETURN_TOKEN(LOAD);
[Dd][Aa][Tt][Aa] RETURN_TOKEN(DATA); DATA RETURN_TOKEN(DATA);
[Ii][Nn][Ff][Ii][Ll][Ee] RETURN_TOKEN(INFILE); INFILE RETURN_TOKEN(INFILE);
{ID} yylval->string=strdup(yytext); RETURN_TOKEN(ID); {ID} yylval->string=strdup(yytext); RETURN_TOKEN(ID);
"(" RETURN_TOKEN(LBRACE); "(" RETURN_TOKEN(LBRACE);
")" RETURN_TOKEN(RBRACE); ")" RETURN_TOKEN(RBRACE);
"," RETURN_TOKEN(COMMA); "," RETURN_TOKEN(COMMA);
"=" RETURN_TOKEN(EQ); "=" RETURN_TOKEN(EQ);
"<=" RETURN_TOKEN(LE); "<=" RETURN_TOKEN(LE);
"<>" RETURN_TOKEN(NE); "<>" RETURN_TOKEN(NE);
"<" RETURN_TOKEN(LT); "!=" RETURN_TOKEN(NE);
">=" RETURN_TOKEN(GE); "<" RETURN_TOKEN(LT);
">" RETURN_TOKEN(GT); ">=" RETURN_TOKEN(GE);
{QUOTE}[\40\42\47A-Za-z0-9_/\.\-]*{QUOTE} yylval->string=strdup(yytext); RETURN_TOKEN(SSS); ">" RETURN_TOKEN(GT);
\"[^"]*\" yylval->string=strdup(yytext); RETURN_TOKEN(SSS);
'[^']*\' yylval->string = strdup(yytext); RETURN_TOKEN(SSS);
. printf("Unknown character [%c]\n",yytext[0]); return yytext[0]; . printf("Unknown character [%c]\n",yytext[0]); return yytext[0];
%% %%
void scan_string(const char *str, yyscan_t scanner) { void scan_string(const char *str, yyscan_t scanner) {
......
...@@ -19,9 +19,6 @@ See the Mulan PSL v2 for more details. */ ...@@ -19,9 +19,6 @@ See the Mulan PSL v2 for more details. */
RC parse(char *st, Query *sqln); RC parse(char *st, Query *sqln);
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void relation_attr_init(RelAttr *relation_attr, const char *relation_name, const char *attribute_name) void relation_attr_init(RelAttr *relation_attr, const char *relation_name, const char *attribute_name)
{ {
if (relation_name != nullptr) { if (relation_name != nullptr) {
...@@ -388,13 +385,10 @@ void query_destroy(Query *query) ...@@ -388,13 +385,10 @@ void query_destroy(Query *query)
query_reset(query); query_reset(query);
free(query); free(query);
} }
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
extern "C" int sql_parse(const char *st, Query *sqls); int sql_parse(const char *st, Query *sqls);
RC parse(const char *st, Query *sqln) RC parse(const char *st, Query *sqln)
{ {
...@@ -404,4 +398,4 @@ RC parse(const char *st, Query *sqln) ...@@ -404,4 +398,4 @@ RC parse(const char *st, Query *sqln)
return SQL_SYNTAX; return SQL_SYNTAX;
else else
return SUCCESS; return SUCCESS;
} }
\ No newline at end of file
...@@ -180,10 +180,6 @@ typedef struct Query { ...@@ -180,10 +180,6 @@ typedef struct Query {
union Queries sstr; union Queries sstr;
} Query; } Query;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void relation_attr_init(RelAttr *relation_attr, const char *relation_name, const char *attribute_name); void relation_attr_init(RelAttr *relation_attr, const char *relation_name, const char *attribute_name);
void relation_attr_destroy(RelAttr *relation_attr); void relation_attr_destroy(RelAttr *relation_attr);
...@@ -241,8 +237,4 @@ Query *query_create(); // create and init ...@@ -241,8 +237,4 @@ Query *query_create(); // create and init
void query_reset(Query *query); void query_reset(Query *query);
void query_destroy(Query *query); // reset and delete void query_destroy(Query *query); // reset and delete
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __OBSERVER_SQL_PARSER_PARSE_DEFS_H__ #endif // __OBSERVER_SQL_PARSER_PARSE_DEFS_H__
...@@ -35,11 +35,11 @@ ...@@ -35,11 +35,11 @@
especially those whose name start with YY_ or yy_. They are especially those whose name start with YY_ or yy_. They are
private implementation details that can be changed or removed. */ private implementation details that can be changed or removed. */
#ifndef YY_YY_YACC_SQL_TAB_H_INCLUDED #ifndef YY_YY_YACC_SQL_HPP_INCLUDED
#define YY_YY_YACC_SQL_TAB_H_INCLUDED # define YY_YY_YACC_SQL_HPP_INCLUDED
/* Debug traces. */ /* Debug traces. */
#ifndef YYDEBUG #ifndef YYDEBUG
#define YYDEBUG 0 # define YYDEBUG 0
#endif #endif
#if YYDEBUG #if YYDEBUG
extern int yydebug; extern int yydebug;
...@@ -47,67 +47,69 @@ extern int yydebug; ...@@ -47,67 +47,69 @@ extern int yydebug;
/* Token kinds. */ /* Token kinds. */
#ifndef YYTOKENTYPE #ifndef YYTOKENTYPE
#define YYTOKENTYPE # define YYTOKENTYPE
enum yytokentype { enum yytokentype
YYEMPTY = -2, {
YYEOF = 0, /* "end of file" */ YYEMPTY = -2,
YYerror = 256, /* error */ YYEOF = 0, /* "end of file" */
YYUNDEF = 257, /* "invalid token" */ YYerror = 256, /* error */
SEMICOLON = 258, /* SEMICOLON */ YYUNDEF = 257, /* "invalid token" */
CREATE = 259, /* CREATE */ SEMICOLON = 258, /* SEMICOLON */
DROP = 260, /* DROP */ CREATE = 259, /* CREATE */
TABLE = 261, /* TABLE */ DROP = 260, /* DROP */
TABLES = 262, /* TABLES */ TABLE = 261, /* TABLE */
INDEX = 263, /* INDEX */ TABLES = 262, /* TABLES */
SELECT = 264, /* SELECT */ INDEX = 263, /* INDEX */
DESC = 265, /* DESC */ SELECT = 264, /* SELECT */
SHOW = 266, /* SHOW */ DESC = 265, /* DESC */
SYNC = 267, /* SYNC */ SHOW = 266, /* SHOW */
INSERT = 268, /* INSERT */ SYNC = 267, /* SYNC */
DELETE = 269, /* DELETE */ INSERT = 268, /* INSERT */
UPDATE = 270, /* UPDATE */ DELETE = 269, /* DELETE */
LBRACE = 271, /* LBRACE */ UPDATE = 270, /* UPDATE */
RBRACE = 272, /* RBRACE */ LBRACE = 271, /* LBRACE */
COMMA = 273, /* COMMA */ RBRACE = 272, /* RBRACE */
TRX_BEGIN = 274, /* TRX_BEGIN */ COMMA = 273, /* COMMA */
TRX_COMMIT = 275, /* TRX_COMMIT */ TRX_BEGIN = 274, /* TRX_BEGIN */
TRX_ROLLBACK = 276, /* TRX_ROLLBACK */ TRX_COMMIT = 275, /* TRX_COMMIT */
INT_T = 277, /* INT_T */ TRX_ROLLBACK = 276, /* TRX_ROLLBACK */
STRING_T = 278, /* STRING_T */ INT_T = 277, /* INT_T */
FLOAT_T = 279, /* FLOAT_T */ STRING_T = 278, /* STRING_T */
HELP = 280, /* HELP */ FLOAT_T = 279, /* FLOAT_T */
EXIT = 281, /* EXIT */ HELP = 280, /* HELP */
DOT = 282, /* DOT */ EXIT = 281, /* EXIT */
INTO = 283, /* INTO */ DOT = 282, /* DOT */
VALUES = 284, /* VALUES */ INTO = 283, /* INTO */
FROM = 285, /* FROM */ VALUES = 284, /* VALUES */
WHERE = 286, /* WHERE */ FROM = 285, /* FROM */
AND = 287, /* AND */ WHERE = 286, /* WHERE */
SET = 288, /* SET */ AND = 287, /* AND */
ON = 289, /* ON */ SET = 288, /* SET */
LOAD = 290, /* LOAD */ ON = 289, /* ON */
DATA = 291, /* DATA */ LOAD = 290, /* LOAD */
INFILE = 292, /* INFILE */ DATA = 291, /* DATA */
EQ = 293, /* EQ */ INFILE = 292, /* INFILE */
LT = 294, /* LT */ EQ = 293, /* EQ */
GT = 295, /* GT */ LT = 294, /* LT */
LE = 296, /* LE */ GT = 295, /* GT */
GE = 297, /* GE */ LE = 296, /* LE */
NE = 298, /* NE */ GE = 297, /* GE */
NUMBER = 299, /* NUMBER */ NE = 298, /* NE */
FLOAT = 300, /* FLOAT */ NUMBER = 299, /* NUMBER */
ID = 301, /* ID */ FLOAT = 300, /* FLOAT */
PATH = 302, /* PATH */ ID = 301, /* ID */
SSS = 303, /* SSS */ PATH = 302, /* PATH */
STAR = 304, /* STAR */ SSS = 303, /* SSS */
STRING_V = 305 /* STRING_V */ STAR = 304, /* STAR */
}; STRING_V = 305 /* STRING_V */
typedef enum yytokentype yytoken_kind_t; };
typedef enum yytokentype yytoken_kind_t;
#endif #endif
/* Value type. */ /* Value type. */
#if !defined YYSTYPE && !defined YYSTYPE_IS_DECLARED #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE { union YYSTYPE
{
#line 106 "yacc_sql.y" #line 106 "yacc_sql.y"
struct _Attr *attr; struct _Attr *attr;
...@@ -116,15 +118,18 @@ union YYSTYPE { ...@@ -116,15 +118,18 @@ union YYSTYPE {
char *string; char *string;
int number; int number;
float floats; float floats;
char *position; char *position;
#line 124 "yacc_sql.hpp"
#line 124 "yacc_sql.tab.h"
}; };
typedef union YYSTYPE YYSTYPE; typedef union YYSTYPE YYSTYPE;
#define YYSTYPE_IS_TRIVIAL 1 # define YYSTYPE_IS_TRIVIAL 1
#define YYSTYPE_IS_DECLARED 1 # define YYSTYPE_IS_DECLARED 1
#endif #endif
int yyparse(void *scanner);
#endif /* !YY_YY_YACC_SQL_TAB_H_INCLUDED */
int yyparse (void *scanner);
#endif /* !YY_YY_YACC_SQL_HPP_INCLUDED */
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
%{ %{
#include "sql/parser/parse_defs.h" #include "sql/parser/parse_defs.h"
#include "sql/parser/yacc_sql.tab.h" #include "sql/parser/yacc_sql.hpp"
#include "sql/parser/lex.yy.h" #include "sql/parser/lex_sql.h"
// #include "common/log/log.h" // 包含C++中的头文件 #include "common/log/log.h"
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
...@@ -25,7 +25,7 @@ typedef struct ParserContext { ...@@ -25,7 +25,7 @@ typedef struct ParserContext {
//获取子串 //获取子串
char *substr(const char *s,int n1,int n2)/*从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址*/ char *substr(const char *s,int n1,int n2)/*从s中提取下标为n1~n2的字符组成一个新字符串,然后返回这个新串的首地址*/
{ {
char *sp = malloc(sizeof(char) * (n2 - n1 + 2)); char *sp = (char *)malloc(sizeof(char) * (n2 - n1 + 2));
int i, j = 0; int i, j = 0;
for (i = n1; i <= n2; i++) { for (i = n1; i <= n2; i++) {
sp[j++] = s[i]; sp[j++] = s[i];
...@@ -226,7 +226,6 @@ create_table: /*create table 语句的语法解析树*/ ...@@ -226,7 +226,6 @@ create_table: /*create table 语句的语法解析树*/
CREATE TABLE ID LBRACE attr_def attr_def_list RBRACE SEMICOLON CREATE TABLE ID LBRACE attr_def attr_def_list RBRACE SEMICOLON
{ {
CONTEXT->ssql->flag=SCF_CREATE_TABLE;//"create_table"; CONTEXT->ssql->flag=SCF_CREATE_TABLE;//"create_table";
// CONTEXT->ssql->sstr.create_table.attribute_count = CONTEXT->value_length;
create_table_init_name(&CONTEXT->ssql->sstr.create_table, $3); create_table_init_name(&CONTEXT->ssql->sstr.create_table, $3);
//临时变量清零 //临时变量清零
CONTEXT->value_length = 0; CONTEXT->value_length = 0;
...@@ -241,23 +240,15 @@ attr_def: ...@@ -241,23 +240,15 @@ attr_def:
ID_get type LBRACE number RBRACE ID_get type LBRACE number RBRACE
{ {
AttrInfo attribute; AttrInfo attribute;
attr_info_init(&attribute, CONTEXT->id, $2, $4); attr_info_init(&attribute, CONTEXT->id, (AttrType)$2, $4);
create_table_append_attribute(&CONTEXT->ssql->sstr.create_table, &attribute); create_table_append_attribute(&CONTEXT->ssql->sstr.create_table, &attribute);
// CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].name =(char*)malloc(sizeof(char));
// strcpy(CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].name, CONTEXT->id);
// CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].type = $2;
// CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].length = $4;
CONTEXT->value_length++; CONTEXT->value_length++;
} }
|ID_get type |ID_get type
{ {
AttrInfo attribute; AttrInfo attribute;
attr_info_init(&attribute, CONTEXT->id, $2, 4); attr_info_init(&attribute, CONTEXT->id, (AttrType)$2, 4);
create_table_append_attribute(&CONTEXT->ssql->sstr.create_table, &attribute); create_table_append_attribute(&CONTEXT->ssql->sstr.create_table, &attribute);
// CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].name=(char*)malloc(sizeof(char));
// strcpy(CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].name, CONTEXT->id);
// CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].type=$2;
// CONTEXT->ssql->sstr.create_table.attributes[CONTEXT->value_length].length=4; // default attribute length
CONTEXT->value_length++; CONTEXT->value_length++;
} }
; ;
...@@ -281,14 +272,7 @@ ID_get: ...@@ -281,14 +272,7 @@ ID_get:
insert: /*insert 语句的语法解析树*/ insert: /*insert 语句的语法解析树*/
INSERT INTO ID VALUES LBRACE value value_list RBRACE SEMICOLON INSERT INTO ID VALUES LBRACE value value_list RBRACE SEMICOLON
{ {
// CONTEXT->values[CONTEXT->value_length++] = *$6;
CONTEXT->ssql->flag=SCF_INSERT;//"insert"; CONTEXT->ssql->flag=SCF_INSERT;//"insert";
// CONTEXT->ssql->sstr.insertion.relation_name = $3;
// CONTEXT->ssql->sstr.insertion.value_num = CONTEXT->value_length;
// for(i = 0; i < CONTEXT->value_length; i++){
// CONTEXT->ssql->sstr.insertion.values[i] = CONTEXT->values[i];
// }
inserts_init(&CONTEXT->ssql->sstr.insertion, $3, CONTEXT->values, CONTEXT->value_length); inserts_init(&CONTEXT->ssql->sstr.insertion, $3, CONTEXT->values, CONTEXT->value_length);
//临时变量清零 //临时变量清零
...@@ -330,20 +314,18 @@ update: /* update 语句的语法解析树*/ ...@@ -330,20 +314,18 @@ update: /* update 语句的语法解析树*/
CONTEXT->ssql->flag = SCF_UPDATE;//"update"; CONTEXT->ssql->flag = SCF_UPDATE;//"update";
Value *value = &CONTEXT->values[0]; Value *value = &CONTEXT->values[0];
updates_init(&CONTEXT->ssql->sstr.update, $2, $4, value, updates_init(&CONTEXT->ssql->sstr.update, $2, $4, value,
CONTEXT->conditions, CONTEXT->condition_length); CONTEXT->conditions, CONTEXT->condition_length);
CONTEXT->condition_length = 0; CONTEXT->condition_length = 0;
} }
; ;
select: /* select 语句的语法解析树*/ select: /* select 语句的语法解析树*/
SELECT select_attr FROM ID rel_list where SEMICOLON SELECT select_attr FROM ID rel_list where SEMICOLON
{ {
// CONTEXT->ssql->sstr.selection.relations[CONTEXT->from_length++]=$4;
selects_append_relation(&CONTEXT->ssql->sstr.selection, $4); selects_append_relation(&CONTEXT->ssql->sstr.selection, $4);
selects_append_conditions(&CONTEXT->ssql->sstr.selection, CONTEXT->conditions, CONTEXT->condition_length); selects_append_conditions(&CONTEXT->ssql->sstr.selection, CONTEXT->conditions, CONTEXT->condition_length);
CONTEXT->ssql->flag=SCF_SELECT;//"select"; CONTEXT->ssql->flag=SCF_SELECT;//"select";
// CONTEXT->ssql->sstr.selection.attr_num = CONTEXT->select_length;
//临时变量清零 //临时变量清零
CONTEXT->condition_length=0; CONTEXT->condition_length=0;
...@@ -376,15 +358,11 @@ attr_list: ...@@ -376,15 +358,11 @@ attr_list:
RelAttr attr; RelAttr attr;
relation_attr_init(&attr, NULL, $2); relation_attr_init(&attr, NULL, $2);
selects_append_attribute(&CONTEXT->ssql->sstr.selection, &attr); selects_append_attribute(&CONTEXT->ssql->sstr.selection, &attr);
// CONTEXT->ssql->sstr.selection.attributes[CONTEXT->select_length].relation_name = NULL;
// CONTEXT->ssql->sstr.selection.attributes[CONTEXT->select_length++].attribute_name=$2;
} }
| COMMA ID DOT ID attr_list { | COMMA ID DOT ID attr_list {
RelAttr attr; RelAttr attr;
relation_attr_init(&attr, $2, $4); relation_attr_init(&attr, $2, $4);
selects_append_attribute(&CONTEXT->ssql->sstr.selection, &attr); selects_append_attribute(&CONTEXT->ssql->sstr.selection, &attr);
// CONTEXT->ssql->sstr.selection.attributes[CONTEXT->select_length].attribute_name=$4;
// CONTEXT->ssql->sstr.selection.attributes[CONTEXT->select_length++].relation_name=$2;
} }
; ;
...@@ -417,16 +395,6 @@ condition: ...@@ -417,16 +395,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 0, NULL, right_value); condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 0, NULL, right_value);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$ = ( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 1;
// $$->left_attr.relation_name = NULL;
// $$->left_attr.attribute_name= $1;
// $$->comp = CONTEXT->comp;
// $$->right_is_attr = 0;
// $$->right_attr.relation_name = NULL;
// $$->right_attr.attribute_name = NULL;
// $$->right_value = *$3;
} }
|value comOp value |value comOp value
{ {
...@@ -436,17 +404,6 @@ condition: ...@@ -436,17 +404,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 0, NULL, left_value, 0, NULL, right_value); condition_init(&condition, CONTEXT->comp, 0, NULL, left_value, 0, NULL, right_value);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$ = ( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 0;
// $$->left_attr.relation_name=NULL;
// $$->left_attr.attribute_name=NULL;
// $$->left_value = *$1;
// $$->comp = CONTEXT->comp;
// $$->right_is_attr = 0;
// $$->right_attr.relation_name = NULL;
// $$->right_attr.attribute_name = NULL;
// $$->right_value = *$3;
} }
|ID comOp ID |ID comOp ID
{ {
...@@ -458,15 +415,6 @@ condition: ...@@ -458,15 +415,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 1, &right_attr, NULL); condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 1, &right_attr, NULL);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$=( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 1;
// $$->left_attr.relation_name=NULL;
// $$->left_attr.attribute_name=$1;
// $$->comp = CONTEXT->comp;
// $$->right_is_attr = 1;
// $$->right_attr.relation_name=NULL;
// $$->right_attr.attribute_name=$3;
} }
|value comOp ID |value comOp ID
{ {
...@@ -477,18 +425,6 @@ condition: ...@@ -477,18 +425,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 0, NULL, left_value, 1, &right_attr, NULL); condition_init(&condition, CONTEXT->comp, 0, NULL, left_value, 1, &right_attr, NULL);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$=( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 0;
// $$->left_attr.relation_name=NULL;
// $$->left_attr.attribute_name=NULL;
// $$->left_value = *$1;
// $$->comp=CONTEXT->comp;
// $$->right_is_attr = 1;
// $$->right_attr.relation_name=NULL;
// $$->right_attr.attribute_name=$3;
} }
|ID DOT ID comOp value |ID DOT ID comOp value
{ {
...@@ -499,17 +435,6 @@ condition: ...@@ -499,17 +435,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 0, NULL, right_value); condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 0, NULL, right_value);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$=( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 1;
// $$->left_attr.relation_name=$1;
// $$->left_attr.attribute_name=$3;
// $$->comp=CONTEXT->comp;
// $$->right_is_attr = 0; //属性值
// $$->right_attr.relation_name=NULL;
// $$->right_attr.attribute_name=NULL;
// $$->right_value =*$5;
} }
|value comOp ID DOT ID |value comOp ID DOT ID
{ {
...@@ -521,16 +446,6 @@ condition: ...@@ -521,16 +446,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 0, NULL, left_value, 1, &right_attr, NULL); condition_init(&condition, CONTEXT->comp, 0, NULL, left_value, 1, &right_attr, NULL);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$=( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 0;//属性值
// $$->left_attr.relation_name=NULL;
// $$->left_attr.attribute_name=NULL;
// $$->left_value = *$1;
// $$->comp =CONTEXT->comp;
// $$->right_is_attr = 1;//属性
// $$->right_attr.relation_name = $3;
// $$->right_attr.attribute_name = $5;
} }
|ID DOT ID comOp ID DOT ID |ID DOT ID comOp ID DOT ID
{ {
...@@ -542,14 +457,6 @@ condition: ...@@ -542,14 +457,6 @@ condition:
Condition condition; Condition condition;
condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 1, &right_attr, NULL); condition_init(&condition, CONTEXT->comp, 1, &left_attr, NULL, 1, &right_attr, NULL);
CONTEXT->conditions[CONTEXT->condition_length++] = condition; CONTEXT->conditions[CONTEXT->condition_length++] = condition;
// $$=( Condition *)malloc(sizeof( Condition));
// $$->left_is_attr = 1; //属性
// $$->left_attr.relation_name=$1;
// $$->left_attr.attribute_name=$3;
// $$->comp =CONTEXT->comp;
// $$->right_is_attr = 1; //属性
// $$->right_attr.relation_name=$5;
// $$->right_attr.attribute_name=$7;
} }
; ;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册