Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good Python library that can parse C++? [closed]

Tags:

c++

python

Google didn't turn up anything that seemed relevant.

I have a bunch of existing, working C++ code, and I'd like to use python to crawl through it and figure out relationships between classes, etc.

EDIT: Just wanted to point out: I don't think I need or want to parse every bit of C++; I just need something smart enough to pick up on class, function and member variable declarations, and to skip over function definitions.

like image 695
csbrooks Avatar asked Sep 18 '09 14:09

csbrooks


People also ask

Can Python integrate with C?

Any code that you write using any compiled language like C, C++, or Java can be integrated or imported into another Python script. This code is considered as an "extension." A Python extension module is nothing more than a normal C library.

Is Python good for parsing?

Parsing text using string methodsPython is incredible when it comes to dealing with strings. It is worth internalising all the common string operations. We can use these methods to extract data from a string as you can see in the simple example below.

What is a modern parsing library for Python?

A modern parsing library for Python, implementing Earley & LALR(1) and an easy interface . Lark is a parser generator that works as a library. You write the grammar in a string or a file and then use it as an argument to dynamically generate the parser.

Is there a drop-in Python library that parses C++?

You won't find a drop-in Python library to do this. Parsing C++ is fiddly, and few parsers have been written that aren't part of a compiler. You can find a good summary of the issues here. The best bet might be clang, as its C++ support is well-established.

What is the best C++ library to use with Python?

The best bet might be clang, as its C++ support is well-established. Though this is not a Python solution, it sounds as though it would be amenable to re-use within a Python wrapper, given the emphasis on encapsulation and good design in its development. Share

What is the best library to parse C++?

There is no (free) good library to parse C++ in any language. Your best choices are probably Dehydra g++ plugin, clang, or Elsa. The pyparsing wiki shows this example - all it does is parse struct declarations, so this might give you just a glimpse at the magnitude of the problem.


2 Answers

Not an answer as such, but just to demonstrate how hard parsing C++ correctly actually is. My favorite demo:

template<bool> struct a_t;  template<> struct a_t<true> {     template<int> struct b {}; };  template<> struct a_t<false> {     enum { b }; };  typedef a_t<sizeof(void*)==sizeof(int)> a;  enum { c, d }; int main() {     a::b<c>d; // declaration or expression? } 

This is perfectly valid, standard-compliant C++, but the exact meaning of commented line depends on your implementation. If sizeof(void*)==sizeof(int) (typical on 32-bit platforms), it is a declaration of local variable d of type a::b<c>. If the condition doesn't hold, then it is a no-op expression ((a::b < c) > d). Adding a constructor for a::b will actually let you expose the difference via presence/absence of side effects.

like image 61
Pavel Minaev Avatar answered Sep 21 '22 18:09

Pavel Minaev


C++ is notoriously hard to parse. Most people who try to do this properly end up taking apart a compiler. In fact this is (in part) why LLVM started: Apple needed a way they could parse C++ for use in XCode that matched the way the compiler parsed it.

That's why there are projects like GCC_XML which you could combine with a python xml library.

Some non-compiler projects that seem to do a pretty good job at parsing C++ are:

  • Eclipse CDT
  • OpenGrok
  • Doxygen
like image 36
Stef Avatar answered Sep 20 '22 18:09

Stef