I was wondering if there is a way to count the number of iterations that happens for a specific grammar. Effectively counting how many parameters there would be for a function.
This is using the boost spirit library for parsing my own syntax, i am trying to get how the number of parameters the parser finds using the list operator %.
// _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;
You can use a local variable in your rule to keep track of the number of sub expressions.
First of all you need to specify the local variables in your definition of function_call
:
qi::rule< Iterator, Attribute, SpaceType, qi::locals<int> > function_call;
^
Then increment such local variable every time you match a sub expression:
function_call = (function_name > '(' > expr[qi::_a++] % ',' > ')')[add_call(_1, _a)];
^ ^
Here you'll find a live demo with a comma separated list of integers.