Parsing C code with Python

Neil Hodgson nhodgson at bigpond.net.au
Thu Jun 14 19:21:37 EDT 2001


Stéphane BAUSSON:

> I am interested by examples of program parsing C code.
> The C code generates raw data, and these raw data I
> want to parse them after with python to display the
> data in something meaning full for human.
>
> The problem is how to analyze the C code, with the
> struct, union, etc ....

   I have a partial line-oriented C++ lexer which I have been trying to
avoid turning into a parser. The code can be found in the CxxTokens.py file
available in
http://scintilla.sourceforge.net/SinkWorld.zip
   There is a demonstration quality (that is, don't use this for-real) C++
pretty printer in CxxPP.py but the main point is a translator from a subset
of C++ to C#/Java in XLang.py which is part of my SinkWorld project.

   It depends on the details of your analysis whether you will need a token
stream or a parse tree. Many problems can be solved with iterating over a
token stream and maintaining some parse level state (nesting level, scope
stack, ...).

   Neil






More information about the Python-list mailing list