2

私は非常に些細な質問をしているかもしれませんが、それを解読するために脳からブロックを取得していません. 以下に示すように、boost::spirit::qi を使用して SQL のような where 句を解析して、ペアのベクトルを生成しようとしています

std::string input = "book.author_id = '1234' and book.isbn = 'xy99' and book.type = 'abc' and book.lang = 'Eng'"

次のスレッドを実行しましたが、まだ実行できません:-( Thread5 Thread4 Thread3 Thread2 Thread1

[Thread1][6]
[Thread2][7]
[Thread3][8]
[Thread4][9]
[Thread5][10]

これを達成する方法を理解するのを手伝ってください...私は100%を完全に与えていなかったかもしれませんが、親切にしてください....

これが完全なコードです(私がやりたいとコメントした部分もあります)。最初のステップとして、ベクター内のすべてのトークンを取得できるかどうかを確認し、各ベクター要素を解析して std::pair の別のベクターを生成できるかどうかを確認しました

#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;
}

出力:

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つを期待しています!!

よろしくお願いします、Vivek

うまくいくためのいくつかの例-coliruでライブ

4

1 に答える 1

1

話が逸れて申し訳ありませんが、あなたの文法はあなたが想像していたよりもはるかに壊れています。

    conds   =  *(char_) // ...

ここでは、基本的に、空白をスキップして、すべての入力を 1 つの文字列に解析しているだけです。実際、追加すると

    for (auto& el : data)
        std::cout << "'" << el << "'\n";

プリントを解析した後:

Pair Test 
parse success
No Of Key-Value Pairs=  2
'book.author_id='1234'andbook.isbn='xy99'andbook.type='abc'andbook.lang='Eng''
''

ご覧のとおり、最初の要素は解析された文字列であり、空の入力でとの両方が一致*char_するため、無料で空の要素を取得します。condscond

シンプルに始めることを強くお勧めします。つまり、はるかに単純です。

地面からゆっくりと文法を構築します。Spirit は、テスト駆動型開発に取り組むための非常に優れたツールです (コンパイル時間は別ですが、考える時間が増えます!)。

以下は、最初の構成要素である修飾子から考え始めindent、より高いレベルの要素に至るまで、私が作成したものです。

// lexemes (no skipper)
ident     = +char_("a-zA-Z.");
op        = no_case [ lit("=") | "<>" | "LIKE" | "IS" ];
nulllit   = no_case [ "NULL" ];
and_      = no_case [ "AND" ];
stringlit = "'" >> *~char_("'") >> "'";

// other productions
field     = ident;
value     = stringlit | nulllit;
condition = field >> op >> value;

conjunction = condition % and_;
start       = conjunction;

これらは、あなたの文法を解析できると私が推測する最も単純なものに近いものです (あまり邪魔にならないように、左右にいくつかの創造的なメモがあります)。

更新だから、これは私が20分で得た場所です:

私は常に、ルールで公開したい型をマッピングすることから始めます。

namespace ast
{
    enum op { op_equal, op_inequal, op_like, op_is };

    struct null { };

    typedef boost::variant<null, std::string> value;

    struct condition
    {
        std::string _field;
        op _op;
        value _value;
    };

    typedef std::vector<condition> conditions;
}

適応conditionなしでスピリット文法で「自然に」使用することはできません。

BOOST_FUSION_ADAPT_STRUCT(ast::condition, (std::string,_field)(ast::op,_op)(ast::value,_value))

次に、文法自体について説明します。

    // lexemes (no skipper)
    ident       = +char_("a-zA-Z._");
    op_token.add
        ("=",    ast::op_equal)
        ("<>",   ast::op_inequal)
        ("like", ast::op_like)
        ("is",   ast::op_is);
    op          = no_case [ op_token ];
    nulllit     = no_case [ "NULL" >> attr(ast::null()) ];
    and_        = no_case [ "AND" ];
    stringlit   = "'" >> *~char_("'") >> "'";

    //// other productions
    field       = ident;
    value       = stringlit | nulllit;
    condition   = field >> op >> value;

    whereclause = condition % and_;
    start       = whereclause;

私の元のスケッチからのわずかな逸脱を見ることができます。これは興味深いものです。

  • _識別子文字に追加
  • シンボルマッチャーに移動op_tokenします (列挙値のマッピングが簡単なため)

それをすべて見るLive And Working On Coliru、出力:

Pair Test 
parse success
No Of Key-Value Pairs=  4
( [book.author_id] = 1234 )
( [book.isbn] LIKE xy99 )
( [book.type] = abc )
( [book.lang] IS NULL )

book.author_id = '1234' and book.isbn liKE 'xy99' and book.type = 'abc' and book.lang IS null
于 2014-05-07T19:24:02.910 に答える