3

I'm learning Boost Spirit 2, and I can't figure out why I can't parse a sequence of two integers into an STL vector of int. From the documentation of the >> operator (here) I know the attribute of the grammar int_ >> int_ should be vector<int>. And from the attribute notation documentation I know that "The notation of vector<A> stands for any STL container holding elements of type A".

And yet, when I try to add an action to int_ >> int_, which takes std::vector as its attribute, compilation fails.

Among other error messages, it says: /usr/include/boost/bind/mem_fn_template.hpp:163:7: note: no known conversion for argument 2 from ‘boost::fusion::vector<int, int>’ to ‘const std::vector<int>&’

I'm aware of this question ("Getting boost::spirit::qi to use stl containers"), but the solution there - including std_tuple.hpp - did not change anything.

Why is this?

#include <iostream>
#include <string>
#include <vector>

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/bind.hpp>

namespace client
{
    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::ascii;

    class ParserActions
    {
    public:
        void printivec (std::vector<int> const& ivec)
        {
            std::cout << "We see " << ivec.size() << " integers" << std::endl;
        }
    };

    template <typename Iterator>
    bool parse_ivec(Iterator first, Iterator last)
    {
        using ascii::space;

        ParserActions actionContainer;

        auto two_ints = (qi::int_ >> qi::int_);

        bool r = qi::parse(
            first,                          /*< start iterator >*/
            last,                           /*< end iterator >*/
            two_ints[boost::bind(&ParserActions::printivec,&actionContainer,_1)]
            );

        return first == last && r;
    }
}

int main()
{
    std::string str = "12 13";

    if (client::parse_ivec(str.begin(),str.end()))
        std::cout << "Parsing succeeded\n";
    else
        std::cout << "Parsing failed!\n";

    return 0;
}
Yuval Kfir
  • 33
  • 6

1 Answers1

3

int_ >> int_ synthesizes a tuple of int, int.¹

If you want to force synthesizing a container, make it repeat(2) [ int_ ] to express that.

Sidenote: As you'll see below, there's room for attribute compatibility even if the bound attribute is a cotainer, so you don't need to.

Sidenote: auto with parser expressions is asking undefined behaviour: Assigning parsers to auto variables

Sidenote: your input uses space delimiter, but the parser doesn't. That will never work.

Your code sample seems to have little to do with the goal, so here's my small example:

1. vector<int>:

Live On Coliru

#include <boost/spirit/include/qi.hpp>
using Vec = std::vector<int>;
namespace qi = boost::spirit::qi;

template <typename It> Vec parse_ivec(It first, It last) {
    Vec v;
    if (qi::phrase_parse(first, last,  qi::int_ >> qi::int_ >> qi::eoi, qi::space, v))
        return v;
    throw std::runtime_error("Parse failed");
}

Vec parse_ivec(std::string const& r) { return parse_ivec(begin(r), end(r)); }

int main() {
    for (int i : parse_ivec("12 13")) {
        std::cout << i << "\n";
    }
}

Prints

12
13

2. Adapted structs

Alternatively, if you wanted a struct, not std::vector<>:

Live On Coliru

#include <boost/fusion/adapted/struct.hpp>
struct Vec { int a, b; };
BOOST_FUSION_ADAPT_STRUCT(Vec, a, b)

#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

template <typename It> Vec parse_ivec(It first, It last) {
    Vec v;
    if (qi::phrase_parse(first, last,  qi::int_ >> qi::int_ >> qi::eoi, qi::space, v))
        return v;
    throw std::runtime_error("Parse failed");
}

Vec parse_ivec(std::string const& r) { return parse_ivec(begin(r), end(r)); }

int main() {
    Vec vec = parse_ivec("12 13");
    std::cout << vec.a << " " << vec.b << "\n";
}

Prints

12 13

3. std::tuple<int, int>:

Live On Coliru

#include <boost/fusion/adapted/std_tuple.hpp>
using Vec = std::tuple<int, int>;

#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

template <typename It> Vec parse_ivec(It first, It last) {
    Vec v;
    if (qi::phrase_parse(first, last,  qi::int_ >> qi::int_ >> qi::eoi, qi::space, v))
        return v;
    throw std::runtime_error("Parse failed");
}

Vec parse_ivec(std::string const& r) { return parse_ivec(begin(r), end(r)); }

int main() {
    Vec vec = parse_ivec("12 13");
    std::cout << std::get<0>(vec) << " " << std::get<1>(vec) << "\n";
}

Prints

12 13

¹ Technically, a Fusion sequence, which can be made compatible with a host of types like std::pair, or your own types adapted.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you! These work (of course). The first example is what I was trying to achieve, but I do want to have several rules and actions associated with them. I will experiment a bit more... – Yuval Kfir Apr 29 '18 at 14:03
  • Yup. You're welcome with other questions down the road – sehe Apr 29 '18 at 14:10