使用boost spirit x3解析类似SQL的查询
我正在尝试使用boost spirit x3解析一个简单的类似SQL的查询。 在《精神》的前一个版本中有一个新版本。但对于spirit x3,我们不需要语法。使用boost spirit x3解析类似SQL的查询,sql,c++14,dsl,boost-spirit-x3,Sql,C++14,Dsl,Boost Spirit X3,我正在尝试使用boost spirit x3解析一个简单的类似SQL的查询。 在《精神》的前一个版本中有一个新版本。但对于spirit x3,我们不需要语法。 以下是我的尝试: // SELECT chr, pos FROM table // Select chr, pos FROM table WHERE a=5 and b = 6 // SELECT chr, pos FROM table WHERE a =5 and b=6 INSIDE region // I would like t
以下是我的尝试:
// SELECT chr, pos FROM table
// Select chr, pos FROM table WHERE a=5 and b = 6
// SELECT chr, pos FROM table WHERE a =5 and b=6 INSIDE region
// I would like to extract :
// vector<string> fields = {chr, pos}
// string tablename = "table"
// string where = "a=5 and b=6"
// string region = "region"
string source ="SELECT chr,pos FROM table WHERE a=5 AND b=6 INSIDE region";
// varname = chr, pos, table, region
auto varname = x3::rule<class varname, string>()
= x3::lexeme[(x3::alpha >> *(x3::alnum|'_'|'.'))];
// any character except "INSIDE"
auto condition = x3::rule<class condition, string>()
= x3::lexeme[*x3::char_]-"INSIDE";
// fields selection
auto selectRule = x3::rule<class fields, vector<string>> ()
= "SELECT" >> varname % ",";
// table name
auto fromRule = x3::rule<class fromRule, string>()
= "FROM" >> varname;
// where clause
auto whereRule = x3::rule<class whereRule, vector<string>>()
= "WHERE" >> condition ;
// inside clause
auto insideRule = x3::rule<class insideRule, string>()
= "INSIDE" >> varname;
auto begin = source.begin();
auto end = source.end();
vector<string> results;
x3::phrase_parse(begin,end,
selectRule >> fromRule >> -(whereRule >>-insideRule),
x3::space, results);
if (begin != end)
cout<<"COULD NOT PARSE"<<endl;
else
cout<<"parse succes"<<endl;
for (auto i : results)
cout<<i<<endl;
结果包含提取的所有属性数据,但条件字符串中保留的内部属性数据除外。
有什么线索可以让我的解析器工作吗?现在可以工作了:
struct VqlResult {
std::vector<std::string> selectData;
std::string fromData;
std::string whereData;
std::string insideData;
};
BOOST_FUSION_ADAPT_STRUCT
(
VqlResult,
selectData,
fromData,
whereData,
insideData
);
// Pseudo Sql parser
// e.g : SELECT chr,pos FROM table WHERE a=5 AND b=6 INSIDE region
bool parseSql(const std::string& sql)
{
auto varnameRule = x3::rule<class varname, string>()
= x3::lexeme[(x3::alpha >> *(x3::alnum|'_'|'.'))];
auto columnsRule = x3::rule<class varname, string>()
= x3::lexeme[*(x3::char_ - '"' - "FROM")];
auto conditionRule = x3::lexeme[*(x3::char_ - "INSIDE")];
auto selectRule = x3::rule<class fields, vector<string>> ()
= "SELECT" >> columnsRule % ",";
auto fromRule = "FROM" >> varnameRule;
auto whereRule = "WHERE" >> conditionRule;
auto insideRule = "INSIDE" >> varnameRule;
auto begin = sql.begin();
auto end = sql.end();
VqlResult results;
x3::phrase_parse(begin,end,
selectRule >> fromRule >> -whereRule >> -insideRule,
x3::space, results);
// parse all
if (begin != end){
cout<<"cannot parse "<<endl;
return false;
}
return true;
}
struct VqlResult{
std::矢量数据;
std::string fromData;
std::字符串数据;
std::数据中的字符串;
};
增强融合适应结构
(
VqlResult,
选择数据,
根据数据,
其中数据,
内部数据
);
//伪Sql解析器
//例如:从区域内a=5和b=6的表格中选择chr、pos
boolpassesql(conststd::string&sql)
{
auto varnameRule=x3::rule()
=x3::词素[(x3::alpha>>*(x3::alnum | |'|')];
自动列规则=x3::规则()
=x3::词素[*(x3::char_-'“'-”FROM”);
自动条件规则=x3::lexeme[*(x3::char_u-“内部”);
自动选择规则=x3::规则()
=“选择”>>columnsRule%,”;
auto fromRule=“FROM”>>varnameRule;
auto whereRule=“WHERE”>>条件规则;
auto-insideRule=“INSIDE”>>varnameRule;
auto begin=sql.begin();
auto-end=sql.end();
vql结果;
短语解析(开始,结束,
从rule>>-whereRule>>-insideRule中选择rule>>,
x3:空间、结果);
//解析所有
如果(开始!=结束){
请澄清您期望的输出。
struct VqlResult {
std::vector<std::string> selectData;
std::string fromData;
std::string whereData;
std::string insideData;
};
BOOST_FUSION_ADAPT_STRUCT
(
VqlResult,
selectData,
fromData,
whereData,
insideData
);
// Pseudo Sql parser
// e.g : SELECT chr,pos FROM table WHERE a=5 AND b=6 INSIDE region
bool parseSql(const std::string& sql)
{
auto varnameRule = x3::rule<class varname, string>()
= x3::lexeme[(x3::alpha >> *(x3::alnum|'_'|'.'))];
auto columnsRule = x3::rule<class varname, string>()
= x3::lexeme[*(x3::char_ - '"' - "FROM")];
auto conditionRule = x3::lexeme[*(x3::char_ - "INSIDE")];
auto selectRule = x3::rule<class fields, vector<string>> ()
= "SELECT" >> columnsRule % ",";
auto fromRule = "FROM" >> varnameRule;
auto whereRule = "WHERE" >> conditionRule;
auto insideRule = "INSIDE" >> varnameRule;
auto begin = sql.begin();
auto end = sql.end();
VqlResult results;
x3::phrase_parse(begin,end,
selectRule >> fromRule >> -whereRule >> -insideRule,
x3::space, results);
// parse all
if (begin != end){
cout<<"cannot parse "<<endl;
return false;
}
return true;
}