0

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;
user3901459
  • 824
  • 3
  • 11
  • 18
  • are you looking for varargs? valist, va_start(), va_end(), va_arg()? – Octopus Sep 15 '14 at 16:47
  • @Octopus This is for boost spirit qi parser. Trying to get the number of parameters the parser finds. Sorry if i didn't make that clear enough. – user3901459 Sep 15 '14 at 16:58

1 Answers1

1

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.

sbabbi
  • 11,070
  • 2
  • 29
  • 57