Python Parser

about | archive


[ 2005-October-30 18:16 ]

If you want to build tools that manipulate Python code, you need a parser. If you want to write your tool in Python, access to the "real" parser is included as part of standard library. However, if you want access to this parser from C/C++ code, you basically need to include the entire Python runtime. I spent a few hours hacking away on it to separate the parser from the rest of Python. The code here is very rough and does not currently support all the Python types, but it does work (tested with Mac OS X and Linux). As an example, I've written a small program that prints the parse tree. It only supports a subset of the parse tree types, but it should be pretty obvious how it could be extended.

pyparser.tar.bz2

Example Python Program

def foo( arg1, arg2, arg3 ):
    a = 5
    b = arg2
    return arg2

b = 72

Example Output

function name = 'foo'
 arguments:
  name = 'arg1'
  name = 'arg2'
  name = 'arg3'
 body:
  assignment
   targets:
    name = 'a'
   value:
    number = 5
  assignment
   targets:
    name = 'b'
   value:
    name = 'arg2'
  return value:
    name = 'arg2'
assignment
 targets:
  name = 'b'
 value:
  number = 72