Michael Caisse | 9 Feb 22:55

Re: Spirit 2.1 - Question about rules and phoenix

mlx1982 wrote:
>
>
> template <typename iterator>
> struct SpiritCalculator : qi::grammar< iterator, std::vector<Real>(),
> ascii::space_type >
> {
>     SpiritCalculator() :
>         SpiritCalculator::base_type(Start)
>
>          Start =
>             (
>                 Expression % ','
>             )
>         ;
>
>         Expression =
>             (
>                 Element[qi::_val = qi::_1] >> *( PlusMinus[qi::_val =
> qi::_1] )
>             )
>         ;
>
>         PlusMinus =
>             (
>                 ( qi::lit('+') >> Element[qi::_val += qi::_1] ) |
>                 ( qi::lit('-') >> Element[qi::_val -= qi::_1] )
>             )
>         ;
>         Element =
>             (
>                   qi::double_
>             )
>         ;
>
>     qi::rule< iterator, Results_Type(), ascii::space_type > Start;
>     qi::rule< iterator, double(), ascii::space_type > Expression, 
> PlusMinus, Element;
> }
>
> The two codes should do the same thing. However if I pass a string
> "2+4,1+2+3" to the first one he correctly return ( 6, 6 ) while the second
> one return ( 4, 3 ), which are the last two elements.
>
> Can anyone tell me how to make the second one working. I need that kind of
> structure to build my parser (I need to have PlusMinus, MultiplyDivide, and
> other operators spitted from the Expression).
>
> Thanks,
>
>    Cristiano
>   

Hello Cristiano -

Take a look at your PlusMinus rule. The synthesized attribute is of type double.
The _val represents the rule's attribute. Entering the rule it is apparently 0. Some
double_ is parsed and the added to the initial value of 0. 

The Expression rule also has a synthesized attribute of a double. The semantic
actions are written so that its attribute is equal to whatever was last parsed,
which is exactly what you are seeing.

There a lot of ways to do what you want... take a look at inherited attributes
in the Spirit documentation to see if that helps you out.

regards -
michael

--

-- 

----------------------------------
Michael Caisse
Object Modeling Designs
www.objectmodelingdesigns.com

------------------------------------------------------------------------------
SOLARIS 10 is the OS for Data Centers - provides features such as DTrace,
Predictive Self Healing and Award Winning ZFS. Get Solaris 10 NOW
http://p.sf.net/sfu/solaris-dev2dev

Gmane