Mparser
A simple monadic parser combinator library for OCaml
Install / Use
/learn @murmour/MparserREADME
=================================================== MParser, a simple monadic parser combinator library
This library implements a rather complete and efficient monadic parser combinator library similar to the Parsec library for Haskell by Daan Leijen and the FParsec library for FSharp by Stephan Tolksdorf.
See LICENSE.txt for copying conditions (LGPL with static linking exception).
Home page: https://github.com/murmour/mparser.
MParser used to be a part of ocaml-base, a collection of useful OCaml libraries by Holger Arnold [1]_.
The monadic interface of MParser is compatible with pa_monad [2]_.
Dependencies
To build this package, you need:
- OCaml (>= 4.02).
- Dune (>= 1.11) [3]_.
- Findlib [4]_.
- Optionally:
re[5]_ formparser-re. - Optionally:
pcre-ocaml[6]_ formparser-pcre.
Installing
Either use OPAM [7]_ or build manually with Dune [3]_.
Installing from OPAM: opam install [sub-library name].
To build manually, cd to this folder and run dune build -p [sub-library name]. Add @install for installation. Add @doc to produce API reference in _build/default/_doc/_html. Consult Dune manual for more options.
Available sub-libraries:
mparser: base library;mparser-re: a plugin that adds support for regular expressions based onre[5]_ (MParser_REmodule,mparser-refindlib package);mparser-pcre: a plugin that adds support for regular expressions based onpcre-ocaml[6]_ (MParser_PCREmodule,mparser-pcrefindlib package).
Usage example
Let's implement a simple expression evaluator.
To save the typing effort, it is often handy to open the MParser module:
.. sourcecode:: ocaml
open MParser
First, we define a parsing combinator expr, which handles expression
parsing, taking care of the operator precedence issues:
.. sourcecode:: ocaml
let infix p op = Infix (p |>> (fun _ a b -> (`Binop (op, a, b))), Assoc_left)
let operators =
[
[
infix (char '*') Mul; infix (char '/') Div;
];
[
infix (char '+') Add; infix (char '-') Sub;
];
]
let decimal = many1_chars digit |>> int_of_string
let expr = expression operators (decimal |>> fun i -> `Int i)
Next, we implement an interpreter for our expression tree:
.. sourcecode:: ocaml
let rec calc = function
| Int i -> i | Binop (op, a, b) ->
match op with
| Add -> calc a + calc b | Sub -> calc a - calc b
| Mul -> calc a * calc b | Div -> calc a / calc b
The evaluator function:
.. sourcecode:: ocaml
let eval (s: string) : int = match MParser.parse_string expr s () with | Success e -> calc e | Failed (msg, e) -> failwith msg
Using it:
.. sourcecode:: ocaml
eval "4*4+10/2" -> 21
Have fun!
References
.. [1] http://www.holgerarnold.net/software .. [2] https://www.cas.mcmaster.ca/~carette/pa_monad .. [3] https://github.com/ocaml/dune .. [4] http://projects.camlcity.org/projects/findlib.html .. [5] https://github.com/ocaml/ocaml-re .. [6] https://mmottl.github.io/pcre-ocaml .. [7] https://opam.ocaml.org
Related Skills
node-connect
349.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
109.4kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
349.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
349.0kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
