boost :: spirit解析器返回空向量

时间:2013-06-29 21:58:15

标签: c++ boost-spirit

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

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

1 个答案:

答案 0 :(得分:5)

就像我说in the answer right where I suggested this semantic action进行验证一样:

enter image description here

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

再次,一个完整的演示,包含您的规则:

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

打印

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

按预期