Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
taking c++ code as an input and extract names of functions along with their argument list in that code . any suggestions
Posted
Comments
LittleYellowBird 27-Jul-11 6:24am    
Hi, is this homework? If it is (and actually even if it isn't) have a go at it and come back with a specific problem, it doesn't sound too tough to me. Best of Luck. :)
Stefan_Lang 27-Jul-11 8:33am    
Your input may be C++ source code, but C/C++ is not well suited for text based processing, other languages such as Perl, Python, AWK, etc. may be better suited to solve this. My list may not be very representative as I've never personally done such a program, these are just a languages I know other people prefer using for text processing.
Emilio Garavaglia 27-Jul-11 9:08am    
C++ "crude and naked" no, but with some support like boost::spirit may be easy.

I guess you want to do some project.

For doing so you need to tokenize the string and parse it using
your parsing logic.

For tokenizing you need to know regarding regular expression and how to use them using C.

This link can be helpfull for you
http://www.peope.net/old/regex.html[^]
 
Share this answer
 
Comments
Guyverthree 27-Jul-11 7:26am    
I agree that regular expressions are most likely the way to solve this problem.
Yes, you need to tokenize.

For a do-it-yourself approach:

Tokenize into character/digit strings (identifiers or literal constants) and punctuation (separators, operators). Discard white spaces, including comments. Do not forget operators that occur as pairs like ++, >= ...

Try and find the declarations. For this, you will need to split the whole file into statements (token sequences terminated by a colon ; ) and function definitions (token sequences terminated by a block {...}). Make sure to match the braces in pairs to obtain full blocks. You can ignore the block content.

Now among the remaining definitions, you should find those having a pair of parenthesis (). These are the function declarations (unless they appear at the right of an equal sign, meaning that they are initializer calls).

Parsing the complete function signature (attributes, return type, argument types and argument names) shouldn't be such a big deal, checking for , separators. Always make sure to match internal quotes, brackets, parenthesis or angle bracket in pairs.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900