如何解析Python中的C ++源代码? [英] How to parse C++ source in Python?

查看:155
本文介绍了如何解析Python中的C ++源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们想要解析我们的巨大的C ++源代码树,以获得足够的信息,来为另一个工具提供类和对象关系的图表,辨别事物的整体组织等。



我最好的尝试是到目前为止一个Python脚本扫描所有.cpp和.h文件,运行regex搜索尝试检测类声明,方法等。我们不需要一个成熟的分析器来捕获每个细节,或者一些重的UML图生成器 - 有很多我们想忽略的细节,我们发明了新类型的图。脚本sorta工作,但由gosh它是真的:C ++ 难以解析!



那么我不知道有什么工具可以从我们的来源中提取我们想要的信息?我不是一个语言专家,不想要有一个陡峭的学习曲线的东西。我们低调的蓝领程序员咕噜可以使用:P



Python在这里是首选的标准语言,但它不是必需的。

解决方案

我会简单推荐 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.

这篇关于如何解析Python中的C ++源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