在运行时动态组合 Boost.Spirit.Qi 规则(任意数量的替代方案)

发布于 2024-11-05 18:00:54 字数 3294 浏览 6 评论 0原文

我想知道 Boost.Spirit.Qi 中是否有一种方法可以在运行时动态组合任意数量的规则。 Boost.Spirit 的内部工作原理对我来说仍然有点神秘,但由于规则是作为对象实现的,所以它似乎是可行的。我的动机是使我的语法的某些部分易于扩展。

考虑以下人为的示例:

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

Visual Studio 2010 给出的错误是:

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

我怀疑这是由于未将继承的属性传递给 grammar.copy() 引起的。不幸的是,我找不到简单的方法来做到这一点,所以我选择了一种解决方法。结果,我有了最后一个版本(我已经要感谢所有坚持到现在的人了!)。这实际上似乎有效:

    // "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];

但是,一旦我附加一个简单的语义操作(例如“[no_op]”),行为就变得非常奇怪。它不再像以前那样打印 0,1,2,而是打印 0,0,2。所以我想知道,我想要完成的事情是否会导致未定义的行为?这是一个错误吗?或者很可能,我只是以错误的方式使用某些东西(例如语义动作?)?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅气尐潴 2024-11-12 18:00:54

是的,我不确定是否真正理解它的内部工作原理,但您没有复制 for 循环中的所有规则(仅复制左侧的规则),因此这似乎有效:

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

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)) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文