Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

parsing C code using python [closed]

Tags:

python

parsing

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

like image 534
nightcrawler Avatar asked Jun 21 '12 16:06

nightcrawler


People also ask

Can you use C code in python?

The python default implementation is written in C programming and it's called CPython. So it's not very uncommon to use C functions in a python program. In this tutorial, we learned how to easily call C functions in a python program.

How does python parse code?

Python parsing is done using various ways such as the use of parser module, parsing using regular expressions, parsing using some string methods such as split() and strip(), parsing using pandas such as reading CSV file to text by using read.

What parser does python use?

Making experiments. As the generated C parser is the one used by Python, this means that if something goes wrong when adding some new rules to the grammar you cannot correctly compile and execute Python anymore.


2 Answers

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

like image 128
Dhara Avatar answered Oct 08 '22 17:10

Dhara


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.

like image 25
Marcin Avatar answered Oct 08 '22 17:10

Marcin