Page
Library
Module
Module type
Parameter
Class
Class type
Source
For an older version of this repository based on sedlex, refer to the sedlex branch.
Writing a parser in OCaml ~requires~ used to require a lot of boilerplate code. Nice_parser changes this:
nice_parser
library (API documented here) consolidates boilerplate code that can be shared among all parsers, providing a sane interface (with beautiful error messages) to your {menhir, ocamlyacc}-generated parser.The example folder contains a fully functional parser skeleton based on Nice_parser
and standard tools:
You can get started with your own parser in seconds:
opam install nice_parser # install the nice_parser library
git clone https://github.com/smolkaj/ocaml-parsing.git # clone this repository
cd ocaml-parsing && rm -r src && mv example src # use example as starting point
dune build src/example.a # try to build...
dune exec src/bin/main.exe # ...and run your parser!
You should see the following output (the error message relies on OCaml >= 4.08):
Trying to parse "(a b (c d) e)".
-> (List ((Atom a) (Atom b) (List ((Atom c) (Atom d))) (Atom e)))
Trying to parse "(long_atom_with_0123)".
-> (List ((Atom long_atom_with_0123)))
Trying to parse "
( so far so good
but (this is)) illegal (isnt it?)
(* parsing will fail ^^^^^^^ here *)
".
Fatal error: exception Line 3, characters 25-32:
3 | but (this is)) illegal (isnt it?)
^^^^^^^
Error: [parser] unexpected token
The API is documented here. The example skeleton should be self-explanatory.
Ideally, use OCaml 4.08 or higher (for beautiful error messages). The project can be built using dune. Consult the dune-project file for the necessary dependencies; all can be installed using opam, OCaml's packet manager.
Suggestions and changes are welcome. Please submit pull requests, or open issues.