本文介绍了boost :: spirit解析器返回空向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么使用下列规则的解析器会传回空容器?有3条规则。一个用于解析除双引号之外的字符串,第二个解析一对(例如col1:2),第三个解析这样的对的向量。以下MSVS2012程序的输出是

 解析成功
结果:'':0
结果:'':0
result:'':0

b
$ b

 命名空间解析器
{

spirit :: qi :: rule< iterator,column_name_t()> quoted_string =
spirit :: qi :: lexeme [\>> +〜spirit :: qi :: char _(\)>> \];
spirit :: qi :: rule< iterator,column_and_aggregate(),spirit :: qi :: space_type> agg_pair =
quoted_string // :: boost :: bind & apply_col_and_aggr_visitor,spirit :: qi :: _ val,spirit :: qi :: _ 1)]
>':'
//下面使用规则验证技术。 spirit :: int_ [spirit :: qi :: _ pass =(spirit :: qi :: _ 1> = AVG&& amp; spirit :: qi :: _ 1" = SUM)]; // :: boost :: bind (& apply_col_and_aggr_visitor,spirit :: qi :: _ val,spirit :: qi :: _ 1)];
spirit :: qi :: rule< iterator,column_and_aggregate_container(),spirit :: qi :: space_type> aggregates_parser =
'{'
> agg_pair / * [phoenix :: push_back(spirit :: qi :: _ val,spirit :: qi :: _ 1)] * /%','// NB !!! list-redux technic
>'}';
}
使用命名空间解析器;
使用命名空间boost :: spirit;
bool doParse :: string&输入)
{
typedef std :: string :: const_iterator It;
auto f(begin(input)),l(end(input));

// parser< It,qi :: space_type> p;
column_and_aggregate_container data;
typedef BOOST_TYPEOF(qi :: space)skipper_type;
try
{
bool ok = qi :: phrase_parse(f,l,aggregates_parser,qi :: space,data);
if(ok)
{
std :: cout< parse success\\\
;
for(auto& pair:data)
std :: cout<< result:'<< pair.first<< ':<< (int)pair.second<< \\\
;
}
else std :: cerr<< parse failed:'< std :: string(f,l)<< '\\\
;

if(f!= l)std :: cerr<< trailing unparsed:'< std :: string(f,l)<< '\\\
;
return ok;
}
catch(const qi :: expectation_failure< It>& e)
{
std :: string frag(e.first,e.last);
std :: cerr<< e.what()< '< Frag< '\\\
;
}

return false;
}

int main()
{
// bool ok = doParse({'column 1':1,'column 2':0,列3':1});
doParse({\column 1 \:1,\column 2\:0,\column 3\:1});
// return ok? 0:255;
}


