If I need to make a program that can count my total number of functions from a file and take all of those functions, list its parameters, type, and name, how would I go about doing so? For example:
1 2
int example_function(int a);
int example_function2(int b) const;
Should output
Function Type Parameters
_______________________________________________
example_function int int a
example_function2 const int int b
I'm not really sure where to go with this. I was thinking of using some form of a vector of strings, but I'm not sure how to read the input in a way where that would be possible.
They are all going to be one line declarations. I may have to account for friend functions but for this assignment, things are kept simple as I haven't gotten into function complexity yet. The key things are that I need to be able to read the one-line functions and successfully identify the parameters, name, and type then copy them all in the order from the example.
As for libraries I am currently allowed to use:
<iostream>
<iomanip>
<cstdlib>
<cstdio>
<cmath>
<string>
<vector>
<fstream>
If it's all on one line, then that reduces the parsing difficulty by a lot.
(Assuming there isn't comments):
- You need to parse every token until you reach the left-parentheses (.
- The last token parsed will be the function name.
- The combination of tokens parsed before the function name will be the return type.
Inside the parentheses, you would need to delimit the results by each argument (result + name) being separated by commas. Like the function name, the token before the comma will be the name of the variable, and the other tokens before the name are the variable's type.
...of course, this becomes harder to parse if you start to do fancy things like pass in function pointers as parameters. And C/C++ allow redundant parentheses in many places, which also complicates parsing. But if you're just going off the examples in your first post, it isn't too bad.
As you create this, I suggest having simple test case strings ranging from less complex to more complex.