Writing parsers for custom data is a fairly common task in a programmer’s every day life. Consequently it is nice to know alternatives. In this post I want to compare 2 different parser libraries, written in different languages.
On the one side: Haskell with attoparsec. On the other side: Python with pyparsing. Both languages have more than one parser library in their package repositories, but I think these two are nice to compare, because they are similar in spirit.
The input is a simple log file format of an imaginary process logging data. One line contains exactly one log entry and all entries follow the same pattern. I took this example from a nice post on schoolofhaskell.
I really like both parser libraries. At least in this usecase it is amazing how easy and automatic it was to translate the original Haskell parser to Python.
I used enclosing parenthesis as a small trick to be able to write the Python parser as close to the Haskell parser in do-notation. I think both parser implementations are comprehensible and easy to grasp.