LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Need to extract name of C variable from C declaration. (http://www.linuxquestions.org/questions/programming-9/need-to-extract-name-of-c-variable-from-c-declaration-586468/)

judgex 09-21-2007 10:45 PM

Need to extract name of C variable from C declaration.
 
Hi everyone,

I only want the name of the declared variable. Everything else is not important to me:

char x[50]; //ANS:x
int y; //ANS:y
char z; //ANS:z
int (*f)(double); //ANS:y
union un u; //ANS:u
struct x y; //ANS:y
struct w w[25]; //ANS:w
struct z z; //ANS:z
double c; //ANS:c

Can somebody give me some shell or C code or Perl which extracts the name of a C variable from an arbitrary input? The above are only examples.

Thanks.

Hko 09-22-2007 08:19 AM

What is the input? C source code file? or an binary executable file?

PTrenholme 09-22-2007 12:21 PM

Shouldn't the answer to your fourth entry (the function pointer) be "f," not "y?"

If you're parsing source code, awk is an easy language to use to do the parsing.

But, if you want to get a little more sophisticated, look at yacc to generate a C program to parse it all out for you, or, if you're masochistic, look at the parser in the g++ source code tree.

osor 09-22-2007 01:46 PM

Quote:

Originally Posted by PTrenholme (Post 2900214)
But, if you want to get a little more sophisticated, look at yacc to generate a C program to parse it all out for you, or, if you're masochistic, look at the parser in the g++ source code tree.

You might also take a look at ctags or something else that does the brunt of the work for you.


All times are GMT -5. The time now is 06:40 AM.