Typed-BNF helps you minimize the procedures of writing parsers.
- OCaml dune build system
- Typed-BNF compiler(Python 3.10 required)
- Opam libraries:
menhirLib
sedlex
sedlex.ppx
Dune project:
(library
(name <your project name>)
(libraries menhirLib sedlex sedlex.ppx)
(preprocess
(pps sedlex.ppx)))
Writing a json parser library requires only 2 hand-written files:
- the grammar
json.tbnf
; or grammar for your own languagexxx.tbnf
. lib/json_require.ml
(or yourlib/xxx_require.ml
), which provides the implementation of the external symbols you declared injson.tbnf
(or yourxxx.tbnf
).
Then you build the project with tbnf
and menhir
with this bash script:
tbnf json.tbnf ocaml --outdir lib/ --mod json
menhir lib/json_parser.mly --external-tokens Json_tokens
MLI_CONTENT="$(cat lib/json_parser.mli)"
echo "open Json_require;;" > lib/json_parser.mli
echo $MLI_CONTENT >> lib/json_parser.mli
Inserting open Json_require
to the head of lib/json_parser.mli
is a required patch for menhir codegen.
run dune utop .
.
utop # Easyjson.Api.parse_json("{\"1\": 2, \"3\": [1, 2]}");;
- : Json_require.json =
Easyjson.Json_require.Dict
[([|<abstr>|], Easyjson.Json_require.Int 2);
([|<abstr>|],
Easyjson.Json_require.List
[Easyjson.Json_require.Int 1; Easyjson.Json_require.Int 2])]