0

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.

JeanJouX
  • 2,555
  • 1
  • 25
  • 37
  • 4
    Parsec is completely agnostic as to whether or not you're parsing line by line. The format of your data is very easily within the reach of Parsec, which is an industrial-strength parser designed for programming languages (with a far more complex grammar than your example!) Something like `parseBlock = between (string "BEGIN\n") (string "END\n") $ sepBy (char '\n') parseSingleLine` (where `parseSingleLine` parses one row of data) would work. – Benjamin Hodgson Jun 20 '15 at 08:26
  • 1
    There is nothing about Parsec which is "line by line". It should easily be able to handle this. – MathematicalOrchid Jun 22 '15 at 12:39
  • @BenjaminHodgson I think you meant `sepBy parseSingleLine (char '\n')`. – jub0bs Jul 08 '15 at 13:09
  • @JeanJoux Edit your question and specify the data types you want to use as the result(s) of your parser(s) in your example. – jub0bs Jul 08 '15 at 13:19

1 Answers1

0

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.

Jeremy List
  • 1,756
  • 9
  • 16