If we use a regular expression to match an enumeration, rather than use it to parse the enumeration. I think it's possible. try the following steps:
step1. make sure the C / C ++ source code can be compiled successfully.
step 2. remove all comments from the source code C / C ++.
step 3. match enum
working Ruby example code:
# copy from Mastering Regular Expression 3rd COMMENT = '/\*[^\*]*\*+(?:[^/*][^*]*\*+)*/' COMMENT2 = '//[^\n]+' DOUBLE = '"(?:\\.|[^\\"])*"' SINGLE = '\'(?:\\.|[^\\\'])*\'' # pattern for match enum ENUM = '\benum\s*(\w+)\s*\{(\s*\w+(?:\s*=\s*\w+)?(?:\s*,\s*\w+(?:\s*=\s*\w+)?)*)\s*(?:,\s*)?\}\s*\w+\s*;' foo = File.open("foo.cpp", "r").read() # strip all comments from foo.cpp foo.gsub!(/(#{DOUBLE}|#{SINGLE})|#{COMMENT}|#{COMMENT2}/, '\1') bar = [] # match enum... foo.scan(/#{ENUM}/) do | m | printf("%s: %s\n", m[0], m[1].gsub(/\s/, '')) end
output:
Temperature: C=0,F=1,R,K Depth: m=0,ft=1
source share