C++ 使用boost spirit x3解析递归变量时出现问题

C++ 使用boost spirit x3解析递归变量时出现问题,c++,parsing,boost-spirit,boost-spirit-x3,C++,Parsing,Boost Spirit,Boost Spirit X3,我目前正在尝试使用boost spirit x3解析成一个x3::variant。 该变体看起来像: typedef x3::variant< nil, x3::forward_ast<LambdaType>, x3::forward_ast<ClassType> > Type typedef x3::variant< 无 x3::向前, x3::前向 >类型 其中LambdaType和ClassType如下所示: struct L

我目前正在尝试使用boost spirit x3解析成一个
x3::variant
。 该变体看起来像:

typedef x3::variant<
    nil,
    x3::forward_ast<LambdaType>,
    x3::forward_ast<ClassType>
> Type
typedef x3::variant<
无
x3::向前,
x3::前向
>类型
其中LambdaType和ClassType如下所示:

struct LambdaType {
        std::vector<Type> parameters_;
        Type return_type_;
    };

struct ClassType{
    std::vector<std::string> name_; 
   std::vector<Type> template_args_;
};
struct LambdaType{
std::向量参数;
返回类型\返回类型\返回类型;
};
结构类类型{
std::向量名;
std::向量模板参数;
};
如果我试图解析成一个类型或这些结构中的一个,我会得到一个编译器错误,它告诉我不能将
常量boost::spirit::x3::char_类
分配给
类型
, 我对此感到困惑,因为我从来都不想分配一个
常量boost::spirit::x3::char_类
。 我有一个实例,它有一个解析器,当您试图编译它时,它会显示问题和错误。 我花了一整天的时间试图解决这个问题,现在我不知道为什么这样做行不通。
对于这方面的任何帮助,我将不胜感激。

您将
x3::space
作为最后一个参数传递给
x3::parse
,因此它将把它绑定到解析器的属性

你当然不想那样

如果要传递跳过程序,请使用
x3::phrase\u parse

PS您的解析器还有一个问题:它在type/lambdaType中留下了递归。我认为这是因为您删除了标识符解析(
x3::string(“foo”)
),因此如果您将输入修改为
(foo,foo)=>foo

#define BOOST_SPIRIT_X3_DEBUG
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/home/x3/support/ast/variant.hpp>
#include <boost/fusion/include/adapt_struct.hpp>

namespace x3 = boost::spirit::x3;

namespace ast{
    struct LambdaType;
    struct ClassType;
    struct nil{};

    typedef x3::variant<
        nil,
        x3::forward_ast<LambdaType>,
        x3::forward_ast<ClassType>
    > Type;

    struct LambdaType {
        std::vector<Type> parameters_;
        Type return_type_;
    };

    struct ClassType{
        std::vector<std::string> name_; 
        std::vector<Type> template_args_;
    };

}

BOOST_FUSION_ADAPT_STRUCT(ast::LambdaType, parameters_, return_type_)
BOOST_FUSION_ADAPT_STRUCT(ast::ClassType, name_, template_args_)

namespace parser{
    typedef x3::rule<struct lambda_type_class, ast::LambdaType> lambda_type_type;
    typedef x3::rule<struct class_type_class,  ast::ClassType>  class_type_type;
    typedef x3::rule<struct type_class,        ast::Type>       type_type;

    const class_type_type  class_type  = "class_type";
    const lambda_type_type lambda_type = "lambda_type";
    const type_type        type_p      = "type";

    auto const type_p_def = class_type | lambda_type;

    auto const lambda_type_def =
        ("(" >> -(type_p%",") >> ")" >> "=>" >> type_p)
        | (x3::repeat(1)[type_p%","] >> "=>" >> type_p)
            ;

    auto const class_type_def =
            (x3::string("foo")%"::") >> -("<" >> type_p%"," >> ">")
            ;

    BOOST_SPIRIT_DEFINE(
            lambda_type,
            class_type,
            type_p
    )
}

int main()
{
    std::string input = "(foo, foo) => foo";
    x3::phrase_parse(input.begin(), input.end(), parser::type_p, x3::space);
}
#定义BOOST_SPIRIT_X3_调试
#包括
#包括
#包括
#包括
名称空间x3=boost::spirit::x3;
名称空间ast{
结构LambdaType;
结构类类型;
结构nil{};
typedefx3::variant<
无
x3::向前,
x3::前向
>类型;
结构LambdaType{
std::向量参数;
返回类型\返回类型\返回类型;
};
结构类类型{
std::向量名;
std::向量模板参数;
};
}
BOOST\u FUSION\u ADAPT\u STRUCT(ast::LambdaType、参数、返回类型)
BOOST\u FUSION\u ADAPT\u STRUCT(ast::类类型、名称、模板参数)
名称空间分析器{
typedef x3::规则lambda_type_type;
typedefx3::规则类\类型\类型;
typedef x3::规则类型_类型;
const class\u type\u type class\u type=“class\u type”;
常数lambda\u type\u type lambda\u type=“lambda\u type”;
const type\u type\u p=“type”;
auto const type_p_def=class_type|lambda_type;
自动常数λ型定义=
((“(“>>>-(类型p%,”>>”)“>>”=>“>>类型p)
|(x3::重复(1)[类型p%,“]>>”=>“>”>>类型p)
;
自动常数类类型定义=
(x3::字符串(“foo”)%::”>>-(“”)
;
提升精神(
lambda_型,
类(u型),,
类型p
)
}
int main()
{
std::string input=“(foo,foo)=>foo”;
x3::短语解析(input.begin(),input.end(),parser::type\u p,x3::space);
}
具有调试输出


(foo,foo)=>foo
(foo,foo)=>foo
(foo,foo)=>foo
foo,foo)=>foo
foo,foo)=>foo
,foo)=>foo
[f,o,o]],[]
,foo)=>foo
foo)=>foo
foo)=>foo
)=>foo
[f,o,o]],[]
)=>foo
福
福

又添加了一些提示和一个笑话,不是吗:(,我现在觉得很愚蠢,我在调试过去的3个小时都没有成功。非常感谢你打开我的眼睛我也认识到了左递归,这是我的问题,我以前从未处理过这个问题,我正在努力避免左递归