Is there any python library that parse c++ source code statically

Philip Semanchuk philip at semanchuk.com
Sun Mar 13 11:54:36 EDT 2011


On Mar 13, 2011, at 11:46 AM, Stefan Behnel wrote:

> Francesco Bochicchio, 13.03.2011 10:37:
>> On 13 Mar, 10:14, kuangye<kuangye19840... at gmail.com>  wrote:
>>> Hi, all. I need to generate other programming language source code
>>> from C++ source code for a project. To achieve this, the first step is
>>> to "understand" the c++ source code at least in formally. Thus is
>>> there any library to parse the C++ source code statically. So I can
>>> developer on this library.
>>> 
>>> Since the C++ source code is rather simple and regular. I think i can
>>> generate other language representation from C++ source code.
>> 
>> 
>> The problem is that C++ is a beast of a language and is not easy to
>> find full parsers for it.
>> I've never done it, but sometime I researched possible ways to do it.
>> The best idea I could come with
>> is doing it in 2 steps:
>> 
>>  - using gcc-xml ( http://www.gccxml.org/HTML/Index.html ) to generate
>> an xml representation of the code
>>  - using one of the many xml library for python to read the xml
>> equivalent of the code and then generate the equivalent
>>    code in other languages ( where you could use a template engine,
>> but I found that the python built-in string
>>    formatting libraries are quite up to the task ).
> 
> I also heard that clang is supposed to the quite useful for this kind of undertaking.

I was just discussing this with some folks here at PyCon. Clang has a library interface (libclang):
http://clang.llvm.org/doxygen/group__CINDEX.html

There's Python bindings for it; I'm sure the author would like some company =)

https://bitbucket.org/binet/py-clang/


Cheers
P




More information about the Python-list mailing list