注意:出于演示目的,我选择在这里发明一个示例语法,因为您的问题没有显示您的问题。使用这里推荐的方法,在解析后编写一个函数来执行查询应该不难。
我真的建议构建一个解析树。
我会推荐属性传播优先于语义动作。参见例如
Spirit 中的属性传播规则非常灵活。默认公开的属性类型在每个 Parser 的文档中都有很好的记录
例如 -qi::char_
会导致boost::optional<char>
并且qi::double_ | qi::int_
会导致boost::variant<double, int>
.
您可能希望在您自己发明的 AST 数据类型中累积已解析的元素,例如:
struct SelectStatement
{
std::vector<std::string> columns, fromtables;
std::string whereclause; // TODO model as a vector<WhereCondition> :)
friend std::ostream& operator<<(std::ostream& os, SelectStatement const& ss)
{
return os << "SELECT [" << ss.columns.size() << " columns] from [" << ss.fromtables.size() << " tables]\nWHERE " + ss.whereclause;
}
};
您可以通过将结构调整为融合序列来使其适应 Spirits 属性传播机制:
BOOST_FUSION_ADAPT_STRUCT(SelectStatement,
(std::vector<std::string>, columns)
(std::vector<std::string>, fromtables)
(std::string, whereclause)
)
现在您可以将以下规则解析为该类型:
sqlident = lexeme [ alpha >> *alnum ]; // table or column name
columns = no_case [ "select" ] >> (sqlident % ',');
tables = no_case [ "from" ] >> (sqlident % ',');
start = columns >> tables
>> no_case [ "where" ]
>> lexeme [ +(char_ - ';') ]
>> ';';
您可以在此处实时查看此代码:http : //liveworkspace.org/code/0b525234dbce22cbd8becd69f84065c1
完整的演示代码:
// #define BOOST_SPIRIT_DEBUG
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;
struct SelectStatement
{
std::vector<std::string> columns, fromtables;
std::string whereclause; // TODO model as a vector<WhereCondition> :)
friend std::ostream& operator<<(std::ostream& os, SelectStatement const& ss)
{
return os << "SELECT [" << ss.columns.size() << " columns] from [" << ss.fromtables.size() << " tables]\nWHERE " + ss.whereclause;
}
};
BOOST_FUSION_ADAPT_STRUCT(SelectStatement,
(std::vector<std::string>, columns)
(std::vector<std::string>, fromtables)
(std::string, whereclause)
)
template <typename It, typename Skipper = qi::space_type>
struct parser : qi::grammar<It, SelectStatement(), Skipper>
{
parser() : parser::base_type(start)
{
using namespace qi;
sqlident = lexeme [ alpha >> *alnum ]; // table or column name
columns = no_case [ "select" ] >> (sqlident % ',');
tables = no_case [ "from" ] >> (sqlident % ',');
start = columns >> tables
>> no_case [ "where" ]
>> lexeme [ +(char_ - ';') ]
>> ';';
BOOST_SPIRIT_DEBUG_NODE(start);
BOOST_SPIRIT_DEBUG_NODE(sqlident);
BOOST_SPIRIT_DEBUG_NODE(columns);
BOOST_SPIRIT_DEBUG_NODE(tables);
}
private:
qi::rule<It, std::string() , Skipper> sqlident;
qi::rule<It, std::vector<std::string>(), Skipper> columns , tables;
qi::rule<It, SelectStatement() , Skipper> start;
};
template <typename C, typename Skipper>
bool doParse(const C& input, const Skipper& skipper)
{
auto f(std::begin(input)), l(std::end(input));
parser<decltype(f), Skipper> p;
SelectStatement query;
try
{
bool ok = qi::phrase_parse(f,l,p,skipper,query);
if (ok)
{
std::cout << "parse success\n";
std::cout << "query: " << query << "\n";
}
else std::cerr << "parse failed: '" << std::string(f,l) << "'\n";
if (f!=l) std::cerr << "trailing unparsed: '" << std::string(f,l) << "'\n";
return ok;
} catch(const qi::expectation_failure<decltype(f)>& e)
{
std::string frag(e.first, e.last);
std::cerr << e.what() << "'" << frag << "'\n";
}
return false;
}
int main()
{
const std::string input = "select id, name, price from books, authors where books.author_id = authors.id;";
bool ok = doParse(input, qi::space);
return ok? 0 : 255;
}
将打印输出:
parse success
query: SELECT [3 columns] from [2 tables]
WHERE books.author_id = authors.id