From 431830d5de03d7b1ecc9faff13bfe7c700f492f4 Mon Sep 17 00:00:00 2001 From: AUA Date: Sun, 17 Nov 2024 12:09:40 +0000 Subject: [PATCH 1/2] =?UTF-8?q?commit-message:=20=E9=87=8A=E6=94=BE?= =?UTF-8?q?=E6=9C=AA=E8=A7=A3=E6=9E=90=E6=88=90=E8=AF=AD=E6=B3=95=E6=A0=91?= =?UTF-8?q?=E7=9A=84id=20#479?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/observer/sql/parser/lex_sql.l | 14 +++++++-- src/observer/sql/parser/yacc_sql.y | 48 ++++++++++++++++-------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/observer/sql/parser/lex_sql.l b/src/observer/sql/parser/lex_sql.l index 2e8225b97..ae49590dc 100644 --- a/src/observer/sql/parser/lex_sql.l +++ b/src/observer/sql/parser/lex_sql.l @@ -27,6 +27,7 @@ while (0); %{ #include #include +#include /** * flex 代码包含三个部分,使用 %% 分隔 @@ -46,6 +47,7 @@ while (0); extern int atoi(); extern double atof(); +std::vector allocated_strings; #define RETURN_TOKEN(token) LOG_DEBUG("%s", #token);return token %} @@ -119,7 +121,7 @@ GROUP RETURN_TOKEN(GROUP); BY RETURN_TOKEN(BY); STORAGE RETURN_TOKEN(STORAGE); FORMAT RETURN_TOKEN(FORMAT); -{ID} yylval->string=strdup(yytext); RETURN_TOKEN(ID); +{ID} yylval->string=strdup(yytext); allocated_strings.push_back(yylval->string); RETURN_TOKEN(ID); "(" RETURN_TOKEN(LBRACE); ")" RETURN_TOKEN(RBRACE); @@ -136,8 +138,8 @@ FORMAT RETURN_TOKEN(FORMAT); "-" | "*" | "/" { return yytext[0]; } -\"[^"]*\" yylval->string = strdup(yytext); RETURN_TOKEN(SSS); -'[^']*\' yylval->string = strdup(yytext); RETURN_TOKEN(SSS); +\"[^"]*\" yylval->string = strdup(yytext); allocated_strings.push_back(yylval->string); RETURN_TOKEN(SSS); +'[^']*\' yylval->string = strdup(yytext); allocated_strings.push_back(yylval->string); RETURN_TOKEN(SSS); . LOG_DEBUG("Unknown character [%c]",yytext[0]); return yytext[0]; %% @@ -145,3 +147,9 @@ FORMAT RETURN_TOKEN(FORMAT); void scan_string(const char *str, yyscan_t scanner) { yy_switch_to_buffer(yy_scan_string(str, scanner), scanner); } +void free_allocated_strings() { + for (char *ptr : allocated_strings) { + free(ptr); + } + allocated_strings.clear(); +} diff --git a/src/observer/sql/parser/yacc_sql.y b/src/observer/sql/parser/yacc_sql.y index 9e392b620..e071e68f0 100644 --- a/src/observer/sql/parser/yacc_sql.y +++ b/src/observer/sql/parser/yacc_sql.y @@ -255,7 +255,7 @@ drop_table_stmt: /*drop table 语句的语法解析树*/ DROP TABLE ID { $$ = new ParsedSqlNode(SCF_DROP_TABLE); $$->drop_table.relation_name = $3; - free($3); + //free($3); }; show_tables_stmt: @@ -268,7 +268,7 @@ desc_table_stmt: DESC ID { $$ = new ParsedSqlNode(SCF_DESC_TABLE); $$->desc_table.relation_name = $2; - free($2); + //free($2); } ; @@ -280,9 +280,9 @@ create_index_stmt: /*create index 语句的语法解析树*/ create_index.index_name = $3; create_index.relation_name = $5; create_index.attribute_name = $7; - free($3); - free($5); - free($7); + //free($3); + //free($5); + //free($7); } ; @@ -292,8 +292,8 @@ drop_index_stmt: /*drop index 语句的语法解析树*/ $$ = new ParsedSqlNode(SCF_DROP_INDEX); $$->drop_index.index_name = $3; $$->drop_index.relation_name = $5; - free($3); - free($5); + //free($3); + //free($5); } ; create_table_stmt: /*create table 语句的语法解析树*/ @@ -302,7 +302,7 @@ create_table_stmt: /*create table 语句的语法解析树*/ $$ = new ParsedSqlNode(SCF_CREATE_TABLE); CreateTableSqlNode &create_table = $$->create_table; create_table.relation_name = $3; - free($3); + //free($3); std::vector *src_attrs = $6; @@ -315,7 +315,7 @@ create_table_stmt: /*create table 语句的语法解析树*/ delete $5; if ($8 != nullptr) { create_table.storage_format = $8; - free($8); + //free($8); } } ; @@ -343,7 +343,7 @@ attr_def: $$->type = (AttrType)$2; $$->name = $1; $$->length = $4; - free($1); + //free($1); } | ID type { @@ -351,7 +351,7 @@ attr_def: $$->type = (AttrType)$2; $$->name = $1; $$->length = 4; - free($1); + //free($1); } ; number: @@ -375,7 +375,7 @@ insert_stmt: /*insert 语句的语法解析树*/ $$->insertion.values.emplace_back(*$6); std::reverse($$->insertion.values.begin(), $$->insertion.values.end()); delete $6; - free($3); + //free($3); } ; @@ -407,7 +407,7 @@ value: char *tmp = common::substr($1,1,strlen($1)-2); $$ = new Value(tmp); free(tmp); - free($1); + //free($1); } ; storage_format: @@ -430,7 +430,7 @@ delete_stmt: /* delete 语句的语法解析树*/ $$->deletion.conditions.swap(*$4); delete $4; } - free($3); + //free($3); } ; update_stmt: /* update 语句的语法解析树*/ @@ -444,8 +444,8 @@ update_stmt: /* update 语句的语法解析树*/ $$->update.conditions.swap(*$7); delete $7; } - free($2); - free($4); + //free($2); + //free($4); } ; select_stmt: /* select 语句的语法解析树*/ @@ -539,14 +539,14 @@ rel_attr: ID { $$ = new RelAttrSqlNode; $$->attribute_name = $1; - free($1); + //free($1); } | ID DOT ID { $$ = new RelAttrSqlNode; $$->relation_name = $1; $$->attribute_name = $3; - free($1); - free($3); + //free($1); + //free($3); } ; @@ -559,7 +559,7 @@ rel_list: relation { $$ = new std::vector(); $$->push_back($1); - free($1); + //free($1); } | relation COMMA rel_list { if ($3 != nullptr) { @@ -569,7 +569,7 @@ rel_list: } $$->insert($$->begin(), $1); - free($1); + //free($1); } ; @@ -673,7 +673,7 @@ load_data_stmt: $$ = new ParsedSqlNode(SCF_LOAD_DATA); $$->load_data.relation_name = $7; $$->load_data.file_name = tmp_file_name; - free($7); + //free($7); free(tmp_file_name); } ; @@ -692,7 +692,7 @@ set_variable_stmt: $$ = new ParsedSqlNode(SCF_SET_VARIABLE); $$->set_variable.name = $2; $$->set_variable.value = *$4; - free($2); + //free($2); delete $4; } ; @@ -703,6 +703,7 @@ opt_semicolon: /*empty*/ %% //_____________________________________________________________________ extern void scan_string(const char *str, yyscan_t scanner); +extern void free_allocated_strings(); int sql_parse(const char *s, ParsedSqlResult *sql_result) { yyscan_t scanner; @@ -710,5 +711,6 @@ int sql_parse(const char *s, ParsedSqlResult *sql_result) { scan_string(s, scanner); int result = yyparse(s, sql_result, scanner); yylex_destroy(scanner); + free_allocated_strings(); return result; } From d6d6a1d9893464affc06196135bebd715a1f8de4 Mon Sep 17 00:00:00 2001 From: AUA Date: Fri, 22 Nov 2024 13:26:03 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=E9=87=8A=E6=94=BE=E6=9C=AA=E8=A7=A3?= =?UTF-8?q?=E6=9E=90=E6=88=90=E8=AF=AD=E6=B3=95=E6=A0=91=E7=9A=84id=20#479?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/observer/sql/parser/lex_sql.l | 13 +++-------- src/observer/sql/parser/yacc_sql.y | 35 ++++++++---------------------- 2 files changed, 12 insertions(+), 36 deletions(-) diff --git a/src/observer/sql/parser/lex_sql.l b/src/observer/sql/parser/lex_sql.l index ae49590dc..a7859c407 100644 --- a/src/observer/sql/parser/lex_sql.l +++ b/src/observer/sql/parser/lex_sql.l @@ -47,7 +47,6 @@ while (0); extern int atoi(); extern double atof(); -std::vector allocated_strings; #define RETURN_TOKEN(token) LOG_DEBUG("%s", #token);return token %} @@ -121,7 +120,7 @@ GROUP RETURN_TOKEN(GROUP); BY RETURN_TOKEN(BY); STORAGE RETURN_TOKEN(STORAGE); FORMAT RETURN_TOKEN(FORMAT); -{ID} yylval->string=strdup(yytext); allocated_strings.push_back(yylval->string); RETURN_TOKEN(ID); +{ID} yylval->string=strdup(yytext); static_cast*>(yyextra)->push_back(yylval->string); RETURN_TOKEN(ID); "(" RETURN_TOKEN(LBRACE); ")" RETURN_TOKEN(RBRACE); @@ -138,8 +137,8 @@ FORMAT RETURN_TOKEN(FORMAT); "-" | "*" | "/" { return yytext[0]; } -\"[^"]*\" yylval->string = strdup(yytext); allocated_strings.push_back(yylval->string); RETURN_TOKEN(SSS); -'[^']*\' yylval->string = strdup(yytext); allocated_strings.push_back(yylval->string); RETURN_TOKEN(SSS); +\"[^"]*\" yylval->string = strdup(yytext); static_cast*>(yyextra)->push_back(yylval->string); RETURN_TOKEN(SSS); +'[^']*\' yylval->string = strdup(yytext); static_cast*>(yyextra)->push_back(yylval->string); RETURN_TOKEN(SSS); . LOG_DEBUG("Unknown character [%c]",yytext[0]); return yytext[0]; %% @@ -147,9 +146,3 @@ FORMAT RETURN_TOKEN(FORMAT); void scan_string(const char *str, yyscan_t scanner) { yy_switch_to_buffer(yy_scan_string(str, scanner), scanner); } -void free_allocated_strings() { - for (char *ptr : allocated_strings) { - free(ptr); - } - allocated_strings.clear(); -} diff --git a/src/observer/sql/parser/yacc_sql.y b/src/observer/sql/parser/yacc_sql.y index e071e68f0..235576053 100644 --- a/src/observer/sql/parser/yacc_sql.y +++ b/src/observer/sql/parser/yacc_sql.y @@ -255,7 +255,6 @@ drop_table_stmt: /*drop table 语句的语法解析树*/ DROP TABLE ID { $$ = new ParsedSqlNode(SCF_DROP_TABLE); $$->drop_table.relation_name = $3; - //free($3); }; show_tables_stmt: @@ -268,7 +267,6 @@ desc_table_stmt: DESC ID { $$ = new ParsedSqlNode(SCF_DESC_TABLE); $$->desc_table.relation_name = $2; - //free($2); } ; @@ -280,9 +278,6 @@ create_index_stmt: /*create index 语句的语法解析树*/ create_index.index_name = $3; create_index.relation_name = $5; create_index.attribute_name = $7; - //free($3); - //free($5); - //free($7); } ; @@ -292,8 +287,6 @@ drop_index_stmt: /*drop index 语句的语法解析树*/ $$ = new ParsedSqlNode(SCF_DROP_INDEX); $$->drop_index.index_name = $3; $$->drop_index.relation_name = $5; - //free($3); - //free($5); } ; create_table_stmt: /*create table 语句的语法解析树*/ @@ -315,7 +308,6 @@ create_table_stmt: /*create table 语句的语法解析树*/ delete $5; if ($8 != nullptr) { create_table.storage_format = $8; - //free($8); } } ; @@ -343,7 +335,6 @@ attr_def: $$->type = (AttrType)$2; $$->name = $1; $$->length = $4; - //free($1); } | ID type { @@ -351,7 +342,6 @@ attr_def: $$->type = (AttrType)$2; $$->name = $1; $$->length = 4; - //free($1); } ; number: @@ -375,7 +365,6 @@ insert_stmt: /*insert 语句的语法解析树*/ $$->insertion.values.emplace_back(*$6); std::reverse($$->insertion.values.begin(), $$->insertion.values.end()); delete $6; - //free($3); } ; @@ -407,7 +396,6 @@ value: char *tmp = common::substr($1,1,strlen($1)-2); $$ = new Value(tmp); free(tmp); - //free($1); } ; storage_format: @@ -430,7 +418,6 @@ delete_stmt: /* delete 语句的语法解析树*/ $$->deletion.conditions.swap(*$4); delete $4; } - //free($3); } ; update_stmt: /* update 语句的语法解析树*/ @@ -444,8 +431,6 @@ update_stmt: /* update 语句的语法解析树*/ $$->update.conditions.swap(*$7); delete $7; } - //free($2); - //free($4); } ; select_stmt: /* select 语句的语法解析树*/ @@ -539,14 +524,11 @@ rel_attr: ID { $$ = new RelAttrSqlNode; $$->attribute_name = $1; - //free($1); } | ID DOT ID { $$ = new RelAttrSqlNode; $$->relation_name = $1; $$->attribute_name = $3; - //free($1); - //free($3); } ; @@ -559,7 +541,6 @@ rel_list: relation { $$ = new std::vector(); $$->push_back($1); - //free($1); } | relation COMMA rel_list { if ($3 != nullptr) { @@ -569,7 +550,6 @@ rel_list: } $$->insert($$->begin(), $1); - //free($1); } ; @@ -673,7 +653,6 @@ load_data_stmt: $$ = new ParsedSqlNode(SCF_LOAD_DATA); $$->load_data.relation_name = $7; $$->load_data.file_name = tmp_file_name; - //free($7); free(tmp_file_name); } ; @@ -692,7 +671,6 @@ set_variable_stmt: $$ = new ParsedSqlNode(SCF_SET_VARIABLE); $$->set_variable.name = $2; $$->set_variable.value = *$4; - //free($2); delete $4; } ; @@ -703,14 +681,19 @@ opt_semicolon: /*empty*/ %% //_____________________________________________________________________ extern void scan_string(const char *str, yyscan_t scanner); -extern void free_allocated_strings(); int sql_parse(const char *s, ParsedSqlResult *sql_result) { yyscan_t scanner; - yylex_init(&scanner); + std::vector allocated_strings; + yylex_init_extra(static_cast(&allocated_strings),&scanner); scan_string(s, scanner); int result = yyparse(s, sql_result, scanner); + + for (char *ptr : allocated_strings) { + free(ptr); + } + allocated_strings.clear(); + yylex_destroy(scanner); - free_allocated_strings(); return result; -} +} \ No newline at end of file