LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-21-2007, 09:45 PM   #1
judgex
LQ Newbie
 
Registered: Jan 2006
Location: Australia
Distribution: Fedora Core 4 and Cygwin
Posts: 14

Rep: Reputation: 0
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.
 
Old 09-22-2007, 07:19 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
What is the input? C source code file? or an binary executable file?
 
Old 09-22-2007, 11:21 AM   #3
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,147

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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.
 
Old 09-22-2007, 12:46 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 69
Quote:
Originally Posted by PTrenholme View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace variable with user defined variable ce124 Programming 10 04-13-2007 09:29 AM
Using Flex and Bison to parse the variable declaration section in Pascal unreal07 Programming 1 03-10-2007 08:25 PM
setting a variable variable in a script... this works, but could it be more elegant? pwc101 Programming 3 08-18-2006 11:23 AM
Scripting: accessing a variable stored in a variable? tomolesonjr Linux - Newbie 5 05-05-2006 08:47 PM
the c declaration kapsikum Programming 3 04-06-2005 02:12 AM


All times are GMT -5. The time now is 11:18 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration