My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Wiki pages
Links

pycparser is a complete parser for the C language, written in pure Python. It is a module designed to be easily integrated into applications that need to parse C source code.

The following are some uses for pycparser, taken from real user reports:

  • C code obfuscator
  • Front-end for various specialized C compilers
  • Static code checker
  • Automatic unit-test discovery
  • Adding specialized extensions to the C language

pycparser is also referenced in a few academic theses and papers, mainly on the topic of parallel computing, where it's being used to translate a set of parallel extensions for the C language, down to plain C.

An example of using pycparser:

parser = CParser()

buf = '''
  static void foo(int k)
  {
      j = p && r || q;
      return j;
  }
'''

t = parser.parse(buf, 'x.c')
t.show()

Prints:

FileAST: 
  FuncDef: 
    Decl: foo, [], ['static'], []
      FuncDecl: 
        ParamList: 
          Decl: k, [], [], []
            TypeDecl: k, []
              IdentifierType: ['int']
        TypeDecl: foo, []
          IdentifierType: ['void']
    Compound: 
      Assignment: =
        ID: j
        BinaryOp: ||
          BinaryOp: &&
            ID: p
            ID: r
          ID: q
      Return: 
        ID: j
Powered by Google Project Hosting