2

一天中的好时光!

我写了这段代码,并希望它能够被编译。

#include <string>

#include <boost/spirit/include/qi.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/variant/recursive_variant.hpp>
#include <boost/fusion/include/adapted.hpp>

struct ParsedThunk
{
    typedef boost::variant<std::string, boost::tuple<boost::recursive_wrapper<ParsedThunk>, boost::recursive_wrapper<ParsedThunk> > > ParsedThunkEntity;
    ParsedThunkEntity _entity;
};

BOOST_FUSION_ADAPT_STRUCT(
    ParsedThunk,
    (ParsedThunk::ParsedThunkEntity, _entity)
)

class ThunkParser
{
protected:
    boost::spirit::qi::rule<std::string::iterator, ParsedThunk(), boost::spirit::ascii::space_type> _thunkRule;

public:
    ThunkParser()
    {
        _thunkRule %= (
            (boost::spirit::qi::lexeme[+(boost::spirit::qi::char_ - boost::spirit::qi::char_(" =()"))]) | 
            (boost::spirit::qi::lit("(") >> _thunkRule >> _thunkRule >> boost::spirit::qi::lit(")")) | 
            (_thunkRule >> _thunkRule)
            ) >> boost::spirit::qi::eps;
    }
};

int main(void)
{
    return 0;
}

但是当我用 clang 或 GCC 编译它时,我得到一个错误。其中最有趣的部分是:

/usr/include/boost/variant/detail/initializer.hpp:89:24: note: candidate function not viable: no known conversion from 'const boost::fusion::vector2<ParsedThunk, ParsedThunk>' to 'const
      boost::tuples::tuple<boost::recursive_wrapper<ParsedThunk>, boost::recursive_wrapper<ParsedThunk>, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type,
      boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>' for 2nd argument;
            static int initialize(void* dest, param_T operand)

我使用 clang 3.1 或 gcc 4.7 和 boost 1.52,但我认为没关系。:)

我应该如何编辑这段代码以使其被编译?

更新

好吧,我刚刚注意到我指定的语法是左递归的,因此递归下降解析器在处理输入时可能不会停止。这可能是编译问题的原因吗?

4

1 回答 1

3

修复您的变体类型:

typedef boost::variant<
            std::string,
            boost::recursive_wrapper<boost::tuple<ParsedThunk, ParsedThunk> >
        > ParsedThunkEntity;
于 2012-12-07T09:08:37.030 回答