Skip to content
A general recursive-descent parser written in OCaml, and a small calculator built from it.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



I wrote Zooly a few years ago, at a time when I was still learning both functional programming and OCaml. The code could use some serious improvement.

Namely, I'd like to:

  • transition to the Core library instead of Batteries Included
  • Rewrite everything to move away from classes, which are rarely used in OCaml
  • Simplify the implementation through common abstractions

That begin said, the code works, and I learnt a lot writing it. For this type of project, that's the best one could hope for, isn't it?


A general recursive-descent parser written in OCaml, and a small calculator built from it.

Zooly intentionally doesn't use the parsing tools built-in to OCaml in order to be an effective learning experience into writing a parser from scratch.

Rather than build a parser that can exclusively parse mathematical expressions, a number of general purpose parsers are defined that are capable of parsing any conceivable syntax. A grammar for a calculator is then built from these general parsing constructs in order to demonstrate their use.

Dependencies and Build Instructions

Zooly depends on OCaml Batteries Included and has been tested with Ocaml 4.00.1.

To build the byte-compiled version of Zooly:

ocamlbuild -use-ocamlfind zooly.byte

To build the native version:

ocamlbuild -use-ocamlfind zooly.native

Example Session

>>> 2 + 3
>>> 3 - 4 * 6.2
>>> sin(pi / 2)
>>> cos(e^2)
>>> cos(3, 4.5)
ERROR: 'cos' expects 1 arguments
>>> let x = 23  
>>> let y = 3 * (x - 4)
>>> sin(x)^2 + cos(x)^2
>>> tan(x + foobar)
ERROR: No such variable 'foobar'

Known Issues

  • Positive and negative signs do not have the correct operator precedence. For example, -2^4 is evaluated as 16.

  • Error messages could be significantly improved. It may be beneficial to add some sort of required directive to indicate that after a certain point in a rule, the remainder of the rule must be satisfied (i.e., backtracking is not allowed).

Something went wrong with that request. Please try again.