Sophie

Sophie

distrib > Mandriva > 9.1 > i586 > by-pkgid > b9ba69a436161613d8fb030c8c726a8e > files > 638

spirit-1.5.1-2mdk.noarch.rpm

#ifndef SPIRIT_TREE_CALC_GRAMMAR_HPP_
#define SPIRIT_TREE_CALC_GRAMMAR_HPP_

using namespace spirit;

////////////////////////////////////////////////////////////////////////////
//
//  Our calculator grammar
//
////////////////////////////////////////////////////////////////////////////
struct calculator : public grammar<calculator>
{
    static const int integerID = 1;
    static const int factorID = 2;
    static const int termID = 3;
    static const int expressionID = 4;
    template <typename ScannerT>
    struct definition
    {
        definition(calculator const& /*self*/)
        {
            //  Start grammar definition
            integer     =   leaf_node_d[ lexeme_d[ 
                                (!ch_p('-') >> +digit_p) 
                            ] ];

            factor      =   integer
                        |   inner_node_d[ch_p('(') >> expression >> ch_p(')')]
                        |   (root_node_d[ch_p('-')] >> factor);

            term        =   factor >>
                            *(  (root_node_d[ch_p('*')] >> factor)
                              | (root_node_d[ch_p('/')] >> factor)
                            );

            expression  =   term >>
                            *(  (root_node_d[ch_p('+')] >> term)
                              | (root_node_d[ch_p('-')] >> term)
                            );
            //  End grammar definition
            
            // set the rule id's so we can use them to process the tree
            integer.set_id(integerID);
            factor.set_id(factorID);
            term.set_id(termID);
            expression.set_id(expressionID);

            // turn on the debugging info.
            SPIRIT_DEBUG_RULE(integer);
            SPIRIT_DEBUG_RULE(factor);
            SPIRIT_DEBUG_RULE(term);
            SPIRIT_DEBUG_RULE(expression);
        }

        rule<ScannerT> expression, term, factor, integer;

        rule<ScannerT> const&
        start() const { return expression; }
    };
};



#endif