diff --git a/src/observer/sql/parser/lex_sql.l b/src/observer/sql/parser/lex_sql.l index ed028d048..26727a30b 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 代码包含三个部分,使用 %% 分隔 @@ -119,7 +120,7 @@ GROUP RETURN_TOKEN(GROUP); BY RETURN_TOKEN(BY); STORAGE RETURN_TOKEN(STORAGE); FORMAT RETURN_TOKEN(FORMAT); -{ID} yylval->cstring=strdup(yytext); RETURN_TOKEN(ID); +{ID} yylval->cstring=strdup(yytext); static_cast*>(yyextra)->push_back(yylval->cstring); RETURN_TOKEN(ID); "(" RETURN_TOKEN(LBRACE); ")" RETURN_TOKEN(RBRACE); @@ -136,8 +137,8 @@ FORMAT RETURN_TOKEN(FORMAT); "-" | "*" | "/" { return yytext[0]; } -\"[^"]*\" yylval->cstring = strdup(yytext); RETURN_TOKEN(SSS); -'[^']*\' yylval->cstring = strdup(yytext); RETURN_TOKEN(SSS); +\"[^"]*\" yylval->cstring = strdup(yytext); static_cast*>(yyextra)->push_back(yylval->cstring); RETURN_TOKEN(SSS); +'[^']*\' yylval->cstring = strdup(yytext); static_cast*>(yyextra)->push_back(yylval->cstring); RETURN_TOKEN(SSS); . LOG_DEBUG("Unknown character [%c]",yytext[0]); return yytext[0]; %% diff --git a/src/observer/sql/parser/yacc_sql.y b/src/observer/sql/parser/yacc_sql.y index 786879000..db210e970 100644 --- a/src/observer/sql/parser/yacc_sql.y +++ b/src/observer/sql/parser/yacc_sql.y @@ -254,7 +254,6 @@ drop_table_stmt: /*drop table 语句的语法解析树*/ DROP TABLE ID { $$ = new ParsedSqlNode(SCF_DROP_TABLE); $$->drop_table.relation_name = $3; - free($3); }; show_tables_stmt: @@ -267,7 +266,6 @@ desc_table_stmt: DESC ID { $$ = new ParsedSqlNode(SCF_DESC_TABLE); $$->desc_table.relation_name = $2; - free($2); } ; @@ -279,9 +277,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); } ; @@ -291,8 +286,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 语句的语法解析树*/ @@ -301,7 +294,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); vector *src_attrs = $6; @@ -314,7 +307,6 @@ create_table_stmt: /*create table 语句的语法解析树*/ delete $5; if ($8 != nullptr) { create_table.storage_format = $8; - free($8); } } ; @@ -342,7 +334,6 @@ attr_def: $$->type = (AttrType)$2; $$->name = $1; $$->length = $4; - free($1); } | ID type { @@ -350,7 +341,6 @@ attr_def: $$->type = (AttrType)$2; $$->name = $1; $$->length = 4; - free($1); } ; number: @@ -374,7 +364,6 @@ insert_stmt: /*insert 语句的语法解析树*/ $$->insertion.values.emplace_back(*$6); reverse($$->insertion.values.begin(), $$->insertion.values.end()); delete $6; - free($3); } ; @@ -406,7 +395,6 @@ value: char *tmp = common::substr($1,1,strlen($1)-2); $$ = new Value(tmp); free(tmp); - free($1); } ; storage_format: @@ -429,7 +417,6 @@ delete_stmt: /* delete 语句的语法解析树*/ $$->deletion.conditions.swap(*$4); delete $4; } - free($3); } ; update_stmt: /* update 语句的语法解析树*/ @@ -443,8 +430,6 @@ update_stmt: /* update 语句的语法解析树*/ $$->update.conditions.swap(*$7); delete $7; } - free($2); - free($4); } ; select_stmt: /* select 语句的语法解析树*/ @@ -538,14 +523,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); } ; @@ -558,7 +540,6 @@ rel_list: relation { $$ = new vector(); $$->push_back($1); - free($1); } | relation COMMA rel_list { if ($3 != nullptr) { @@ -568,7 +549,6 @@ rel_list: } $$->insert($$->begin(), $1); - free($1); } ; @@ -672,7 +652,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); } ; @@ -691,7 +670,6 @@ set_variable_stmt: $$ = new ParsedSqlNode(SCF_SET_VARIABLE); $$->set_variable.name = $2; $$->set_variable.value = *$4; - free($2); delete $4; } ; @@ -705,9 +683,16 @@ extern void scan_string(const char *str, yyscan_t scanner); 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); return result; -} +} \ No newline at end of file