Find function names from C

B

Berlin Brown

I am parsing a C file and want to find the function names, is this
python worthy or is Emacs-Lisp better for this. I have searched for
some libraries but coming up null. I just need the function names, no
args or anything else. I would probably have to run a regular
expression pattern looking for int,void,static, etc and then the
enclosing { }.
 
J

Josiah Carlson

Berlin said:
I am parsing a C file and want to find the function names, is this
python worthy or is Emacs-Lisp better for this. I have searched for
some libraries but coming up null. I just need the function names, no
args or anything else. I would probably have to run a regular
expression pattern looking for int,void,static, etc and then the
enclosing { }.

That sounds like something a recursive descent (sp?) parser could do
very well (or really any other programming language parser). In
general, a regular expression would not be sufficient, due to nested
blocks of {} like the following:

int main () {
if (1) {
return 4;
} else {
return 3;
}
return 2;
}

FYI: regular expressions are those languages that can be recognized by
finite state automata. Because most modern programming languages are
context-free grammars, they are not recognized by regular expressions
and thusly finite state automata.

I would be willing to bet that someone has already implemented a parser
for C/C++ in Python. If not, talk to someone taking a compilers class,
they'll be able to help you.

- Josiah
 
J

Jey Kottalam

Berlin Brown said:
I am parsing a C file and want to find the function names, is this
python worthy or is Emacs-Lisp better for this. I have searched for
some libraries but coming up null. I just need the function names, no
args or anything else. I would probably have to run a regular
expression pattern looking for int,void,static, etc and then the
enclosing { }.

Look into www.gcc-xml.org or there's an ANSI-C parser at
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html that could easily be
adapted for your needs. (Yes, that's with C and yacc, not python. sorry.)

-Jey Kottalam
 
C

Chun-Chieh Huang

Berlin Brown said:
I have to go back to the books, dont know python or lisp very well,
yacc,lex, for one thing, that is exactly what lisp does in emacs
anyway, the way it matches one { with another }

Well, how about etags in Emacs or ctags in vi? I don't know if this is
exactly what you want, but typing "etags *.c" will generate a "TAGS"
file, which contains all function names in those c source files. And
then you can use Emacs to do function name searching. Take a look for
the functionality in Emacs, maybe you don't have to write it by your
own.

Good luck!

--
Chun-Chieh Huang, aka Albert E-mail: jjhuang AT cm.nctu.edu.tw
¶À«T³Ç
Department of Computer Science
National Tsing Hua University MIME/ASCII/PDF/PostScript are welcome!
HsinChu, Taiwan NO MS WORD DOC FILE, PLEASE!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top