模板< typename it,typename skipper = qi :: space_type>
struct quoted_string_parser
{
quoted_string_parser()
{
使用命名空间qi;
quoted_string%= lexeme [''>> *〜char_('')>> ''];
BOOST_SPIRIT_DEBUG_NODE(quoted_string);
}
qi :: rule< it,std :: string(),skipper> quoted_string;
};
template< typename it,typename skipper = qi :: space_type>
struct aggregates_parser:qi :: grammar< it,column_and_aggregate_container(),skipper>
{
aggregates_parser():aggregates_parser: :base_type(aggregates_parser_)
{
使用命名空间qi;
agg_pair%= quoted_string_parser< it,skipper>':'> int _ [_ pass =(qi :: _ AVG& qi :: _ 1" = SUM)];
aggregates_parser_ ='{'> agg_pair%','>'}';
BOOST_SPIRIT_DEBUG_NODE(aggregates_parser_);
}
private:
qi :: rule< it,sql_faggregate(),skipper> faggr;
qi :: rule< it,column_and_aggregate(),skipper> agg_pair;
qi :: rule< it,column_and_aggregate_container(),skipper> aggregates_parser_;
};


解决方案

像我说的进行验证:





当存在语义动作时,通常不会发生自动属性传播。使用%= 强制自动属性传播(我们希望这是因为语义动作不分配属性值,它只是验证它们)。



再次,一个完整的工作示范,结合你的规则:

  #include< boost /fusion/adapted/std_pair.hpp> 
#include< boost / spirit / include / qi.hpp>
#include< boost / spirit / include / phoenix.hpp>

命名空间qi = boost :: spirit :: qi;
namespace phx = boost :: phoenix;

typedef std :: string column_name_t;

enum sql_faggregate
{
AVG,
// ...
SUM,
};

typedef std :: pair< column_name_t,sql_faggregate> column_and_aggregate;
typedef std :: vector< column_and_aggregate> column_and_aggregate_container;

template< typename It,typename Skipper = qi :: space_type>
struct parser:qi :: grammar< It,column_and_aggregate_container(),Skipper>
{
parser():parser :: base_type(aggregates_parser)
{
使用命名空间qi;
quoted_string = lexeme [''>> +〜char_('')>> ';'];
agg_pair%= quoted_string>':'//下面使用了规则验证技术:
> int _ [_ pass =(_1> = AVG&& _1& = SUM)];

aggregates_parser ='{'> agg_pair%','>'}';

BOOST_SPIRIT_DEBUG_NODE(aggregates_parser);
}

private:
qi :: rule< It,std :: string(),qi :: space_type> quoted_string;
qi :: rule< It,sql_faggregate :: space_type> faggr;
qi :: rule< It,column_and_aggregate(),qi :: space_type> agg_pair;
qi :: rule< It,column_and_aggregate_container(),qi :: space_type&
};

bool doParse(const std :: string& input)
{
typedef std :: string :: const_iterator It;
auto f (begin(input)),l(end(input));

parser< It,qi :: space_type> p;
column_and_aggregate_container data;


{
bool ok = qi :: phrase_parse(f,l,p,qi :: space,data);
if(ok)
{
std :: cout< parse success\\\
;
for(auto& pair:data)
std :: cout<< result:'<< pair.first<< ':<< (int)pair.second<< \\\
;
}
else std :: cerr<< parse failed:'< std :: string(f,l)<< '\\\
;

if(f!= l)std :: cerr<< trailing unparsed:'<< std :: string(f,l)<< '\\\
;
return ok;
} catch(const qi :: expectation_failure< It>& e)
{
std :: string frag(e.first,e.last);
std :: cerr<< e.what()< '< frag< '\\\
;
}

return false;
}

int main()
{
bool ok = doParse({\column 1 \:1,\column 2\\ \\:0,\column 3\:1});
return ok? 0:255;
}

列印

  parse success 
result:'column 1':1
result:'column 2':0
result:'column 3':1




Why the parser using the rules below returns an empty container? There're 3 rules. One is for parsing a string of characters except double quote, the second parses a pair (e.g. "col1" : 2) and the third parses the vector of such pairs. The ouput of the program below in MSVS2012 is

parse success
result: '' : 0
result: '' : 0
result: '' : 0

.

namespace parsers
{

            spirit::qi::rule< iterator, column_name_t() > quoted_string =
                    spirit::qi::lexeme["\"" >> +~spirit::qi::char_("\"") >> "\""];
                spirit::qi::rule< iterator, column_and_aggregate(), spirit::qi::space_type > agg_pair =
                    quoted_string//::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )]
                    > ':'
                    // A rule validation technic is used below.
                    > spirit::int_[spirit::qi::_pass = (spirit::qi::_1 >=AVG && spirit::qi::_1<=SUM)];//::boost::bind( &apply_col_and_aggr_visitor, spirit::qi::_val, spirit::qi::_1 )];
                spirit::qi::rule< iterator, column_and_aggregate_container(), spirit::qi::space_type > aggregates_parser =
                      '{'
                    > agg_pair/*[phoenix::push_back(spirit::qi::_val, spirit::qi::_1)]*/ % ',' // N.B.!!! list-redux technic
                    > '}';
}
using namespace parsers;
using namespace boost::spirit;
bool doParse(const std::string& input)
{
    typedef std::string::const_iterator It;
    auto f(begin(input)), l(end(input));

    //parser<It, qi::space_type> p;
    column_and_aggregate_container data;
    typedef BOOST_TYPEOF(qi::space) skipper_type;
    try
    {
        bool ok = qi::phrase_parse(f,l,aggregates_parser,qi::space,data);
        if (ok)
        {
            std::cout << "parse success\n";
            for (auto& pair : data)
                std::cout << "result: '" << pair.first << "' : " << (int) pair.second << "\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<It>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'\n";
    }

    return false;
}

int main()
{
    //bool ok = doParse("{ 'column 1' : 1, 'column 2' : 0, 'column 3' : 1 }");
    doParse("{ \"column 1\" : 1, \"column 2\" : 0, \"column 3\" : 1 }");
    //return ok? 0 : 255;
}


template <typename it, typename skipper = qi::space_type>
struct quoted_string_parser
{
    quoted_string_parser()
    {
        using namespace qi;
        quoted_string %= lexeme['"' >> *~char_('"') >> '"'];
        BOOST_SPIRIT_DEBUG_NODE(quoted_string);
    }
    qi::rule<it, std::string(), skipper> quoted_string;
};
template <typename it, typename skipper = qi::space_type>
struct aggregates_parser : qi::grammar<it, column_and_aggregate_container(), skipper>
{
    aggregates_parser() : aggregates_parser::base_type(aggregates_parser_)
    {
        using namespace qi;
        agg_pair %= quoted_string_parser<it,skipper> > ':' > int_[_pass = (qi::_1 >= AVG && qi::_1 <= SUM)];
        aggregates_parser_ = '{' > agg_pair % ',' > '}';
        BOOST_SPIRIT_DEBUG_NODE(aggregates_parser_);
    }
  private:
    qi::rule<it, sql_faggregate(), skipper> faggr;
    qi::rule<it, column_and_aggregate(), skipper> agg_pair;
    qi::rule<it, column_and_aggregate_container(), skipper> aggregates_parser_;
};
解决方案

Like I said in the answer right where I suggested this semantic action for validation:

When a semantic action is present, automatic attribute propagation does not normally occur. Using %= forces automatic attribute propagation (we want this because the semantic action doesn't assign the attribute value(s), it just validates them).

Again, a fully working demonstration, incorporating your rules:

#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi    = boost::spirit::qi;
namespace phx   = boost::phoenix;

typedef std::string column_name_t;

enum sql_faggregate
{
    AVG,
    // ....
    SUM,
};

typedef std::pair<column_name_t, sql_faggregate> column_and_aggregate;
typedef std::vector<column_and_aggregate> column_and_aggregate_container;

template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, column_and_aggregate_container(), Skipper>
{
    parser() : parser::base_type(aggregates_parser)
    {
        using namespace qi;
        quoted_string     = lexeme['"' >> +~char_('"') >> '"'];
        agg_pair         %= quoted_string > ':' // A rule validation technic is used below.
                            > int_[_pass = (_1 >=AVG && _1<=SUM)];

        aggregates_parser = '{' > agg_pair % ',' > '}';

        BOOST_SPIRIT_DEBUG_NODE(aggregates_parser);
    }

  private:
    qi::rule<It, std::string(), qi::space_type>           quoted_string;
    qi::rule<It, sql_faggregate(), qi::space_type>        faggr;
    qi::rule<It, column_and_aggregate(), qi::space_type>           agg_pair;
    qi::rule<It, column_and_aggregate_container(), qi::space_type> aggregates_parser;
};

bool doParse(const std::string& input)
{
    typedef std::string::const_iterator It;
    auto f(begin(input)), l(end(input));

    parser<It, qi::space_type> p;
    column_and_aggregate_container data;

    try
    {
        bool ok = qi::phrase_parse(f,l,p,qi::space,data);
        if (ok)
        {
            std::cout << "parse success\n";
            for (auto& pair : data)
                std::cout << "result: '" << pair.first << "' : " << (int) pair.second << "\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<It>& e)
    {
        std::string frag(e.first, e.last);
        std::cerr << e.what() << "'" << frag << "'\n";
    }

    return false;
}

int main()
{
    bool ok = doParse("{ \"column 1\" : 1, \"column 2\" : 0, \"column 3\" : 1 }");
    return ok? 0 : 255;
}

Prints

parse success
result: 'column 1' : 1
result: 'column 2' : 0
result: 'column 3' : 1

as expected

这篇关于boost :: spirit解析器返回空向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:45