parsing C code using python [closed]

2020-02-23 06:50发布

I have a huge C file (~100k lines) which I need to be able to parse. Mainly I need to be able to get details about individual fields of every structure (like field name and type for every field in the structure) from its definition. Is there a good(open source, which i can use in my code) way to do this already? Or should I write my own parser for this. If I have to write my own, can anyone suggest a good place to start? I have never worked with python before.

Thanks

2条回答
淡お忘
2楼-- · 2020-02-23 07:21

The right way to do this is almost certainly to interface with the front-end of an existing compiler, such as gcc, then work with the intermediate representation, rather than attempting to create your own parser, in any language.

However, pycparser, as suggested by Dhara might well be a good substitute, and definitely better than any attempt to roll your own.

查看更多
ゆ 、 Hurt°
3楼-- · 2020-02-23 07:42

Take a look at this link for an extensive list of parsing tools available for Python. Specifically, for parsing c code, try the pycparser

查看更多
登录 后发表回答