且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何解析Python中的C ++源代码?

更新时间:2023-02-23 16:50:19

我会简单推荐 Clang 。 p>

这是一个基于C ++库的编译器,设计时很容易重用。这意味着您可以将其仅用于解析和生成抽象语法树。它会处理所有繁琐的运算符重载解析,模板实例化等。



Clang导出一个基于C的界面,它使用Python Bindings扩展。接口通常相当丰富,但我没有使用它。无论如何,如果您希望帮助扩展它,欢迎捐款。


We want to parse our huge C++ source tree to gain enough info to feed to another tool to make diagrams of class and object relations, discern the overall organization of things etc.

My best try so far is a Python script that scans all .cpp and .h files, runs regex searches to try to detect class declarations, methods, etc. We don't need a full-blown analyzer to capture every detail, or some heavy UML diagram generator - there's a lot of detail we'd like to ignore and we're inventing new types of diagrams. The script sorta works, but by gosh it's true: C++ is hard to parse!

So I wonder what tools exist for extracting the info we want from our sources? I'm not a language expert, and don't want something with a steep learning curve. Something we low-brow blue-collar programmer grunts can use :P

Python is preferred as one of the standard languages here, but it's not essential.

I'll simply recommend Clang.

It's a C++ library-based compiler designed with ease of reuse in mind. It notably means that you can use it solely for parsing and generating an Abstract Syntax Tree. It takes care of all the tedious operator overloading resolution, template instantiation and so on.

Clang exports a C-based interface, which is extended with Python Bindings. The interface is normally quite rich, but I haven't use it. Anyway, contributions are welcome if you wish to help extending it.