Parsing C header files with python
Paddy McCarthy
paddy3118 at netscape.net
Sun Aug 22 01:41:18 EDT 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.
>
> What's the easiest way breaking down this header file into a list of
> functions and their argument using python? Is there something that will
> parse this (Perhaps a protoize.py) ? I don't want (or understand!) a full C
> parser, just this simple case.
>
<<SNIP>>
>
> Thanks,
> Ian
Would this suffice:
<CODE>
>>> import re
>>> import pprint
>>> hdr=''' int func1(float *arr, int len, double arg1);
int func2(float **arr, float *arr2, int len, double arg1, double arg2);
'''
>>> print hdr
int func1(float *arr, int len, double arg1);
int func2(float **arr, float *arr2, int len, double arg1, double arg2);
>>> func2args = {}
>>> for line in hdr.split('\n'):
line = [word for word in re.split(r'[\s,;()]+', line) if word]
if len(line)>2:func2args[line[1]] = line[2:]
>>> pprint.pprint(func2args)
{'func1': ['float', '*arr', 'int', 'len', 'double', 'arg1'],
'func2': ['float',
'**arr',
'float',
'*arr2',
'int',
'len',
'double',
'arg1',
'double',
'arg2']}
>>>
</CODE>
More information about the Python-list
mailing list