Automatic Function Counting/Listing

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.
How much do you have to roll your own code, and how much can you make use of other tools to do the heavy work of parsing code.

http://ctags.sourceforge.net/
I essentially need to do it all on my own. I am not allowed to grab an outside program to do any of it for me
Last edited on
What else aren't you allowed to use?

Say
https://www.cplusplus.com/reference/regex/

What are the limits on the complexity of the functions you're supposed to scan for?

Are you expected to be able to code with say
1
2
3
4
int
example_function(
    int a
);


or
1
2
3
using MyInt = int;
MyInt example_function
    (MyInt a);


or any other of myriad ways of making just the simple act of parsing the code a lot more of a challenge.

Or do you just assume as you've posted, that the entire declaration will be nicely formatted on a single line, with no surprise gotcha's.
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.
Last edited on
Using getline() and various forms of string.find seems to be the way to go then.
After a bit of brainstorming:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct Parameter
{
  string name;
  string value;
};

struct Function
{
  string name;
  string return_type;
  vector<Parameter> arguments;
};

Function parse(const string line)
{
  // your code
}


Having a struct to store the different parts should make testing and output easier.
Topic archived. No new replies allowed.