Carl Barron | 9 Feb 22:58

Re: Spirit 2.1 - Question about rules and phoenix


On Feb 9, 2010, at 4:13 PM, mlx1982 wrote:

> 
> Hi,
> 
> I am rewriting a parser for algebraic expression from the old spirit classic
> to spirit QI.
> 
> I have a problem with the new syntax for the grammar. 
> 
> The following code is working:
> 
> 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] >> *( ( 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,  Element;
> }
> 
> However I need to split the rule Expression to insert other parts. Doing
> this in the following way my code is no more working (it compiles but the
> result is wrong):
> 
> 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_
>            )
>        ;

   well   x=0; x+=y  ,x contains y,   That is what your actions are doing.

easiest solution is to define an expression in terms of both + and - operators, since you are
just computing the expression.  Something like:

	expr =  term [ql::_val=qi::_1]
		>>  *( '+' >> term[ qi::_val += qi::_1]
		     | '-' >> term [qi::_val -= qi::_1]
		);

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