Search code examples
fileparsingocamlstring-parsingfile-read

Ocaml - Files and parsing


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?


Solution

  • 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.