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
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.
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.
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.
Take a look at this link for an extensive list of parsing tools available for Python. Specifically, for parsing c code, try the pycparser
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With