Regex to parse C/C++ functions declarations

前端 未结 3 1262
夕颜
夕颜 2020-12-18 07:36

I need to parse and split C and C++ functions into the main components (return type, function name/class and method, parameters, etc).

I\'m working from either heade

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-18 08:03

    Take a look at Boost.Spirit, it is a boost library that allows the implementation of recursive descent parsers using only C++ code and no preprocessors. You have to specify a BNF Grammar, and then pass a string for it to parse. You can even generate an Abstract-Syntax Tree (AST), which is useful to process the parsed data.

    The BNF specification looks like for a list of integers or words separated might look like :

    using spirit::alpha_p;
    using spirit::digit_p;
    using spirit::anychar_p;
    using spirit::end_p;
    using spirit::space_p;
    
    // Inside the definition...
    integer    = +digit_p;                      // One or more digits.
    word       = +alpha_p;                      // One or more letters.
    token      = integer | word;                // An integer or a word.
    token_list = token >> *(+space_p >> token)  // A token, followed by 0 or more tokens.
    

    For more information refer to the documentation, the library is a bit complex at the beginning, but then it gets easier to use (and more powerful).

提交回复
热议问题