Search code examples
parsinghaskellparsechappyalex

Parsing complex files with Parsec


I would like to parse files with several sequences of data (same number of column, same content, ...) with Haskell. My data sequences will be delimited by keywords before and after.

BEGIN
1   882
2   809
3   435
4   197
5   229
6   425
...
END

BEGIN
1   235 623 684
2   871 699 557
3   918 686 49
4   53  564 906
5   246 344 501
6   929 138 474
...
END

My problem is that after several tests with Parsec, I have the impression that Parsec is rather made to parse a file line by line and not the whole file.

Is Parsec the right way to make what I want or should I consider an other tool like Happy or Alex ?

Is there a website (or other ressource) providing examples of parsing complex text files with Parsec ?


Note : The example I give is a very simple one. Things would be more tricky in my files with many more keywords and combinations.


Solution

  • The format as you've described wouldn't be hard at all to handle in parsec.

    As for learning how to use it: your first step should be to avoid whatever guide gave you the impression that parsec worked line-by-line. I recommend Chapter 16 of Real World Haskell as a good place to get started, and once you're comfortable with the basics the reference material at http://hackage.haskell.org/package/parsec is actually very clear.