mlx1982 | 9 Feb 22:13
Picon
Picon
Favicon

Spirit 2.1 - Question about rules and phoenix


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_
            )
        ;

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

-- 
View this message in context: http://old.nabble.com/Spirit-2.1---Question-about-rules-and-phoenix-tp27520366p27520366.html
Sent from the spirit-general mailing list archive at Nabble.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