Boost logo

Boost Users :

Subject: Re: [Boost-users] [Boost.Spirit] Synthesised attributes and sub-rules
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2010-10-14 09:06:15


> On 14 October 2010 22:36, Hartmut Kaiser <hartmut.kaiser_at_[hidden]> wrote:
> > That should be easy, see below.
> >
> > start = a_rule | b_rule | c_rule;
> >
> > qi::rule<Iterator, std::vector<int>()> a_rule;
> > qi::rule<Iterator, std::vector<int>()> b_rule;
> > qi::rule<Iterator, std::vector<int>()> c_rule;
> >
> I think I tried something like that, but it didn't work. Unless I'm
> mistaken, each of those rules will create their own std::vector<int>
> instance, so the values will not end up inserted into the vector specified
> in the grammar parameter.

No, it should work. The vector attribute is propagated to the alternatives, where it is filled.

> I was able to eventually achieve my goal by specifying each of the rules
> like so:
>
> qi::rule<Iterator, std::vector<int> &> a_rule;
>
> and in the main grammar I passed the _val into them.
>
> start = a_rule(_val) | b_rule(_val) | c_rule(_val)
>
> Then I referenced the arguments with _r1 in the sub-rules.
>
> Does that look correct? Is there a better way?

That should work as well. This might be a tick better in terms of performance, even. But I'm guessing here, I have not done any measurements.

Regards Hartmut
---------------
http://boost-spirit.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net