I am trying to create a parser that can parse C code. My use case to parse a buffer that may contain a function prototype. I want to push this function name into a symbol table. I am new to Spirit and PEG and I am trying to figure out how I can write a rule that can identify function prototypes.
This is my current implementation:
auto nameRule = x3::alpha >> *x3::alnum;
auto fcnPrototypeRule = nameRule >> *nameRule;
auto fcnRule = fcnPrototypeRule >> space >> x3::char_('(') >> -(nameRule % ',') >> x3::char_(');');
This is my application code:
class Parser {
public:
std::string functionParser(const std::string& input) {
std::string output;
x3::phrase_parse(input.begin(), input.end(), fcnRule, space, output);
return output;
}
};
input is = "extern void myFunction();" The output is an empty string. I wanted to get the function prototype.