9 Feb 23:01
Re: Spirit 2.1 - Question about rules and phoenix
Chris Hoeppler <hoeppler <at> gmx.net>
2010-02-09 22:01:04 GMT
2010-02-09 22:01:04 GMT
> I am rewriting a parser for algebraic expression from the old spirit
> classic to spirit QI.
<snip />
> 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_
> )
> ;
>
> 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).
Did you have a look at boost/libs/spirit/example/qi/calc3.cpp ?
If you really want to split the rules as shown above, you'd have pass
the current value of Expression to PlusMinus. See attached.
HTH,
Chris
------------------------------------------------------------------------------ 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
_______________________________________________ Spirit-general mailing list Spirit-general <at> lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/spirit-general
RSS Feed