Parsing C header files with python

Paul McGuire ptmcg at austin.rr._bogus_.com
Sat Aug 21 21:38:31 CEST 2004


"Ian McConnell" <ian at emit.demon.co.uk> wrote in message
news:873c2gzftf.fsf at emit.demon.co.uk...
> I've got a header file which lists a whole load of C functions of the form
>
>   int func1(float *arr, int len, double arg1);
>   int func2(float **arr, float *arr2, int len, double arg1, double arg2);
>
> It's a numerical library so all functions return an int and accept varying
> combinations of float pointers, ints and doubles.
>

If regexp's give you pause, try this pyparsing example.  It makes heavy use
of setting results names, so that the parsed tokens can be easily retrieved
from the results as if they were named attributes.

Download pyparsing at http://pyparsing.sourceforge.net.

-- Paul


------------------------
from pyparsing import *

testdata = """
  int func1(float *arr, int len, double arg1);
  int func2(float **arr, float *arr2, int len, double arg1, double arg2);
  """

ident = Word(alphas, alphanums + "_")
vartype = Combine( oneOf("float double int") + Optional(Word("*")), adjacent
= False)
arglist = delimitedList( Group(vartype.setResultsName("type") +
                                ident.setResultsName("name")) )
functionCall = Literal("int") + ident.setResultsName("name") + \
                    "(" + arglist.setResultsName("args") + ")" + ";"

for fn,s,e in functionCall.scanString(testdata):
    print fn.name
    for a in fn.args:
        print " -", a.type, a.name

------------------------
gives the following output:

func1
 - float* arr
 - int len
 - double arg1
func2
 - float** arr
 - float* arr2
 - int len
 - double arg1
 - double arg2





More information about the Python-list mailing list