3

I'm looking for a parser for Python (preferably v. 2.7) written in human-readable Python. Performance or flexibility are not important. The accuracy/correctness of the parsing and the clarity of the parser's code are far more important considerations here.

Searching online I've found a few parser generators that generate human-readable Python code, but I have not found the corresponding Python grammar to go with any of them (from what I could see, they all follow different grammar specification conventions). At any rate, even if I could find a suitable parser-generator/Python grammar combo, a readily available Python parser that fits my requirements (human-readable Python code) is naturally far more preferable.

Any suggestions?

Thanks!

kjo
  • 33,683
  • 52
  • 148
  • 265

2 Answers2

3

PyPy is a Python implementation written entirely in Python. I am not an expert, but here's the link to their parser which - obviously - has been written in Python itself:

https://bitbucket.org/pypy/pypy/src/819faa2129a8/pypy/interpreter/pyparser

mac
  • 42,153
  • 26
  • 121
  • 131
  • Thanks, this looked promising at first, but, AFAICT, PythonParser is not documented, and I can't figure out how to use it from reading the source (not a good sign if readability is a requirement)... – kjo Dec 24 '11 at 23:20
  • @kjo - Not sure what you mean by "not documented" (=what you expected to find). The source code has quite a bit of information on the workings of the module. Since programming language parsers are not usually meant for direct use by developers who code in that language, I doubt you will find a lot of "website documentation" in the form of - for example - the one you can browse at http://docs.python.org/ ... or did I misunderstand your comment? – mac Dec 28 '11 at 17:45
  • I can't find how to generate the arguments required to instantiate a parser object; in particular, its `__init__` has a "state" argument that is very hard to pin down. The source code certainly does not say a word about what this argument is supposed to be (what class, etc.). After a lot of searching I was able to find some bits of (very high-level) info, widely scattered over several pages, on these "states", but not enough to tell me how to instantiate a state, so that I can instantiate a parser, and pass it a string of Python source code. If you know how to do this, please let me know. – kjo Dec 31 '11 at 14:13
2

I think you should invest your effort in ast. An excerpt from the python docs.

The ast module helps Python applications to process trees of the Python abstract syntax grammar. The abstract syntax itself might change with each Python release; this module helps to find out programmatically what the current grammar looks like.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • I don't see how this answers my question. `ast.parse` does nothing more than call the built-in `compile` function. How is that a "human-readable parser for Python in Python"? – kjo Dec 24 '11 at 23:27