Stack Exchange
boost::spirit::qi grammar using grammars with different iterator types
I'm facing a problem with boost::spirit::qi. I have defined the following two parsers:
struct attr_1 { std::string a; };
BOOST_FUSION_ADAPT_STRUCT(attr_1, (std::string, a))

struct attr_2 { double a; };
BOOST_FUSION_ADAPT_STRUCT(attr_2, (double, a))

struct grammar_1 : boost::spirit::qi::grammar<const char*, attr_1()> {
    grammar_1() : grammar_1::base_type{rule_} { 
        rule_ = boost::spirit::qi::eps >> +boost::spirit::ascii::upper; 
    }
private:
    boost::spirit::qi::rule<const char*, attr_1()> rule_;
};

struct grammar_2 : boost::spirit::qi::grammar<std::string::iterator, attr_2()> {
    grammar_2() : grammar_2::base_type{rule_} { 
        rule_ = boost::spirit::qi::double_; 
    }
private:
    boost::spirit::qi::rule<std::string::iterator, attr_2()> rule_;
};

Now, I would like to write a third grammar using the previous ones, as follows:
typedef boost::variant<attr_1, attr_2> attr_comp;

struct grammar_comp : boost::spirit::qi::grammar<????, attr_comp()> {
    grammar_comp() : grammar_comp::base_type{rule_} { 
        rule_ = (g1_ | g2_); 
    }
private:
    grammar_1 g1_;
    grammar_2 g2_;
    boost::spirit::qi::rule<????, attr_comp()> rule_;
};

Since grammar_1 and grammar_2 have different iterators type, which type should I place in the definition of the new grammar?
Here is a (non-compiling) simplified example:
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/home/qi.hpp>
#include <boost/variant.hpp>
#include <string>
#include <sstream>

struct attr_1 { std::string a; };
BOOST_FUSION_ADAPT_STRUCT(attr_1, (std::string, a))

struct attr_2 { double a; };
BOOST_FUSION_ADAPT_STRUCT(attr_2, (double, a))

struct grammar_1 : boost::spirit::qi::grammar<const char*, attr_1()> {
    grammar_1() : grammar_1::base_type{rule_} { 
        rule_ = boost::spirit::qi::eps >> +boost::spirit::ascii::upper; 
    }
private:
    boost::spirit::qi::rule<const char*, attr_1()> rule_;
};

struct grammar_2 : boost::spirit::qi::grammar<std::string::iterator, attr_2()> {
    grammar_2() : grammar_2::base_type{rule_} { 
        rule_ = boost::spirit::qi::double_; 
    }
private:
    boost::spirit::qi::rule<std::string::iterator, attr_2()> rule_;
};

typedef boost::variant<attr_1, attr_2> attr_comp;

struct grammar_comp : boost::spirit::qi::grammar<????, attr_comp()> {
    grammar_comp() : grammar_comp::base_type{rule_} { 
        rule_ = (g1_ | g2_); 
    }
private:
    grammar_1 g1_;
    grammar_2 g2_;
    boost::spirit::qi::rule<????, attr_comp()> rule_;
};

int main() {
    std::string s;
    std::istringstream iss("3\n13.2\nCIAO\nFOOFOOfoo\n");
    grammar_comp gg_;
    attr_comp aa_;
    while (std::getline(iss, s)){
        auto it = s.begin();
        if (boost::spirit::qi::parse(it, s.end(), gg_, aa_)) {
            std::cout << s << std::endl;
            std::cout << std::endl;
        }
    }
    return 0;
}


You can't use sub grammars of different iterator types. For the obvious reason that you won't magically be parsing different sets of input.
A single input implies a single input iterator range.
Just defer the decision about iterator type to the toplevel instantiator:
Live On Coliru
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/spirit/home/qi.hpp>
#include <boost/variant.hpp>
#include <sstream>
#include <string>

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

struct attr_1 {
    std::string a;
};
BOOST_FUSION_ADAPT_STRUCT(attr_1, (std::string, a))

struct attr_2 {
    double a;
};
BOOST_FUSION_ADAPT_STRUCT(attr_2, (double, a))

template <typename It = const char *> struct grammar_1 : qi::grammar<It, attr_1()> {
    grammar_1() : grammar_1::base_type{ rule_ } { rule_ = qi::eps >> +ascii::upper; }

  private:
    qi::rule<It, attr_1()> rule_;
};

template <typename It = std::string::const_iterator> struct grammar_2 : qi::grammar<It, attr_2()> {
    grammar_2() : grammar_2::base_type{ rule_ } { rule_ = qi::double_; }

  private:
    qi::rule<It, attr_2()> rule_;
};

typedef boost::variant<attr_1, attr_2> attr_comp;

template <typename It = std::string::const_iterator> struct grammar_comp : qi::grammar<It, attr_comp()> {
    grammar_comp() : grammar_comp::base_type{ rule_ } { rule_ = (g1_ | g2_); }

  private:
    grammar_1<It> g1_;
    grammar_2<It> g2_;
    qi::rule<It, attr_comp()> rule_;
};

int main() {
    std::istringstream iss("3\n13.2\nCIAO\nFOOFOOfoo\n");

    grammar_comp<> gg_;
    attr_comp aa_;
    std::string s;

    while (std::getline(iss, s)) {
        auto it = s.cbegin();
        if (qi::parse(it, s.cend(), gg_, aa_)) {
            std::cout << s << std::endl;
            std::cout << std::endl;
        }
    }
}

Prints
3

13.2

CIAO

FOOFOOfoo

