用boost精神解析布尔表达式

时间:2012-09-11 06:42:00

标签: c++ parsing boost

我正在尝试使用boost spirit为布尔表达式编写解析器。我在stackoverflow.com上找到了一个很好的例子,Boolean expression (grammar) parser in c++,这帮助我理解了精神语法和解析过程的工作原理。

代码在我的dev机器上编译和运行完美,这是一个debian 6盒子,安装了2.6.32-5 bigmem内核和boost 1.42(精神2.2)。我决定为了我的目的修改代码,以便不仅解析形式为“a和b或c”的表达式,还解析“sc [100]和cc [200]或fd [300]”形式的表达式,其中布尔操作的操作数也需要解析,以便得到一个因为我需要它为我的项目。我选择了以下方法:

  • 添加另一种抽象数据类型

    struct op_or  {};
    struct op_and {};
    struct op_xor {};
    struct op_not {};
    struct r_sc  {};
    
    typedef std::string var;
    template <typename tag> struct binop;
    template <typename tag> struct unop;
    template <typename tag> struct rule;
    
    typedef boost::variant<
            var,
            boost::recursive_wrapper<unop<op_not>>,
            boost::recursive_wrapper<binop<op_and>>,
            boost::recursive_wrapper<binop<op_xor>>,
            boost::recursive_wrapper<binop<op_or>>,
            boost::recursive_wrapper<rule<r_sc>> > expr;
    
    template <typename tag> struct rule
    {
        explicit rule( const expr& lhs, const expr& rhs )
            :   oper1( lhs ),
                oper2( rhs )
        {
    
        }
    
        expr oper1, oper2;
    };
    
    
    
    template <typename tag> struct binop
    {
        explicit binop( const expr& lhs, const expr& rhs )
            :   oper1( lhs ),
                oper2( rhs )
        {
    
        }
    
        expr oper1, oper2;
    };
    
    
    
    template <typename tag> struct unop
    {
        explicit unop( const expr& rhs )
            :   oper1( rhs )
        {
    
        }
    
        expr oper1;
    };
    
  • 修改访客以打印ast

    struct printer : boost::static_visitor<void>
    {
        printer( std::ostream& outputStream )
            :   _outputStream( outputStream )
        {
    
        }
    
        std::ostream& _outputStream;
    
        //
        void operator()( const var& variable ) const
        {
            _outputStream << variable;
        }
    
        void operator()( const binop<op_and>& binaryOp ) const
        {
             printOp( " & ", binaryOp.oper1, binaryOp.oper2 );
        }
    
        void operator()( const binop<op_or>& binaryOp ) const
        { 
            printOp( " | ", binaryOp.oper1, binaryOp.oper2 );
        }
    
        void operator()( const binop<op_xor>& binaryOp ) const
        {
            printOp( " ^ ", binaryOp.oper1, binaryOp.oper2 );
        }
    
    void printOp( const std::string& operation, const expr& lhs, const expr& rhs ) const
    {
        _outputStream << "(";
            boost::apply_visitor( *this, lhs );
            _outputStream << operation;
            boost::apply_visitor( *this, rhs );
        _outputStream << ")";
    }
    
    void operator()( const unop<op_not>& uaryOp ) const
    {
        _outputStream << "(";
        _outputStream << "!";
            boost::apply_visitor( *this, uaryOp.oper1 );
        _outputStream << ")";
    }
    
    void operator()( const rule<r_sc>& rule ) const
    {
        printRule( " serviceCode ", rule.oper1, rule.oper2 );
    }
    
    void printRule( const std::string& rule, const expr& lhs, const expr& rhs ) const
    {
        _outputStream << "{";
            boost::apply_visitor( *this, lhs );
            _outputStream << rule;
            boost::apply_visitor( *this, rhs );
        _outputStream << "}";
    }
    };
    
  • 修改语法规则以生成规则对象

    template <typename It, typename Skipper = qi::space_type>
    struct parser : qi::grammar<It, expr(), Skipper>
    {
        parser()
            : parser::base_type( _expr )
        {
            using namespace qi;
            using boost::spirit::ascii::string;
    
            _expr  = _or.alias();
    
            _or = ( _xor >> "or" >> _or )[ _val = phx::construct<binop<op_or> >( _1, _2 ) ] |
                _xor   [ _val = _1 ];
    
            _xor = ( _and >> "xor" >> _xor )[ _val = phx::construct<binop<op_xor> >( _1, _2 ) ] |
                _and[ _val = _1 ];
    
            _and = ( _not >> "and" >> _and )[ _val = phx::construct<binop<op_and> >( _1, _2 ) ] |
                _not[ _val = _1 ];
    
            _not = ( "not" > _base )[ _val = phx::construct<unop<op_not> >( _1 ) ] |
                _base[ _val = _1 ];
    
            _base = ( ( '(' > _expr > ')' ) | _serviceCode[ _val = _1 ] );
    
            _serviceCode = ( +alnum >> "[" >> +alnum >> "]" )
                [ _val = phx::construct<rule<r_sc> >( _1, _2 ) ] |
                    _text;
    
            _text = qi::lexeme[ +( alnum ) ];
        }
    private:
    
            qi::rule<It, var() , Skipper> _text;
            qi::rule<It, expr(), Skipper> _not, _and, _xor, _or, _base, _expr, _serviceCode;
    };
    

我以为就是这样,但这个解决方案甚至都没有编译。编译器发出以下错误:

  

错误:模板参数列表中的解析错误   错误:与‘operator>’

中的‘construct<<expression error> > > (boost::spirit::_1, boost::spirit::_2)’不匹配

由于我很擅长提升精神,有人可以给我一个提示我做错了吗?

0 个答案:

没有答案