C++ 使用spirit x3解析后跟字符串列表的字符串列表
我正在尝试使用boost spirit x3将字符串解析为结构:C++ 使用spirit x3解析后跟字符串列表的字符串列表,c++,parsing,c++14,boost-spirit,boost-spirit-x3,C++,Parsing,C++14,Boost Spirit,Boost Spirit X3,我正在尝试使用boost spirit x3将字符串解析为结构: struct identifier { std::vector<std::string> namespaces; std::vector<std::string> classes; std::string identifier; }; 我的解析器规则如下所示 auto const nested_identifier_def = x3::lexeme[
struct identifier {
std::vector<std::string> namespaces;
std::vector<std::string> classes;
std::string identifier;
};
我的解析器规则如下所示
auto const nested_identifier_def =
x3::lexeme[
-(id_string % "::")
>> -(id_string % ".")
>> id_string
];
其中id\u string
解析alphanum
的组合。
我知道这条规则不能按我所希望的那样进行解析,因为在解析foo.bar
时,例如规则的这一部分-(id\u string%”)
会消耗整个字符串。
如何更改规则以在结构中正确解析?假设您的
id\u字符串是这样的:
foo::bar::baz.bla.blub
foo.bar
boo::bar
foo
auto const id_string = x3::rule<struct id_string_tag, std::string>{} =
x3::lexeme[
(x3::alpha | '_')
>> *(x3::alnum | '_')
];
问题在于,p%different
是p>>*(different>>p)
的简写,即它总是在分隔符后消耗一个p
。但是,您需要的是*(p>>定界)
,这样就不会在定界符之后使用p
,而是留给下一个规则使用
auto const nested_identifier_def =
*(id_string >> "::")
>> *(id_string >> '.')
>> id_string;