Camlp4 Deprecation Warning: this tutorial describes technology that is considered obsolete. It’s been replaced by extensions points and ppx rewriters. Camlp4 is part of the standard OCaml distribution and Camlp5 is distributed The title of the tutorial says Camlp4 but that is because it was written prior to the. Many of the tutorials below need updating and tutorials on many new topics are Camlp4 Tutorial (by Jake Donham), to build syntax extensions for OCaml with.

Author: Zulugami Dourr
Country: Morocco
Language: English (Spanish)
Genre: Literature
Published (Last): 9 April 2015
Pages: 149
PDF File Size: 7.38 Mb
ePub File Size: 2.26 Mb
ISBN: 369-1-47829-600-7
Downloads: 87236
Price: Free* [*Free Regsitration Required]
Uploader: Kagrel

Quotations allow the programmer to treat a piece of code as data instead of being part of the program itself.

A (meta-)guide to CamlP4: Metaprogramming in OCaml ยท Andrei Formiga

This page was last edited on 18 Novemberat Domain-specific languages are a major application of Camlp4. Since OCaml is a multi-paradigm language, with an interactive toplevel and a native code compiler, it can be used as a backend for any kind of original language.

This is basic in code generation: Back to the Revised syntax: The abstract syntax tree is at the yutorial of the syntax extensions, which are in fact OCaml programs. To extend the OCaml parser, it may be useful to take a look at how it is defined for the standard syntax es.

To learn how to use quotations to generate OCaml AST nodes, you can look at this appendix from the same manual. Although the definition of grammars must be done in OCaml, the parser that is being defined or extended is not necessarily related to OCaml, in which case the syntax tree that is being manipulated is not the one of OCaml.

The wiki has a page with a simple extension for float expressions this extension uses a map over the AST to avoid having to rewrite many grammar productions. All of the examples below are for Camlp5 or the previous version of Camlp4 versions 3. CamlP4 itself is written in this syntax. A syntax extension is defined by a compiled OCaml module, which is passed to the camlp4o executable along with the program to process.


It is also possible to define new printers, though most of the time this is not very useful. Camlp4 underwent a major revision between the version included in ocaml 3. As this list shows, it is a very powerful and useful tool for parsing and metaprogramming. For now, there are no better options, so the only way out is to read both sources and try to integrate them mentally.

The topics are presented in the best order IMO, of course for learning, under this assumption. Besides that, there are always the sources.

Camlp4 – Wikipedia

The reason for the title will be explained in a bit. To use this from the OCaml compilers, you just need to use the -pp command-line flag.

CamlP4 makes it easy to create parsers, because it includes an embedded notation for parser generation. Its original author is Daniel de Rauglaudre.

If you know the revised syntax, you can start to use quotations to generate OCaml code. Actually, although the old CamlP4 had an official reference manual and tutorial, the new CamlP4 has neither. However, this has some disadvanges:.

Retrieved from ” https: Any loaded module can extend a grammar defined in another module, and an extension can not only add new productions, but also change existing ones or even delete them.

OCaml tutorials

For example, this piece of code:. Many factors contribute to this. As demonstrated by the definition of a revised syntax for OCaml, the same programming language can use different concrete syntaxes. De Rauglaudre maintains a separate backward-compatible version, which has been renamed Camlp5.

The supplied documentation as of this pages’ creation is very small. More examples can be found in the recommended sources below. He also uses quotations in original syntax in the earlier parts, rendering the example code unusable in current versions of OCaml in some cases he linked to newer versions that work. The marshaled tree also includes location information, which allows the compiler to report errors correctly for the input source.


The generated AST can be emitted by a pretty-printer, showing code in a readable format for humans. This post is meant to be a guide to the available documentation and tutorials about CamlP4, assuming no previous experience with it.

A Camlp4 preprocessor operates by loading a collection of compiled modules which define a parser as well as a pretty-printer: This tree can also be filtered before printing.

If the target language is OCaml, simple syntax add-ons or syntactic sugar can be defined, in order to provide an expressivity which is not easy to achieve using the standard features of the OCaml language. This is similar to the way format strings work in printf-like functions.

So a fork ensued: Camlp4 Wiki The ocaml beta announcement: It is often used to write syntax extensions to OCaml, like adding support for a notation for monads or for list comprehensions. Camlp4 includes a domain-specific language as it provides syntax extensions which ease the development of syntax extensions.

Jake Donham has written a series of posts in his blog about CamlP4. However, there is a good source of examples of the revised syntax: From the point of view of syntax extensions for OCaml, CamlP4 parses OCaml code, most likely using an extended syntax, generates an AST that may be filtered, and then prints it.

Views Read Edit View history.