Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I extend a boost spirit grammar

The thing is that I made a grammar that has been useful for a task, but now the task has changed and I need to define new rules.

But I wouldn't like to modify the grammar I already have instead of that I'd like to create a new grammar that uses the existing grammar I have without code duplication, so I just need to define the new rules I need. I tried something like this, but is not working :

struct New_grammar : Old_grammar<Iterator, Skipper>    
{
    New_grammar() : New_grammar::base_type(Command_list)
    {
        Command_list %= qi::eps >> + Commands;
        Comandos %= oneoldCommand | NewCommand;
        NewCommand = ("NewCommand" >> stmt)[qi::_val = phoenix::new_<NewCom>(qi::_1)];
    }
    // this is a new rule I need:
    qi::rule<Iterator, Commands*(), qi::locals<std::string>, Skipper> NewCommand; 
};

basically Old_grammar is the grammar I already have and I just want to add the new rule I need in the New_grammar and also be able to use the rules and grammars I already have in the Old_gramar.

like image 469
user2562470 Avatar asked Feb 16 '23 23:02

user2562470


1 Answers

I'd not complicate matters by inheriting. Composition is often more than enough, and it won't confuse the qi parser interface.

I've drawn up a small sketch of how a versioning grammar could be done. Assume the old grammar:

template <typename It, typename Skipper>
struct OldGrammar : qi::grammar<It, Skipper, std::string()>
{
    OldGrammar() : OldGrammar::base_type(mainrule)
    {
        using namespace qi;
        rule1 = int_(1); // expect version 1
        rule2 = *char_;  // hopefully some interesting grammar
        mainrule = omit [ "version" > rule1 ] >> rule2;
    }
  private:
    qi::rule<It, Skipper, std::string()> mainrule;
    qi::rule<It, Skipper, int()>         rule1;
    qi::rule<It, Skipper, std::string()> rule2;
};

As you can see, this was quite restrictive, requiring the version to be exactly 1. However, the future happened, and a new version of the grammar was invented. Now, I'd add

friend struct NewGrammar<It, Skipper>;

to the old grammar and go about implementing the new grammar, which graciously falls back to the old grammar if so required:

template <typename It, typename Skipper>
struct NewGrammar : qi::grammar<It, Skipper, std::string()>
{
    NewGrammar() : NewGrammar::base_type(mainrule)
    {
        using namespace qi;
        new_rule1 = int_(2); // support version 2 now
        new_start = omit [ "version" >> new_rule1 ] >> old.rule2; // note, no expectation point

        mainrule = new_start 
                 | old.mainrule;  // or fall back to version 1 grammar
    }
  private:
    OldGrammar<It, Skipper> old;
    qi::rule<It, Skipper, std::string()> new_start, mainrule;
    qi::rule<It, Skipper, int()>         new_rule1;
};

(I haven't tried to make it work with inheritance, though in all likelihood it should also work.)

Let's test this baby:

template <template <typename It,typename Skipper> class Grammar>
bool test(std::string const& input)
{
    auto f(input.begin()), l(input.end());
    static const Grammar<std::string::const_iterator, qi::space_type> p;
    try {
        return qi::phrase_parse(f,l,p,qi::space) && (f == l); // require full input consumed
    } 
    catch(...) { return false; } // qi::expectation_failure<>
}

int main()
{
    assert(true  == test<OldGrammar>("version 1 woot"));
    assert(false == test<OldGrammar>("version 2 nope"));

    assert(true  == test<NewGrammar>("version 1 woot"));
    assert(true  == test<NewGrammar>("version 2 woot as well"));
}

All tests pass, obviously: see it live on Coliru1 Hope this helps!


1 Well, darn. Coliru is too slow to compile this today. So here is the full test program:

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>

namespace qi = boost::spirit::qi;

template <typename It, typename Skipper>
struct NewGrammar; // forward declare for friend declaration

template <typename It, typename Skipper>
struct OldGrammar : qi::grammar<It, Skipper, std::string()>
{
    friend struct NewGrammar<It, Skipper>; // NOTE

    OldGrammar() : OldGrammar::base_type(mainrule)
    {
        using namespace qi;
        rule1 = int_(1); // expect version 1
        rule2 = *char_;  // hopefully some interesting grammar
        mainrule = omit [ "version" > rule1 ] >> rule2;

        BOOST_SPIRIT_DEBUG_NODE(mainrule);
        BOOST_SPIRIT_DEBUG_NODE(rule1);
        BOOST_SPIRIT_DEBUG_NODE(rule2);
    }
  private:
    qi::rule<It, Skipper, std::string()> mainrule;
    qi::rule<It, Skipper, int()>         rule1;
    qi::rule<It, Skipper, std::string()> rule2;
};

template <typename It, typename Skipper>
struct NewGrammar : qi::grammar<It, Skipper, std::string()>
{
    NewGrammar() : NewGrammar::base_type(mainrule)
    {
        using namespace qi;
        new_rule1 = int_(2); // support version 2 now
        new_start = omit [ "version" >> new_rule1 ] >> old.rule2; // note, no expectation point

        mainrule = new_start 
                 | old.mainrule;  // or fall back to version 1 grammar

        BOOST_SPIRIT_DEBUG_NODE(new_start);
        BOOST_SPIRIT_DEBUG_NODE(mainrule);
        BOOST_SPIRIT_DEBUG_NODE(new_rule1);
    }
  private:
    OldGrammar<It, Skipper> old;
    qi::rule<It, Skipper, std::string()> new_start, mainrule;
    qi::rule<It, Skipper, int()>         new_rule1;
};

template <template <typename It,typename Skipper> class Grammar>
bool test(std::string const& input)
{
    auto f(input.begin()), l(input.end());
    static const Grammar<std::string::const_iterator, qi::space_type> p;
    try {
        return qi::phrase_parse(f,l,p,qi::space) && (f == l); // require full input consumed
    } 
    catch(...) { return false; } // qi::expectation_failure<>
}

int main()
{
    assert(true  == test<OldGrammar>("version 1 woot"));
    assert(false == test<OldGrammar>("version 2 nope"));

    assert(true  == test<NewGrammar>("version 1 woot"));
    assert(true  == test<NewGrammar>("version 2 woot as well"));
}
like image 80
sehe Avatar answered Feb 25 '23 06:02

sehe