开发者

Python code to parse and inspect c++

开发者 https://www.devze.com 2022-12-21 23:04 出处:网络
Is there a library for Python that wil开发者_StackOverflow社区l allow me to parse c++ code? For example, let\'s say I want to parse some c++ code and find the names of all classes and their member fu

Is there a library for Python that wil开发者_StackOverflow社区l allow me to parse c++ code?

For example, let's say I want to parse some c++ code and find the names of all classes and their member functions/variables.

I can think of a few ways to hack it together using regular expressions, but if there is an existing library it would be more helpful.


In the past I've used for such purposes gccxml (a C++ parser that emits easily-parseable XML) -- I hacked up my own Python interfaces to it, but now there's a pygccxml which should package that up nicely for you.


Parsing C++ accurately is light-years from something you can do with a regular expression. You need a full C++ parser, and they're pretty hard to build. I've been involved in building one over several years, and track who is doing it; I don't know of any being attempted in Python.

The one I work on is DMS C++ Front End. It provides not only parsing, but full name and type resolution. After parsing, you can basically extract detailed information about the code at whatever level of detail you like, including arbittary details about function content.

You might consider using GCCXML, which does contain a parser, and will produce, I believe, the names of all classes, functions, and top-level variables. GCCXML won't give you any information about what's inside a function.


This is a little outside your question's scope perhaps... but depending on what you're trying to achieve, perhaps Exuberant Ctags is worth looking at.


Have not tried, but using the Python bindings from LLVM's Clang parser may work; see here.


How about pyparsing?

0

精彩评论

暂无评论...
验证码 换一张
取 消