2012-09-25 78 views
2

我想簡單地在這個答案找到了精神解析器:布爾表達式使用Boost

要刪除xor操作和邏輯。我沒有看到怎麼辦呢,我試着用

expr_ = or_.alias(); 

or_ = (and__ >> "or" >> or_)[ _val = phx::construct<binop<op_or >>(_1, _2) ]  | and_ [ _val = _1 ]; 
and_ = (not_ >> "and" >> and_) [ _val = phx::construct<binop<op_and>>(_1, _2) ] | not_ [ _val = _1 ]; 
not_ = ("not" > simple  ) [ _val = phx::construct<unop <op_not>>(_1)  ] | simple [ _val = _1 ]; 

的規則定義,但我得到一個神祕的編譯器的消息我不明白:

test\test.cpp `boost::spirit::_2' cannot appear in a constant-expression 

如果任何人都可以擺脫了一些光線,我會很感激。

這裏是日誌:

compiler: Default compiler Building Makefile: 
    "D:\Dev-Cpp\test\Makefile.win" Executing make... make.exe -f 
    "D:\Dev-Cpp\test\Makefile.win" all g++.exe -c test.cpp -o test.o 
    -I"D:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"D:/Dev-Cpp/include/c++/3.4.2/backward" -I"D:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"D:/Dev-Cpp/include/c++/3.4.2" -I"D:/Dev-Cpp/include" 

    test.cpp: In constructor `parser<It, Skipper>::parser()': 
    test.cpp:76: error: `boost::spirit::_1' cannot appear in a 
    constant-expression test.cpp:76: error: a comma operator cannot 
    appear in a constant-expression test.cpp:76: error: 
    `boost::spirit::_2' cannot appear in a constant-expression 
    test.cpp:76: error: `>>' should be `> >' within a nested template 
    argument list 

    test.cpp:77: error: `boost::spirit::_1' cannot appear in a 
    constant-expression 

    test.cpp:77: error: a comma operator cannot appear in a 
    constant-expression test.cpp:77: error: `boost::spirit::_2' cannot 
    appear in a constant-expression test.cpp:77: error: `>>' should be `> 
    >' within a nested template argument list 

    test.cpp:78: error: `boost::spirit::_1' cannot appear in a 
    constant-expression 

    test.cpp:78: error: `>>' should be `> >' within a nested template 
    argument list 

    test.cpp: In function `int main()': test.cpp:98: error: expected 
    primary-expression before "auto" test.cpp:98: error: expected `;' 
    before "auto" 

    test.cpp:111: error: expected primary-expression before ')' token 
    test.cpp:111: error: expected `;' before ')' token test.cpp:113: 
    error: ISO C++ forbids declaration of `f' with no type test.cpp:113: 
    error: `begin' is not a member of `std' test.cpp:113: error: `input' 
    undeclared (first use this function) test.cpp:113: error: (Each 
    undeclared identifier is reported only once for each function it 
    appears in.) test.cpp:113: error: ISO C++ forbids declaration of `l' 
    with no type test.cpp:113: error: `end' is not a member of `std' 
    test.cpp:114: error: `f' cannot appear in a constant-expression 
    test.cpp:114: error: a function call cannot appear in a 
    constant-expression test.cpp:114: error: template argument 1 is 
    invalid test.cpp:114: error: invalid type in declaration before ';' 
    token 

    test.cpp:126: error: `f' cannot appear in a constant-expression 
    test.cpp:126: error: a function call cannot appear in a 
    constant-expression test.cpp:126: error: template argument 1 is 
    invalid test.cpp:126: error: ISO C++ forbids declaration of `e' with 
    no type 

    test.cpp:128: error: expected primary-expression before '(' token 
    test.cpp:128: error: `first' has not been declared test.cpp:128: 
    error: request for member of non-aggregate type before ',' token 
    test.cpp:128: error: `last' has not been declared test.cpp:128: 
    error: request for member of non-aggregate type before ')' token 

    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h: At 
    global scope: 
    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h: In 
    instantiation of `std::iterator_traits<int>': 
    D:/Dev-Cpp/include/boost/detail/iterator.hpp:83: instantiated from 
    `boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:509: instantiated from 
    `boost::InputIterator<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:545: instantiated from 
    `boost::ForwardIterator<int>' 
    D:/Dev-Cpp/include/boost/concept/detail/has_constraints.hpp:42: 
    instantiated from 
    `boost::concepts::not_satisfied<boost::ForwardIterator<int> >' 
    D:/Dev-Cpp/include/boost/mpl/if.hpp:67: instantiated from 
    `boost::mpl::if_<boost::concepts::not_satisfied<boost::ForwardIterator<int> 
    >, boost::concepts::constraint<boost::ForwardIterator<int> >, boost::concepts::requirement<boost::concepts::failed************boost::ForwardIterator<int>::************> 
    >' D:/Dev-Cpp/include/boost/concept/detail/general.hpp:56: instantiated from `boost::concepts::requirement_<void 
    (*)(boost::ForwardIterator<int>)>' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:149: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, boost::spirit::qi::skip_flag::enum_type, 
    Attr&) [with Iterator = int, Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 
    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:129: 
    error: `int' is not a class, struct, or union type 
    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:130: 
    error: `int' is not a class, struct, or union type 
    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:131: 
    error: `int' is not a class, struct, or union type 
    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:132: 
    error: `int' is not a class, struct, or union type 
    D:/Dev-Cpp/include/c++/3.4.2/bits/stl_iterator_base_types.h:133: 
    error: `int' is not a class, struct, or union type 
    D:/Dev-Cpp/include/boost/concept_check.hpp: In instantiation of 
    `boost::InputIterator<int>': 
    D:/Dev-Cpp/include/boost/concept_check.hpp:545: instantiated from 
    `boost::ForwardIterator<int>' 
    D:/Dev-Cpp/include/boost/concept/detail/has_constraints.hpp:42: 
    instantiated from 
    `boost::concepts::not_satisfied<boost::ForwardIterator<int> >' 
    D:/Dev-Cpp/include/boost/mpl/if.hpp:67: instantiated from 
    `boost::mpl::if_<boost::concepts::not_satisfied<boost::ForwardIterator<int> 
    >, boost::concepts::constraint<boost::ForwardIterator<int> >, boost::concepts::requirement<boost::concepts::failed************boost::ForwardIterator<int>::************> 
    >' D:/Dev-Cpp/include/boost/concept/detail/general.hpp:56: instantiated from `boost::concepts::requirement_<void 
    (*)(boost::ForwardIterator<int>)>' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:149: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, boost::spirit::qi::skip_flag::enum_type, 
    Attr&) [with Iterator = int, Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 

    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/boost/concept_check.hpp:509: error: no type named 
    `value_type' in `struct boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:510: error: no type named 
    `difference_type' in `struct boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:511: error: no type named 
    `reference' in `struct boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:512: error: no type named 
    `pointer' in `struct boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:513: error: no type named 
    `iterator_category' in `struct boost::detail::iterator_traits<int>' 

    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp: In function `bool 
    boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, 
    const Skipper&, boost::spirit::qi::skip_flag::enum_type, Attr&) [with 
    Iterator = int, Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_>]': 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 

    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:155: error: 
    conversion from 
    `mpl_::failed************(boost::spirit::qi::phrase_parse(Iterator&, 
    Iterator, const Expr&, const Skipper&, 
    boost::spirit::qi::skip_flag::enum_type, Attr&) [with Iterator = int, 
    Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, 
    boost::detail::variant::void_>]::error_invalid_expression::************)(bool)' 
    to non-scalar type `mpl_::assert< false>' requested 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:155: error: 
    enumerator value for `mpl_assertion_in_line_155' not integer constant 

    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 
    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:164: error: request 
    for member `parse' in `boost::spirit::compile [with Domain = 
    boost::spirit::qi::domain, Expr = bool](((const bool&)(+expr)))', 
    which is of non-class type `bool' 

    D:/Dev-Cpp/include/boost/concept_check.hpp: In destructor 
    `boost::ForwardIterator<TT>::~ForwardIterator() [with TT = int]': 
    D:/Dev-Cpp/include/boost/concept/detail/general.hpp:38: 
    instantiated from `static void 
    boost::concepts::requirement<boost::concepts::failed************Model::************>::failed() 
    [with Model = boost::ForwardIterator<int>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:149: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, boost::spirit::qi::skip_flag::enum_type, 
    Attr&) [with Iterator = int, Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 
    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/boost/concept_check.hpp:548: error: no type named 
    `iterator_category' in `struct boost::ForwardIterator<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:548: error: `failed' is 
    not a member of `<declaration error>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:553: error: no type named 
    `reference' in `struct boost::InputIterator<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:554: error: no type named 
    `reference' in `struct boost::InputIterator<int>' 

    D:/Dev-Cpp/include/boost/spirit/home/qi/char/char_parser.hpp: In 
    member function `bool boost::spirit::qi::char_parser<Derived, Char, 
    Attr>::parse(Iterator&, const Iterator&, Context&, const Skipper&, 
    Attribute&) const [with Iterator = int, Context = const 
    boost::spirit::unused_type, Skipper = boost::spirit::unused_type, 
    Attribute = const boost::spirit::unused_type, Derived = 
    boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, Char = char, Attr = 
    char]': D:/Dev-Cpp/include/boost/spirit/home/qi/skip_over.hpp:27: 
    instantiated from `void boost::spirit::qi::skip_over(Iterator&, const 
    Iterator&, const T&) [with Iterator = int, T = 
    boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:169: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, boost::spirit::qi::skip_flag::enum_type, 
    Attr&) [with Iterator = int, Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 
    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/boost/spirit/home/qi/char/char_parser.hpp:68: 
    error: invalid type argument of `unary *' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/char/char_parser.hpp:70: 
    error: invalid type argument of `unary *' 

    D:/Dev-Cpp/include/boost/concept_check.hpp: In destructor 
    `boost::InputIterator<TT>::~InputIterator() [with TT = int]': 
    D:/Dev-Cpp/include/boost/concept_check.hpp:547: instantiated from 
    `boost::ForwardIterator<TT>::~ForwardIterator() [with TT = int]' 
    D:/Dev-Cpp/include/boost/concept/detail/general.hpp:38: 
    instantiated from `static void 
    boost::concepts::requirement<boost::concepts::failed************Model::************>::failed() 
    [with Model = boost::ForwardIterator<int>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:149: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, boost::spirit::qi::skip_flag::enum_type, 
    Attr&) [with Iterator = int, Expr = bool, Skipper = 
    boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, 
    boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, 
    boost::spirit::char_encoding::standard> >, 0l>, Attr = 
    boost::variant<var, boost::recursive_wrapper<unop<op_not> >, 
    boost::recursive_wrapper<binop<op_and> >, 
    boost::recursive_wrapper<binop<op_or> >, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_, 
    boost::detail::variant::void_, boost::detail::variant::void_>]' 
    D:/Dev-Cpp/include/boost/spirit/home/qi/parse.hpp:197: instantiated 
    from `bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const 
    Expr&, const Skipper&, Attr&) [with Iterator = int, Expr = bool, 
    Skipper = boost::spirit::standard::space_type, Attr = expr]' 
    test.cpp:119: instantiated from here 
    D:/Dev-Cpp/include/boost/concept_check.hpp:517: error: no type named 
    `difference_type' in `struct boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:517: error: `failed' is 
    not a member of `<declaration error>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:518: error: no type named 
    `iterator_category' in `struct boost::detail::iterator_traits<int>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:518: error: `failed' is 
    not a member of `<declaration error>' 
    D:/Dev-Cpp/include/boost/concept_check.hpp:521: error: invalid type 
    argument of `unary *' 

    make.exe: *** [test.o] Error 1 

    Execution terminated 
+0

我對這裏的佔位符是如何工作了解不多,但也許它是從錯誤的命名空間拉取_1和_2?您可以嘗試更具體一些,如phx :: _ 1或qi :: _ 1。 –

+0

使用'#include #include 編譯好g ++ 4.7.1和boost 1.51 #include #include #include #include '。它顯然不起作用,因爲xor_規則尚未實現。 – 2012-09-25 19:12:24

+0

Ohai CJAN.LEE :)我認識到了這個問題。你在那裏的評論中看過我的回答嗎? – sehe

回答

4

您不小心輸錯and_and__

and__更改爲and_將使其編譯。

這裏有一個更清潔的後續版本,刪除有關xor現已不需要聲明:

編輯又「回遷」老的編譯器不知道自動,decltype,和正確的模板解析(>>> >)等):

#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/spirit/include/phoenix_operator.hpp> 
#include <boost/variant/recursive_wrapper.hpp> 

namespace qi = boost::spirit::qi; 
namespace phx = boost::phoenix; 

struct op_or {}; 
struct op_and {}; 
struct op_not {}; 

typedef std::string var; 
template <typename tag> struct binop; 
template <typename tag> struct unop; 

typedef boost::variant<var, 
     boost::recursive_wrapper<unop <op_not> >, 
     boost::recursive_wrapper<binop<op_and> >, 
     boost::recursive_wrapper<binop<op_or> > 
     > expr; 

template <typename tag> struct binop 
{ 
    explicit binop(const expr& l, const expr& r) : oper1(l), oper2(r) { } 
    expr oper1, oper2; 
}; 

template <typename tag> struct unop 
{ 
    explicit unop(const expr& o) : oper1(o) { } 
    expr oper1; 
}; 

struct printer : boost::static_visitor<void> 
{ 
    printer(std::ostream& os) : _os(os) {} 
    std::ostream& _os; 

    // 
    void operator()(const var& v) const { _os << v; } 

    void operator()(const binop<op_and>& b) const { print(" & ", b.oper1, b.oper2); } 
    void operator()(const binop<op_or >& b) const { print(" | ", b.oper1, b.oper2); } 

    void print(const std::string& op, const expr& l, const expr& r) const 
    { 
     _os << "("; 
     boost::apply_visitor(*this, l); 
     _os << op; 
     boost::apply_visitor(*this, r); 
     _os << ")"; 
    } 

    void operator()(const unop<op_not>& u) const 
    { 
     _os << "("; 
     _os << "!"; 
     boost::apply_visitor(*this, u.oper1); 
     _os << ")"; 
    } 
}; 

std::ostream& operator<<(std::ostream& os, const expr& e) 
{ boost::apply_visitor(printer(os), e); return os; } 

    template <typename It, typename Skipper = qi::space_type> 
    struct parser : qi::grammar<It, expr(), Skipper> 
{ 
     parser() : parser::base_type(expr_) 
     { 
      using namespace qi; 

      expr_ = or_.alias(); 

      or_ = (and_ >> "or" >> or_) [ _val = phx::construct<binop<op_or > >(_1, _2) ] | and_ [ _val = _1 ]; 
      and_ = (not_ >> "and" >> and_) [ _val = phx::construct<binop<op_and> >(_1, _2) ] | not_ [ _val = _1 ]; 
      not_ = ("not" > simple  ) [ _val = phx::construct<unop <op_not> >(_1)  ] | simple [ _val = _1 ]; 

      simple = (('(' > expr_ > ')') | var_); 
      var_ = qi::lexeme[ +alpha ]; 

      BOOST_SPIRIT_DEBUG_NODE(expr_); 
      BOOST_SPIRIT_DEBUG_NODE(or_); 
      BOOST_SPIRIT_DEBUG_NODE(and_); 
      BOOST_SPIRIT_DEBUG_NODE(not_); 
      BOOST_SPIRIT_DEBUG_NODE(simple); 
      BOOST_SPIRIT_DEBUG_NODE(var_); 
     } 

     private: 
     qi::rule<It, var() , Skipper> var_; 
     qi::rule<It, expr(), Skipper> not_, and_, or_, simple, expr_; 
}; 

int main() 
{ 
    const std::string inputs[] = { 
     // From the OP: 
     std::string("(a and b) or ((c and d) or (a and b));"), 
     std::string("a and b or (c and d or a and b);"), 

     /// Simpler tests: 
     std::string("a and b;"), 
     std::string("a or b;"), 
     std::string("a or b;"), 
     std::string("not a;"), 
     std::string("not a and b;"), 
     std::string("not (a and b);"), 
     std::string("a or b or c;"), 
     std::string("") // marker 
    }; 

    for (const std::string *i = inputs; !i->empty(); ++i) 
    { 
     typedef std::string::const_iterator It; 
     It f(i->begin()), l(i->end()); 
     parser<It> p; 

     try 
     { 
      expr result; 
      bool ok = qi::phrase_parse(f,l,p > ';',qi::space,result); 

      if (!ok) 
       std::cerr << "invalid input\n"; 
      else 
       std::cout << "result: " << result << "\n"; 

     } catch (const qi::expectation_failure<It>& e) 
     { 
      std::cerr << "expectation_failure at '" << std::string(e.first, e.last) << "'\n"; 
     } 

     if (f!=l) std::cerr << "unparsed: '" << std::string(f,l) << "'\n"; 
    } 

    return 0; 
} 

在運行此示例的輸出是

result: ((a & b) | ((c & d) | (a & b))) 
result: ((a & b) | ((c & d) | (a & b))) 
result: (a & b) 
result: (a | b) 
result: (a | b) 
result: (!a) 
result: ((!a) & b) 
result: (!(a & b)) 
result: (a | (b | c)) 
+0

非常感謝您的幫助,但我仍然遇到問題,我給您提供日誌。太多..謝謝 –

+0

我真的想解決這個問題,非常感謝! –

+0

你好,我添加了日誌,謝謝! –