Roman Holiday
Roman Holiday

Reputation: 109

Parsing a nested hierarchy in spirit qi - can't get the grammar right

I'm in way over my head with spirit::qi and need some help. I'm trying to parse a phrase that is of the form:

( wheels=4 & chassis=metal & engine=( cylinders=8 & volume=6209 ) )

... into a nested structure:

class dict : public std::map<std::string, boost::variant<dict, std::string>>

... where car_dict["wheels"] returns "4", and car_dict["engine"] returns another dict, where engine_dict["cylinders"] returns "8".

Here is my grammar, I'm hoping someone that has plenty of experience in this stuff can just point out where I'm going wrong.

struct qi_car_grammar : qi::grammar<std::string::const_iterator, dict()>
{
    qi::rule<std::string::const_iterator, dict()> car_dict;
    qi::rule<std::string::const_iterator, std::string()> car_key;
    qi::rule<std::string::const_iterator, boost::variant<dict, std::string>()> car_variant_value;

    qi_car_grammar()
        : qi_car_grammar::base_type(car_dict)
    {
        // RULES
        car_key %= *(qi::lit(' ')) >> *(~qi::lit('='));

        car_variant_value %= car_dict | *(~qi::char_("&)"));

        car_dict = qi::lit('(')
            >> car_key >> qi::lit('=') >> car_variant_value
            >> *(qi::lit('&') >> car_key >> qi::lit('=') >> car_variant_value)
            >> qi::lit(')');
    }
}

Any help appreciated. Before anyone suggests it, no, I don't have much control over the structure that I am parsing into.

Upvotes: 1

Views: 385

Answers (1)

sehe
sehe

Reputation: 393009

Mmm. I just did a general cleanup pass¹.

You were already pretty close.

You might just have been missing the boost/fusion/adapted/std_pair.hpp header, which is required in order to transform parsed tuples into std::pair<K,V> automagically.

Here's is the working sample (I spent the most time on creating the dict pretty printer...):

Live On Coliru

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

namespace qi = boost::spirit::qi;

class dict : public std::map<std::string, boost::variant<dict, std::string>>
{
};

template <typename It = std::string::const_iterator, typename Skipper = qi::space_type>
struct qi_car_grammar : qi::grammar<It, dict(), Skipper>
{
    qi_car_grammar()
        : qi_car_grammar::base_type(car_dict)
    {
        // RULES
        car_key   = *~qi::char_("=");
        car_value = *~qi::char_("&)");

        car_variant_value = car_dict | car_value;

        car_dict = '(' >> (car_key >> '=' >> car_variant_value) % '&' >> ')';
    }

  private:
    qi::rule<It, dict(), Skipper> car_dict;
    qi::rule<It, std::string()> car_key, car_value;
    qi::rule<It, boost::variant<dict, std::string>(), Skipper> car_variant_value;
};

// for debug
static inline std::ostream& operator<<(std::ostream& os, dict const& d) {
    struct vis {
        using result_type = void;
        std::ostream& _os;
        std::string indent;

        void operator()(std::string const& s) const { _os << s; }
        void operator()(dict const& d) const { 
            _os << "(\n";
            for (auto& entry: d) {
                _os << indent << "    " << entry.first << " = ";
                boost::apply_visitor(vis{_os, indent+"    "}, entry.second);
                _os << "\n";
            }
            _os << indent << ")\n";
        }
    } v { os, "" };

    v(d);
    return os;
}

int main()
{
    std::string const input = "( wheels=4 & chassis=metal & engine=( cylinders=8 & volume=6209 ) )";

    dict parsed;
    qi_car_grammar<> g;

    auto f = input.begin(), l = input.end();
    bool ok = qi::phrase_parse(f, l, g, qi::space, parsed);

    if (ok)
        std::cout << "Parsed: " << parsed << "\n";
    else
        std::cout << "Parsed failed\n";

    if (f!=l)
        std::cout << "Remaining unparsed: '" << std::string(f,l) << "'\n";
}

Prints:

Parsed: (
    chassis = metal 
    engine = (
        cylinders = 8 
        volume = 6209 
    )

    wheels = 4 
)

¹ (as I always do when reading a code sample, it helps my understanding)

Upvotes: 2

Related Questions