我正在尝试找出解析以下文本的方法
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/