2

我是 boost::spirit 的新手。我编写了解析 SQL 语句的程序,例如“select * from table where conditions”。它编译失败。报告了大量模板错误。那么,有人会帮助我吗?

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

struct db_select {
    void exec() {}

    std::string filed;
    std::string table;
    std::string condition;
};

std::ostream& operator<<(std::ostream& os, const db_select& se) {
    return os << "filed: " << se.filed << " table: " << se.table << " condition: " << se.condition;
}

template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), ascii::space_type> {
    selecter() : selecter::base_type(se) {
            se %= "select" >> +qi::char_ << "from" << +qi::char_ << "where" << +qi::char_;
    } 

    qi::rule<Iterator, db_select (), ascii::space_type> se;
};

int main(int argc, char* argv[]) {
    if (argc < 2)
            return -1;

    std::string str(argv[1]);
    const char* first = str.c_str();
    const char* last = &str[str.size()];
    selecter<const char*> se;
    db_select rst;

    bool r = qi::phrase_parse(first, last, se, ascii::space, rst);
    if (!r || first != last) {
            std::cout << "parse failed, at: " << std::string(first, last) << std::endl;
            return -1;
    } else
            std::cout << "success, " << rst << std::endl;

    return 0;
}
4

1 回答 1

5

编辑最后在电脑后面,修改后的答案:

有三件事需要注意

1. 语法错误

解析器表达式包含错误(<<而不是>>预期的)。这导致了很多编译错误。注意*******编译器错误中的出现:

/.../qi/nonterminal/rule.hpp|176 col 13| error: no matching function for call to ‘assertion_failed(mpl_::failed************ 

旨在引导您找到源代码中的相应注释:

// Report invalid expression error as early as possible.
// If you got an error_invalid_expression error message here,
// then the expression (expr) is not a valid spirit qi expression.
BOOST_SPIRIT_ASSERT_MATCH(qi::domain, Expr);

很容易固定。一个下来,两个去!

2.调整结构

为了将您的数据类型分配为规则属性,您需要使其fusion compatible。最方便的方法:

 BOOST_FUSION_ADAPT_STRUCT(db_select,
    (std::string,field)(std::string,table)(std::string,condition));

现在代码编译。但是解析失败。还有一个:

3. 词素

此外,您还需要采取措施避免使用 +qi::char_ 表达式“吃掉”您的查询关键字。

作为基础,考虑将其编写为

   lexeme [  (!lit("where")  >> +qi::graph) % +qi::space ]
  • lexeme禁止封闭表达式的船长
  • !断言指定的关键字不能匹配

最后,查看文档以qi::no_case进行不区分大小写的匹配。

完整代码

#include <iostream>
#include <string>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

struct db_select {
    void exec() {}

    std::string field;
    std::string table;
    std::string condition;
};

BOOST_FUSION_ADAPT_STRUCT(db_select,(std::string,field)(std::string,table)(std::string,condition));

std::ostream& operator<<(std::ostream& os, const db_select& se) {
    return os << "field: " << se.field << " table: " << se.table << " condition: " << se.condition;
}

template <class Iterator>
struct selecter : qi::grammar<Iterator, db_select (), qi::space_type> {
    selecter() : selecter::base_type(se) {
        using namespace qi;
        se %= "select"
            >> lexeme [ (!lit("from")  >> +graph) % +space ] >> "from"
            >> lexeme [ (!lit("where") >> +graph) % +space ] >> "where" 
            >> +qi::char_;
    } 

    qi::rule<Iterator, db_select (), qi::space_type> se;
};

int main(int argc, char* argv[]) {
    if (argc < 2)
            return -1;

    std::string str(argv[1]);
    const char* first = str.c_str();
    const char* last = &str[str.size()];
    selecter<const char*> se;
    db_select rst;

    bool r = qi::phrase_parse(first, last, se, qi::space, rst);
    if (!r || first != last) {
            std::cout << "parse failed, at: " << std::string(first, last) << std::endl;
            return -1;
    } else
            std::cout << "success, " << rst << std::endl;

    return 0;
}

测试:

g++ test.cpp -o test
./test "select aap, noot, mies from table where field = 'value'"

输出:

success, field: aap,noot,mies table: table condition: field='value'
于 2012-04-26T19:02:02.973 回答