gpt4 book ai didi

c++ - Boost spirit x3 解析为结构,如果它为空则跳过成员

转载 作者:行者123 更新时间:2023-11-30 03:20:38 25 4
gpt4 key购买 nike

我正在尝试找出解析以下文本的方法

function() {body ...}
function(args_) {body...}

我应该对两个变体使用相同的结构还是只用一个结构就可以完成

struct function
{
std::string name_;
std::vector<std::string> args_;
statement_list body_;
};

我现在解析它的方式(如果没有参数,如何跳过参数):

auto const identifier_def = raw[lexeme[(alpha | '_') >> *(alnum | '_')]];
auto const function_def =
lexeme["function" >> !(alnum | '_')] >> identifier
>> '(' >> ((identifier % ',') )>> ')'
>> '{' >> statement >> '}'
;

我可以解析带参数的变体,但不能解析没有参数的变体!

我正在尝试使用 OR 运算符之类的东西,但没有成功。

谢谢!

最佳答案

作为一个快速提示,通常这是可行的:

 >> '(' >> -(identifier % ',') >> ')'

根据具体的类型(尤其是 identifier 的声明),你可能会有这样的调整:

 >> '(' >> (-identifier % ',') >> ')'

强制执行的想法:

 x3::rule<struct arglist_, std::vector<std::string> > { "arglist" }
= '(' >> (
identifier % ','
| x3::attr(std::vector<std::string> ())
)
>> ')';

关于c++ - Boost spirit x3 解析为结构,如果它为空则跳过成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52750720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com