• en

Camlp4 3.10

Deprecation Warning: this tutorial describes technology that is considered obsolete. It's been replaced by extensions points and ppx rewriters

Camlp4, the OCaml "pre-processor pretty printer" is an advanced macro system which allows complex abstract syntax tree transformations on ocaml programs, and on other recursive decent grammars.

Warning this page contains advanced material.

Camlp4 underwent a major revision between the version included in ocaml 3.09 and 3.10. The supplied documentation as of this pages' creation is very small. This page is experimental and user supported content.

External Documents

The semi-official documentation: Camlp4 Wiki

The ocaml beta announcement: camlp4-changes

The examples

An example written for Camlp4 3.10:

These examples are from the camlp4 beta announcement:

This is a translation of M. Jambon's json_static to Camlp4 3.10 by N. Poulliard.

The patch:


The new version (compiles with camlp4orf):


The command line

In the beta announcement, there was mention of new binaries camlp4xxx that come preloaded with selections of new and old syntaxes

New commands:

  • camlp4orf: camlp4o + Grammar, macro and revised quotation syntaxes.
  • camlp4oof: camlp4o + Grammar, macro and the same syntax for quotations.
  • camlp4rf: camlp4r + Grammar, macro and the same syntax for quotations.

It is not necessary to use a .cma file to create a regular camlp4 extension. The .cmi libraries are for creating standalone executables. Only the .cmi files from the include path are needed.

So as an example:

$ ocamlc -I +camlp4 -pp camlp4orf -c dynamic_functor_example3.ml

The .cma files

From N. Poulliard http://caml.inria.fr/pub/ml-archives/caml-list/2007/04/47c5c935136351afaefee6a0aa9cc506.en.html

There is a list of libraries provided with camlp4:

  • camlp4{o,of,orf,r,rf}.cma are toplevel libs there are only made to be loaded in the OCaml interactive toplevel.

  • camlp4lib.cma contains the Camlp4 module which holds the camlp4 kernel.

  • camlp4fullilb.cma contains modules of camlp4lib.cma plus all parsers and filters.