c++ - 获取迭代次数?

标签 c++ boost-spirit-qi boost-phoenix

我想知道是否有一种方法可以计算特定语法发生的迭代次数。有效地计算函数的参数数量。

这是使用 boost spirit 库来解析我自己的语法,我正在尝试获取解析器如何使用列表运算符 % 找到的参数数量。

// _1 is string of function, is there a "_1" equivalent to get number of exprs
function_call = (function_name > '(' > expr % ',' > ')')[add_call(_1, _? /* todo */)]; 
expr = function_call | variable;

最佳答案

您可以在规则中使用局部变量来跟踪子表达式的数量。

首先,您需要在function_call 的定义中指定局部变量:

qi::rule< Iterator, Attribute, SpaceType, qi::locals<int> > function_call;
                                          ^

然后在每次匹配子表达式时递增这样的局部变量:

function_call = (function_name > '(' > expr[qi::_a++] % ',' > ')')[add_call(_1, _a)]; 
                                            ^                                   ^

Here您会找到一个现场演示,其中包含以逗号分隔的整数列表。

关于c++ - 获取迭代次数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852118/

相关文章:

c++ - 如何在 QLineEdit 中使用 std::string?

c++ - Boost Karma 对象方法调用

c++ - Libnoise 不想编译 : Cannot find -lnoise

c++ - learncpp.com 适合初学者吗?

c++ - Win32 : Monitoring for files being created or changed

boost-spirit - Boost Spirit X3 量产准备好了吗?

c++ - 提升精神合成属性困惑

c++ - 未调用 boost spirit 语义 Action

c++ - 在 Boost Phoenix 表达式中转换函数体