views:

203

answers:

3

I want to parse c++ sources in Python. Namely, information about the structure of the classes.

I need to construct some object in Python that should contain information about c++ class. Something like this:

  • [namespace]
  • function name [: parent]
  • [constructor([parameters])]
  • [destructor([parameters])]
  • public methods([parameters])
  • private methods([parameters])
  • [enums]
  • [structures]

any advice for this work?

any helpful modules?

similar projects?

thx!

+5  A: 

There's some good information here. In particular, I would check out PLY.

oooh: edit: this little project, built on top of PLY, parses C++ headers (no templates, i guess), and is pure python. neat!

Robert Karl
oh, man, its perfect!
Mike
@Mike If it's perfect, then you should accept the answer. It's the polite thing to do :)
Eric Palakovich Carr
+1  A: 

Using cppheaderparser can be quite a bit of hard work. When I last checked it didn't handle even reasonable C++ code with multiple files, export macros, namespaces, STL containers and all. I've a project with similar requirement like Mike for which I'm using doxygen combined with lxml. Doxygen is a sophisticated C++ parser that dumps out XML representation of the source code. Everything that you need and more. The format is straight-forward to understand. Python programs can read the generated XML using lxml Python library, which has full-fledged XPath support for quick querying. You can easily save yourselves from the headache of parsing C++!

Sumant
+1  A: 

GCC already has an XML output mode, and who knows C++ better than GCC ? (think of handling #include's and macros if you roll your own parser ...)

There are even bindings for it in pygccxml, I recently used it for a little unittest and it gets the job done relatively easily.

Luper Rouch