package nice_parser

  1. Overview
  2. Docs
Nice parsers without the boilerplate


Dune Dependency






Nice_parser wraps your {menhir, ocamlyacc}-generated parser in a sane interface, eliminating boilerplate code.

Published: 28 Jul 2019


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:

  1. The 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.

  2. The example folder contains a fully functional parser skeleton based on Nice_parser and standard tools:

    • Menhir, a LR(1) parser generator.

    • ocamllex, OCaml's built-in lexer generator.

    • Jane Street's dune, the inofficial standard build system for OCaml.

    • Jane Street's base, the inofficial standard library for OCaml.

You can get started with your own parser in seconds:

opam install nice_parser                                  # install the nice_parser library
git clone    # 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.

How to build

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 Improvements

Suggestions and changes are welcome. Please submit pull requests, or open issues.

Dependencies (3)

  1. stdio
  2. dune >= "1.10"
  3. ocaml >= "4.05.0"

Dev Dependencies (6)

  1. odoc with-doc
  2. ppx_expect with-test & < "v0.13"
  3. ppx_inline_test with-test & < "v0.13"
  4. ppx_jane with-test & < "v0.13"
  5. base with-test & < "v0.13"
  6. menhir with-test

Used by (1)

  1. netkat




Innovation. Community. Security.