GithubHelp home page GithubHelp logo

green-wood / parscomb Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 66 KB

A toy monadic parser combinator library for OCaml

License: MIT License

OCaml 93.05% Python 6.27% Standard ML 0.68%
monadic ocaml parser-combinators

parscomb's Introduction

Parscomb

GitHub CI

Parscomb is a lightweight, monadic parser combinator library for OCaml, created as a toy project to learn and explore monadic programming. It allows you to easily create, modify, and combine parsers for various text formats and languages. The library provides essential atomic operations, operators, and derived operations, as well as several useful parsers for common tasks.

Features

  • Simple and composable parser combinators
  • Monadic and applicative interfaces for building complex parsers
  • A collection of useful parsers for common parsing tasks
  • Supports recursive parsing using the fix combinator
  • Backtracking and error reporting (still working)

Learning Monadic Programming

Parscomb serves as a practical example of monadic programming, showcasing the power and expressiveness of monads in the context of parser combinators. By studying the source code and examples, you can gain a deeper understanding of monads and how they can be used to model complex control flows and data manipulations in a functional programming language like OCaml.

Install dependencies

opam switch create parscomb 4.13.1
eval $(opam env)
opam install --deps-only .
dune build

Example

Arithmetic Calculator

In bin/main.ml

open Parscomb.Parser
open Base
open Stdio

let int_num = integer >>| Int.of_string
let add = str "+" *> success ( + )
let minus = str "-" *> success ( - )
let mul = str "*" *> success ( * )
let div = str "/" *> success ( / )

let expr =
  fix ~f:(fun expr ->
      let factor = int_num <|> (str "(" *> expr <* str ")") in
      let term = chainl1 factor (mul <|> div) in
      chainl1 term (add <|> minus))
  <* eof

let rec main_loop () =
  printf ">>> ";
  Out_channel.flush stdout;
  match In_channel.input_line stdin with
  | None -> ()
  | Some s ->
      (match run expr s with
      | Error e -> print_endline e
      | Ok res -> printf "%d\n" res);
      main_loop ()

let () = main_loop ()

You can play with this simple calculator ./_build/default/bin/main.exe:

>>> 1+2+3
6
>>> 1+2-3
0
>>> 1-2+3
2
>>> 1-(2+3)
-4
>>> 1+2*3
7
>>> (1+2)*3
9
>>> 1-2/3
1
>>> (1-2)/3
0
>>> -3/3
-1

JSON Parser

Please refer to lib/json.ml for its implementation.

Acknowledges

We would like to acknowledge and express our gratitude to the authors and maintainers of the following libraries:

  • Base: A standard library overlay provided by Jane Street, which offers a more extensive alternative to the default OCaml standard library.
  • Core: A comprehensive system programming library provided by Jane Street, which extends Base with additional data structures, algorithms, and utilities.
  • ppx_jane: A set of syntax extensions provided by Jane Street, which includes various useful and convenient features, such as let syntax and deriving utilities.

License

Parscomb is released under the MIT License.

parscomb's People

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.