4

问题是我创建了一个对任务有用的语法,但现在任务发生了变化,我需要定义新规则。

但是我不想修改我已经拥有的语法,而不是我想创建一个新的语法,它使用我现有的语法而没有代码重复,所以我只需要定义我需要的新规则。我尝试了这样的事情,但没有工作:

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; 
};

基本上Old_grammar是我已经拥有的语法,我只想在 中添加我需要的新规则,New_grammar并且还能够使用我在Old_gramar.

4

1 回答 1

8

我不会通过继承使事情复杂化。组合通常绰绰有余,而且不会混淆 qi 解析器接口。

我已经画了一个关于如何完成版本控制语法的小草图。假设旧语法:

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;
};

如您所见,这是非常严格的,要求版本正好是 1。然而,未来发生了,并且发明了新版本的语法。现在,我要补充

friend struct NewGrammar<It, Skipper>;

回到旧语法并着手实施新​​语法,如果需要,它会优雅地退回到旧语法:

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;
};

(我没有尝试让它与继承一起工作,尽管它很可能也应该工作。)

让我们测试一下这个婴儿:

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"));
}

显然,所有测试都通过了:在 Coliru 1上实时查看希望这会有所帮助!


1好吧,该死的。Coliru 今天编译这个太慢了。所以这里是完整的测试程序:

#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"));
}
于 2013-07-09T10:50:58.427 回答