Don't try to parse C with regexes
This is a valid C function, named test
, which takes a const pointer to void (named ptr) and returns a pointer to a function that takes an array of five pointer to functions which return an int and returns an unsigned int.
unsigned int (*(test)(const void *ptr)) (int (*[5])())
{
return 0;
}
(bonus points if someone can find a real-world scenario where this thing could have any use)
Although deprecated, you may also come in contact with the "old style" function notation:
// declaration
unsigned int test2();
// definition
unsigned int test2(ptr)
const void *ptr;
{
return 0;
}
Intermixed in this you can find comments (both multi-line and single-line since C99), trigraphs and even macros:
#define defun(fn) fn (
#define fstart ){
#define fend }
void defun(test3) int a, double b
fstart
printf("%d %f", a, b);
fend
http://ideone.com/JDDeMr
Even excluding the pathological macro scenario, "plain" regexes cannot even start to parse this thing because they can't match parentheses; maybe you can do something with extended regexes, but let's be honest, do you really want to cope with this stuff? Use a ready-made parser or even a compiler (libclang
comes to mind) and let it do the dirty work.