5

How to read contents from file in ocaml? Specifically how to parse them?

Example :

Suppose file contains (a,b,c);(b,c,d)| (a,b,c,d);(b,c,d,e)|

then after reading this, I want two lists containing l1 = [(a,b,c);(b,c,d)] and l2 = [(a,b,c,d);(b,c,d,e)]

Is there any good tutorial for parsing?

pad
  • 39,834
  • 7
  • 83
  • 159
priyanka
  • 945
  • 9
  • 20
  • 2
    Please do not repost questions. If you have anything new to add to your existing question, please edit it instead. – BoltClock Dec 16 '11 at 17:44

3 Answers3

5

This is a good use case for the menhir parser generator (successor to ocamlyacc). You might want to use ocamllex for lexing. All have good documentation.

You could also use camlp4 or camlp5 stream parsing abilities.

Read also the wikipedia pages on lexing & parsing.

Basile Starynkevitch
  • 1
  • 16
  • 251
  • 479
3

I'd be inclined to use Aurochs, a PEG parser for something like this. There is example code in the repo there.

aneccodeal
  • 7,651
  • 6
  • 38
  • 70
2

If you want to specify a grammar and have ocaml generate lexers and parsers for you, check out these ocamllex and ocamlyacc tutorials. I recommend doing it this way. If you really only have one type of token in your file format, then ocamlyacc might be overkill if you can just use the lexer to split the file up into tokens that are considered valid by the grammar.

oob
  • 1,890
  • 3
  • 26
  • 48