1
votes

I'm looking for the simpliest way, how to determine return type, arguments and function name from c header file written under C99.

it's my school project, which have to be written in Perl without any libs. So i got a few options, i can use the regular expression, but it's not applicable to the hardest function like folowing:

int * (* func(int * arg[]))();

the return type should be "int * (* )()" and argument is "int * []".

Second way is to use grammar and parse it, but i think, that this is not the right way.

My buddy told me about an existing algorithm which can do it. But he doesn't remember name, or where he saw him. The algorithm was quite simple. Something like: Find first end parenthesis, everything between this end parenthesis and the first-match previous start parenthesis is arguments...

Does anyone have some idea what am I looking for?

3
This is a parsing problem. Why do you feel that parsing is not the right approach? - Jon Purdy
@JonPrudy: Basic part of this header parser can be written by regular expressions to get 10/10 points. The advanced part can be awarded by 1 bonus point. So I think I do not have to write complicated parser to get 1 bonus point. But I could be wrong :D Cong Xu thanks for algorithm I was looking for, but as I can see, it's not exactly what I need. - l0v3

3 Answers

2
votes

If you can obtain The C Programming Language by Kernighan and Ritchie. Not only is it the C bible, but in chapter 5 they present code to parse C declarations. You can look there to see how they do it and quite possibly adapt their approach (chapter 5, section 12).

0
votes

You simply have to build a parser for that kind of problem. Usually the top-down approach (e.g. a recursive descent) would do it for this kind of job. Fortunately top-down parsers are more or less straight forward to implement.

The only hard bit in C like languages is, that these languages are usually at least LL1 (1 token look ahead) or even worse LL2 or more. So sometimes you have to peek a few tokens in advance to find out whether it's a function declaration or a function call for example.