By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,470 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,470 IT Pros & Developers. It's quick & easy.

Count number of functions in C program

P: 13
Hello Everybody

I dont have any idea of how to count the number of functions in C program. Help me out... Thanks

Navneet
Dec 31 '08 #1
Share this Question
Share on Google+
9 Replies


Expert 10K+
P: 11,448
@nav010
Use a C parser and increment a counter every time you start parsing a C function definition. There are numerous parsers available on the net.

kind regards,

Jos
Dec 31 '08 #2

P: 13
I would be grateful if you can give an example or so...
Any helpful link.
Thanks for replying
Dec 31 '08 #3

Expert 10K+
P: 11,448
@nav010
The matter is too complicated to give you a small example: you have to find an ANSI C grammar and add you own particular code to the grammar rule that parses a function definition.

Google for "ANSI C grammar" and you'll find a lot of ANSI C grammars; imho the simplest approach is to use a yacc grammar and generate ordinary C code. (Jutta Degener has a nice grammar available (it's one of the links)).

Most likely you also want the lex grammar. If you've build your parser you can parse one translation unit but it has to be preprocessed already. You can use any other compliant C compiler for that purpose.

You can also take the other route and use ANTLR (fka PCCTS) for the job, ANTLR is an excellent parser generator and I'm about sure they have an LL(k) ANSI C grammar available too on their site (ANTLR Parser Generator). It can be a fun little project.

kind regards,

Jos
Dec 31 '08 #4

Expert 100+
P: 2,404
If you want a tool for your own use, then the simplest approach that works is fine. If this is a class assignment then there may be additional constraints that could force workable approaches to be unacceptable. What are your constraints?

For example, ...
1. Is it acceptable to use a third-party parser, or is the point of the exercise for you to write a C parser?
2. Is it acceptable to use the compiler to preprocess the input source file, or is the point of the exercise for you to write a C preprocessor?
3. What language(s) do you need to parse: C, C++, or both?
4. Is it acceptable to restrict your input source file so that it doesn't contain any typedefs? (Support for typedefs significantly complicates the C parser.)
5. Is it acceptable to restrict your input source file to not contain any function pointers?
6. etc.

I see a few basic strategies:
1. Run some other program that recognizes functions and then parse the output of that program to make the output look the way you want it.
2. Compile the input source file and then parse the resulting symbol table to find candidate function names. You may need to look back at the input source file to determine which candidates are truly function names.
3. Parse the input source file yourself.
Dec 31 '08 #5

Expert 10K+
P: 11,448
@donbock
4) Typedefs don't have anything to do with function definitions; besides that the lexical analyser just returns TYPE_OR_IDENTIFIER; it's an old trick;

5) Function pointers can only point to function definitions somewhere else; the OP just wants to count the definitions.

kind regards,

Jos
Dec 31 '08 #6

P: 13
JosAH is right, we just need to count the definitions.

I was busy earlier with some other work. I saw all the post today and see all the parser example and come back to this thread if there would be any problem.

Thanks to all for helping me out.

Nav
Jan 9 '09 #7

100+
P: 110
Gnu cflow will help.
Feb 19 '20 #8

P: 9
Using the GNU Compiler Connection we can do it by writing the following code :

Expand|Select|Wrap|Line Numbers
  1. $ nm elf_file | grep "T " | grep -v " _" | wc -l
We should compile it with the following code because GNU Compiler Connection can inline some functions even with optimizations disabled, so you should compile with --O0(double dash with Capital O and 0(zero)):

Expand|Select|Wrap|Line Numbers
  1. -O0 -fno-builtin -fno-inline-functions-called-once
5 Days Ago #9

P: 13
First, copy all of the text of every file that you use in your your program, which you want to check, to a text editor so that you can run your own parser on it.

See that
void foo()
and
bool boo()
and
int noo()

all end with () but so do a lot of other parts of the code,
so also look at the other end of the function.

void, bool, int.

Use this logic to write your own text parser that looks first for the first part "void" and "bool" and "int" and etc. whatever your use as the first word of your functions.

Then have your text parser look for the first occurrence of ")" after each of those words are found.

That should find things like
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )

and things like
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

and things a lot shorter.

Then have your text parser check for multiple occurrences of each deleting the second and further occurrences of each down to only one listing. If your compiler requires previous declarations, that checking should clean that up.

Then have the parser count the occurrences and tell you the result.

Then have the text parser save to a file for printing, or do something else with the result.

This should get you close. Maybe not exactly but close.
4 Days Ago #10

Post your reply

Sign in to post your reply or Sign up for a free account.