我可能會問一個非常微不足道的問題,但我的腦子裏並沒有阻塞它來破解它。 試圖解析像where子句如下所示,使用boost ::精神::氣生成對無法解析SQL類型,其中條件使用boost :: spirit :: qi
std::string input = "book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'"
我已經通過以下線程消失的載體,但仍無法做到這一點:-(Thread5一個SQL Thread4Thread3 Thread2 Thread1
[Thread1][6]
[Thread2][7]
[Thread3][8]
[Thread4][9]
[Thread5][10]
我真誠的請求,請幫助我瞭解如何實現這一目標?可能是我還沒有完全給我的100%,但是請善待....作爲第一步,我只是檢查是否可以在Vector中獲取所有的令牌,然後解析每個Vector元素以生成另一個std向量: :對
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>
#include <map>
#include <vector>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
typedef std::string str_t;
typedef std::pair<str_t, str_t> pair_t;
typedef std::vector<pair_t> pairs_t;
typedef std::vector<str_t> strings_t;
//typedef std::map<std::string, std::string> map_t;
//typedef std::vector<map_t> maps_t;
template <typename It, typename Skipper = qi::space_type>
//struct parser : qi::grammar<It, pairs_t(), Skipper>
struct parser : qi::grammar<It, strings_t(), Skipper>
{
parser() : parser::base_type(start)
{
using namespace qi;
cond = lexeme [ *(char_) ];
conds = *(char_) >> cond % (lit("and"));
//conds = *(char_ - lit("and")) >>(cond % lit("and"));
/*cond = lexeme [ *(char_ - lit("and")) ];
cond = key >> "=" >> value;
key = *(char_ - "=");
value = ('\'' >> *(~char_('\'')) >> '\'');
kv_pair = key >> value;*/
start = conds;
//cond = key >> "=" >> value;
//key = *(char_ - "=");
//value = ('\'' >> *(~char_('\'')) >> '\'');
// kv_pair = key >> value;
// start = kv_pair;
}
private:
qi::rule<It, str_t(), Skipper> cond;
qi::rule<It, strings_t(), Skipper> conds;
//qi::rule<It, std::string(), Skipper> key, value;//, cond;
//qi::rule<It, pair_t(), Skipper> kv_pair;
//qi::rule<It, pairs_t(), Skipper> start;
qi::rule<It, strings_t(), 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;
strings_t data;
try
{
bool ok = qi::phrase_parse(f,l,p,skipper,data);
if (ok)
{
std::cout << "parse success\n";
std::cout << "No Of Key-Value Pairs= "<<data.size()<<"\n";
}
else std::cerr << "parse failed: '" << 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()
{
std::cout<<"Pair Test \n";
const std::string input = "book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'";
bool ok = doParse(input, qi::space);
std::cout<< input <<"\n";
return ok? 0 : 255;
}
OUTPUT:
Pair Test
parse success
No Of Key-Value Pairs= 2
book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'
這我預計4 ...因爲有4個條件!
由於提前 問候, 維韋克
一些示例工作OUT- live on coliru
也許你也會感興趣:[使用Boost.Spirit解析C++中的SQL查詢](http://stackoverflow.com/a/12631146/85371)和[試圖用Boost-Spirit解析SQL語句](http ://stackoverflow.com/a/10339716/85371)。 – sehe