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.

2 Responses to “Monadic Parser Combinators in Python”

  1. verbat Says:

    Is’nt pyparsing similar to this (with lessa magic)?

  2. Dominic Says:

    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.

Leave a Reply