Parsing C header files with python

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Aug 23 03:02:13 CEST 2004


"Ian McConnell" <ian at emit.demon.co.uk> wrote in message
news:87wtzrnj86.fsf at emit.demon.co.uk...
> "Paul McGuire" <ptmcg at austin.rr._bogus_.com> writes:
>
<snip>
>
> Thanks. Your example with pyparsing was just what I was looking for. It
also
> copes very nicely with newlines and spacing in the header file.
>
Ian -

It is just at this kind of one-off parsing job that I think pyparsing really
shines.  I am sure that you could have accomplished this with regexp's, but
a) it would have taken at least a bit longer
b) it would have required more whitespace handline (such as function decls
that span linebreaks)
c) it would have been trickier to add other unanticipated changes (support
for other arg data types (such as char, long), embedded comments, etc.)

BTW, all it takes to make this grammar comment-immune is to add the
following statement before calling scanString():

functionCall.ignore( cStyleComment )

cStyleComment is predefined in the pyparsing module to recognize /* ... */
comments.  Adding this will properly handle (i.e., skip over) definitions
like:

/*
int commentedOutFunc(float arg1, float arg2);
*/

Try that with regexp's!

-- Paul





More information about the Python-list mailing list