分配或修改规则中继承的属性并将结果传播到父规则

发布于 2024-09-09 01:38:23 字数 1116 浏览 0 评论 0原文

假设我有一个像这样的 Boost Spirit 语法,其中父规则将继承的属性传递给其子规则。

template <typename Iterator>
struct MyGrammar : qi::grammar<Iterator, vector<Foo>()>
{
    qi::rule<Iterator, vector<Foo>()> start;

    qi::rule<Iterator, vector<Foo>(Bar)> parent;
    qi::rule<Iterator, Foo(Bar)> child1;
    qi::rule<Iterator, Foo(Bar)> child2;

    MyGrammar() : MyGrammar::base_type(start)
    {
        start = parent( Bar(etc) ); // An "initial" value for the Bar passed
                                    // between the following rules.  More on
                                    // that later.

        parent = child1(_r1) >> child2(_r1);
        child1 = ...[do something with _r1];
        child2 = ...[do something with _r1];
    }
};

每个子规则返回的 Foo 取决于给定的 Bar 继承属性。我也可能对父规则有这个:

parent = *child(_r1);

我想知道子规则是否可以修改继承的属性,将修改传播到其父规则,并将新值传递给下一个规则儿童规则。

在上面的示例中,start 规则将为 Bar 建立一个初始值,该初始值在父级及其子规则中使用。 Bar 每次通过规则时都会被修改。但这可能吗?这样的代码会是什么样子?

Say I have a Boost Spirit grammar like this, where a parent rule passes an inherited attribute to its children.

template <typename Iterator>
struct MyGrammar : qi::grammar<Iterator, vector<Foo>()>
{
    qi::rule<Iterator, vector<Foo>()> start;

    qi::rule<Iterator, vector<Foo>(Bar)> parent;
    qi::rule<Iterator, Foo(Bar)> child1;
    qi::rule<Iterator, Foo(Bar)> child2;

    MyGrammar() : MyGrammar::base_type(start)
    {
        start = parent( Bar(etc) ); // An "initial" value for the Bar passed
                                    // between the following rules.  More on
                                    // that later.

        parent = child1(_r1) >> child2(_r1);
        child1 = ...[do something with _r1];
        child2 = ...[do something with _r1];
    }
};

The Foo each the child rules return depends on the given Bar inherited attribute. I might also have this for the parent rule:

parent = *child(_r1);

What I'm wondering is whether or not it is possible for a child rule to modify an inherited attribute, propagate the modification to its parent rule, and have the new value be passed to the next child rule.

In the example above, the start rule would be establishing an initial value for the Bar that gets used in parent and its child rules. The Bar gets modified every time it passes through a child rule. Is this even possible though, and what would such code look like?

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

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

发布评论

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

评论(1

苏别ゝ 2024-09-16 01:38:23

继承的属性可以通过引用传递:

qi::rule<Iterator, vector<Foo>(Bar)> parent;
qi::rule<Iterator, Foo(Bar&)> child1; 
qi::rule<Iterator, Foo(Bar&)> child2; 

和:

parent = child1(phoenix::ref(_r1)) >> child2(phoenix::ref(_r1));  

允许准确地实现您想要的。

Inherited attributes can be passed by reference:

qi::rule<Iterator, vector<Foo>(Bar)> parent;
qi::rule<Iterator, Foo(Bar&)> child1; 
qi::rule<Iterator, Foo(Bar&)> child2; 

and:

parent = child1(phoenix::ref(_r1)) >> child2(phoenix::ref(_r1));  

allowing to achieve exactly what you want.

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