All Projects → lemaetech → reparse

lemaetech / reparse

Licence: MPL-2.0 license
Reparse is a monadic, recursive descent based, comprehensive parser construction library for ocaml.

Programming Languages

ocaml
1615 projects

Projects that are alternatives of or similar to reparse

lilt
LILT: noun, A characteristic rising and falling of the voice when speaking; a pleasant gentle accent.
Stars: ✭ 18 (-66.04%)
Mutual labels:  parsers
uniorg
An accurate Org-mode parser
Stars: ✭ 190 (+258.49%)
Mutual labels:  parsers
key-parsers
OCaml parsers for multiple key formats
Stars: ✭ 15 (-71.7%)
Mutual labels:  parsers
autumn
A Java parser combinator library written with an unmatched feature set.
Stars: ✭ 112 (+111.32%)
Mutual labels:  parsers
tcalc
A series of C# parser construction examples
Stars: ✭ 34 (-35.85%)
Mutual labels:  parsers
cs-resources
Curated Computer Science and Programming Resource Guide
Stars: ✭ 42 (-20.75%)
Mutual labels:  parsers
m3u8-parser
A simple HLS playlist parser for Java
Stars: ✭ 100 (+88.68%)
Mutual labels:  parsers
leftry
Leftry - A left-recursion enabled recursive-descent parser combinator library for Lua.
Stars: ✭ 32 (-39.62%)
Mutual labels:  parsers
librxvm
non-backtracking NFA-based regular expression library, for C and Python
Stars: ✭ 57 (+7.55%)
Mutual labels:  parsers
Analogy.LogViewer
A customizable Log Viewer with ability to create custom providers. Can be used with C#, C++, Python, Java and others
Stars: ✭ 172 (+224.53%)
Mutual labels:  parsers
rtsp-types
RTSP (RFC 7826) types and parsers/serializers
Stars: ✭ 16 (-69.81%)
Mutual labels:  parsers
Limbo
Library for VLSI CAD Design Useful parsers and solvers' api are implemented.
Stars: ✭ 84 (+58.49%)
Mutual labels:  parsers

Reparse

Reparse is a monadic, recursive descent based, comprehensive parser construction library for ocaml.

Reparse Documentation

Getting Started

$ opam install reparse

Add reparse to dune,

(executable # or library
  (name hello_world)
  (public_name hello_world)
  (libraries reparse))

Example - Calculator

A calculator is the hello world of parsers. Here is an implementation in Reparse which supports +,-,* and / operations.

# #require "reparse";;
open Reparse.String

type expr =
  | Int of int
  | Add of expr * expr
  | Sub of expr * expr
  | Mult of expr * expr
  | Div of expr * expr

let skip_spaces = skip space

let binop : 'a t -> char -> 'b t -> ('a -> 'b -> 'c) -> 'c t =
 fun exp1 op exp2 f ->
  (exp1, skip_spaces *> char op <* skip_spaces, exp2)
  <$$$> fun e1 _ e2 -> f e1 e2

let integer : expr t =
  let+ d = skip_spaces *> digits <* skip_spaces in
  Int (int_of_string d)

let factor : expr t -> expr t =
 fun expr ->
  any [ char '(' *> skip_spaces *> expr <* skip_spaces <* char ')'; integer ]

let term : expr t -> expr t =
 fun factor ->
  recur (fun term ->
      let mult = binop factor '*' term (fun e1 e2 -> Mult (e1, e2)) in
      let div = binop factor '/' term (fun e1 e2 -> Div (e1, e2)) in
      mult <|> div <|> factor)

let expr : expr t =
  recur (fun expr ->
      let factor = factor expr in
      let term = term factor in
      let add = binop term '+' expr (fun e1 e2 -> Add (e1, e2)) in
      let sub = binop term '-' expr (fun e1 e2 -> Sub (e1, e2)) in
      any [ add; sub; term ] <?> "expr")

let rec eval : expr -> int = function
  | Int i -> i
  | Add (e1, e2) -> eval e1 + eval e2
  | Sub (e1, e2) -> eval e1 - eval e2
  | Mult (e1, e2) -> eval e1 * eval e2
  | Div (e1, e2) -> eval e1 / eval e2
# let ast = parse (create_input_from_string "1*2-4+3") expr ;;
val ast : (expr * int, string) result =
  Ok (Sub (Mult (Int 1, Int 2), Add (Int 4, Int 3)), 7)

# eval @@ Result.get_ok (parse (create_input_from_string "12+1*10") expr);;
Line 1, characters 9-72:
Error: This expression has type expr * int
       but an expression was expected of type expr

More Examples

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].