15

I was wondering whether there is a way in Boost.Spirit.Qi to dynamically combine an arbitrary number of rules at runtime. The inner workings of Boost.Spirit are still a bit of a mystery to me, but since rules are implemented as objects it seems feasible. My motivation is to make certain parts of my grammar easily extendable.

Consider the following contrived example:

namespace qi = boost::spirit::qi;
namespace px = boost::phoenix;

typedef std::string::const_iterator iterator_t;

template<typename Expr>
inline bool parse_full(const std::string& input, const Expr& expr)
{
    iterator_t first(input.begin()), last(input.end());

    bool result = qi::phrase_parse(first, last, expr, boost::spirit::ascii::space);

    return first == input.end() && result;
}

void no_op() {}

int main(int argc, char *argv[]) 
{
    int attr = -1;

    // "Static" version - Works fine!
    /*
    qi::rule<iterator_t, void(int&)> grammar;

    qi::rule<iterator_t, void(int&)> ruleA = qi::char_('a')[qi::_r1 = px::val(0)];
    qi::rule<iterator_t, void(int&)> ruleB = qi::char_('b')[qi::_r1 = px::val(1)];
    qi::rule<iterator_t, void(int&)> ruleC = qi::char_('c')[qi::_r1 = px::val(2)];

    grammar = 
        ruleA(qi::_r1) | //[no_op]
        ruleB(qi::_r1) | //[no_op]
        ruleC(qi::_r1);  //[no_op]
    */

    // "Dynamic" version - Does not compile! :(

    std::vector<qi::rule<iterator_t, void(int&)>> rules;

    rules.push_back(qi::char_('a')[qi::_r1 = px::val(0)]);
    rules.push_back(qi::char_('b')[qi::_r1 = px::val(1)]);
    rules.push_back(qi::char_('c')[qi::_r1 = px::val(2)]);

    std::vector<qi::rule<iterator_t, void(int&)>>::iterator i(rules.begin()), last(rules.end());

    qi::rule<iterator_t, void(int&)> grammar;

    grammar = (*i)(qi::_r1);

    for(++i; i!=last; ++i)
    {
        grammar = grammar.copy() | (*i)(qi::_r1);
    }

    // Tests

    if(parse_full("a", grammar(px::ref(attr)))) std::cout << attr << std::endl;
    if(parse_full("b", grammar(px::ref(attr)))) std::cout << attr << std::endl;
    if(parse_full("c", grammar(px::ref(attr)))) std::cout << attr << std::endl;

    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    return 0;
}

The error given by Visual Studio 2010 is:

error C2440: 'initializing' : cannot convert from 'boost::fusion::void_' to 'int &'

My suspicion is that this is caused by not passing the inherited attribute to grammar.copy(). Unfortunately, I couldn't find an easy way of doing this, so I opted for a workaround. As a result, I have one last version (and I would already like to thank anyone who stuck around until now!). This one actually seems to work:

    // "Dynamic" version - Kind of works! :-/

    std::vector<qi::rule<iterator_t, void(int&)>> rules;

    rules.push_back(qi::char_('a')[qi::_r1 = px::val(0)]);
    rules.push_back(qi::char_('b')[qi::_r1 = px::val(1)]);
    rules.push_back(qi::char_('c')[qi::_r1 = px::val(2)]);

    std::vector<qi::rule<iterator_t, void(int&)>>::iterator i(rules.begin()), last(rules.end());

    qi::rule<iterator_t, int()> temp;

    temp = (*i)(qi::_val); //[no_op]

    for(++i; i!=last; ++i)
    {
        temp = temp.copy() | (*i)(qi::_val); //[no_op]
    }

    qi::rule<iterator_t, void(int&)> grammar;

    grammar = temp[qi::_r1 = qi::_1];

However, once I attach a simple semantic action (such as "[no_op]"), the behavior becomes really weird. Rather than printing 0,1,2 as before, it prints 0,0,2. So I'm wondering, is what I'm trying to accomplish resulting in undefined behavior? Is this a bug? Or quite possibly, am I just using something (e.g. semantic actions?) the wrong way?

kloffy
  • 2,928
  • 2
  • 25
  • 34

1 Answers1

7

Yes I'm not sure to really understand how it work internally but you do not copy all the rules in your for loop (only the left one) so this seems to work:

std::vector<qi::rule<iterator_t, void(int&)>> rules;

rules.push_back(qi::char_('a')[qi::_r1 = px::val(0)]);
rules.push_back(qi::char_('b')[qi::_r1 = px::val(1)]);
rules.push_back(qi::char_('c')[qi::_r1 = px::val(2)]);

std::vector<qi::rule<iterator_t, void(int&)>>::iterator
 i(rules.begin()), last(rules.end());

qi::rule<iterator_t, int()> temp;

for(; i!=last; ++i)
{
qi::rule<iterator_t, int()> tmp = (*i)(qi::_val)[no_op];

    temp = temp.copy() | tmp.copy();
}

qi::rule<iterator_t, void(int&)> grammar;

grammar = temp[qi::_r1 = qi::_1];

// Tests

int intres1;
int intres2;
int intres3;

bool res1 = parse_full("a", grammar(px::ref(intres1)) );
bool res2 = parse_full("b", grammar(px::ref(intres2)) );
bool res3 = parse_full("c", grammar(px::ref(intres3)) );
ABu
  • 10,423
  • 6
  • 52
  • 103
n1ckp
  • 1,481
  • 1
  • 14
  • 21
  • Thank you! Your answer looks very promising. Unfortunately, I don't have any means of testing it right now. I'll accept it as soon as I get a chance to try it! – kloffy Jul 08 '11 at 12:17
  • 1
    Sincere apologies for being almost 2 years late. I have now come around to test this solution and it works great! – kloffy Apr 16 '13 at 04:19