简体   繁体   中英

boost::spirit parser returns empty vector

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 (eg "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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM