Monadic Parser Combinators in Python
OK, ok, I know this is insane…
I’ve translated some code from Hutton & Meijer’s paper on monadic parser combinators from Haskell into Python.
You can write things like:
> token = isalpha |seq| many(isdigit)
> tokens = token |sepBy| whitespace
> runParser("p123 q456 hello world", tokens)
['p123', 'q456']
Please don’t try to use this to do anything serious.
So here it is: Source code for monadic parser combinators in Python.
Probably the only useful bit of this is the multi-reader buffered stream class at the start. It allows you to have multiple forward-only readers on a single stream. The stream is buffered so that readers lagging behind the front-runner can access values that have already been pulled from the stream. Any values that are no longer needed by any reader are removed from the buffer.

March 21st, 2005 at 10:40 pm
Is’nt pyparsing similar to this (with lessa magic)?
March 21st, 2005 at 11:29 pm
It probably is. And to be honest, the magic probably doesn’t help much. This is more of a show-off than a practical application.
In Haskell, it’s a different story. Parsec is a great parser library.