Regex to pull out C function prototype declarations?

前端 未结 8 646
星月不相逢
星月不相逢 2020-11-30 04:48

I\'m somewhere on the learning curve when it comes to regular expressions, and I need to use them to automatically modify function prototypes in a bunch of C headers. Does

8条回答
  •  醉话见心
    2020-11-30 05:19

    To do this properly, you'll need to parse according to the C language grammar. But if this is for the C language only and for header files only, perhaps you can take some shortcuts and get by without full blown BNF.

    ^
    \s*
    (unsigned|signed)?
    \s+
    (void|int|char|short|long|float|double)  # return type
    \s+
    (\w+)                                    # function name
    \s*
    \(
    [^)]*                                    # args - total cop out
    \)
    \s*
    ;
    

    This is by no means correct, and needs work. But it could represent a starting point, if you're willing to put in some effort and improve it. It can be broken by function definitions that span lines, function pointer argument, MACROS and probably many other things.

    Note that BNF can be converted to a regex. It will be a big, complex regex, but it's doable.

提交回复
热议问题