Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 817
priyanka Avatar asked Dec 15 '11 07:12

priyanka


People also ask

How do parser generators work?

A parser generator takes a grammar as input and automatically generates source code that can parse streams of characters using the grammar. The generated code is a parser, which takes a sequence of characters and tries to match the sequence against the grammar.

What is menhir OCaml?

What is Menhir? Menhir is a LR(1) parser generator for the OCaml programming language. That is, Menhir compiles LR(1) grammar specifications down to OCaml code. Menhir was designed and implemented by François Pottier and Yann Régis-Gianas. Menhir is 90% compatible with ocamlyacc.

What is Lexbuf?

Lexing. lexeme_end lexbuf returns the offset in the input stream of the character following the last character of the matched string. The first character of the stream has offset 0.

What is :: In OCaml?

:: means 2 camel humps, ' means 1 hump! – Nick Craver. Feb 27, 2010 at 12:09. Ok a decent comment: merd.sourceforge.net/pixel/language-study/… I don't use oCaml, but there's a full syntax list you my find useful, even if there's no real context to it.


1 Answers

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.

like image 104
Basile Starynkevitch Avatar answered Sep 23 '22 06:09

Basile Starynkevitch