Boost logo

Boost Users :

Subject: [Boost-users] Spriti parser for boolean expression
From: soumeng78 (soumen78_at_[hidden])
Date: 2012-08-09 06:29:30


Hi,

I'm using spirit first time. I'm trying to write a boolean expression (with
only &, | and ! operators) parser. I've defined my grammar like following:

    template <typename Iterator>
    struct boolean_expression_parser : qi::grammar<Iterator, std::string(),
ascii::space_type>
    {
        boolean_expression_parser() :
boolean_expression_parser::base_type(expr)
        {
            using namespace qi;
            using ascii::char_;
            using boost::spirit::ascii::alnum;
            using namespace qi::labels;

            using phoenix::construct;
            using phoenix::val;
        
            operand %= lexeme[+(alnum)];

            simple_expr %= ('(' > expr > ')') | operand;

            unary_expr %= ('!' > simple_expr ) ;

            and_expr %= ( expr > '*' > expr);

            or_expr %= (expr > '|' > expr);

            expr %= simple_expr | unary_expr | *and_expr | *or_expr;

            // on_error<fail>
            // (
            // unary_expr,
            // std::cout
            // << val("Error! Expecting ")
            // << _4 // what
failed?
            // << val(" here: \"")
            // << construct<std::string>(_3, _2) // iterators
to error-pos, end
            // << val("\"")
            // << std::endl
            // );
        }
        
        qi::rule<Iterator, std::string(), ascii::space_type> operand;
        qi::rule<Iterator, std::string(), ascii::space_type> simple_expr;
        qi::rule<Iterator, std::string(), ascii::space_type> unary_expr;
        qi::rule<Iterator, std::string(), ascii::space_type> and_expr;
        qi::rule<Iterator, std::string(), ascii::space_type> or_expr;
        qi::rule<Iterator, std::string(), ascii::space_type> expr;
    };

I'm facing few hurdles here:

1. It's not working for any binary expression (like 'A + B'). It's working
fine for unary expressions (like '!(A)' or '(!A)'.

Can someone point me what I'm doing wrong?

2. I want store it in tree form (as I want to build a BDD out of it). Can
someone point me how to do that?

3. Also, why on_error<> is not working even when I enable it?

I'm using boost 1.49 and gcc-4.2.2.

Regards,
~ Soumen

--
View this message in context: http://boost.2283326.n4.nabble.com/Spriti-parser-for-boolean-expression-tp4634170.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net