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: Mautaur Zulkilrajas
Country: Samoa
Language: English (Spanish)
Genre: Career
Published (Last): 15 June 2010
Pages: 95
PDF File Size: 1.92 Mb
ePub File Size: 17.22 Mb
ISBN: 651-2-81345-941-8
Downloads: 71392
Price: Free* [*Free Regsitration Required]
Uploader: Gulrajas

As with most things in CamlP4, quotations are also customizable.

The camlp4 commands

Filters and Printers So CamlP4 parses its input and then builds an abstract syntax tree out of it. 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.

Back to the Revised syntax: OCaml syntax extensions Conceptually, the plan is simple: Besides the above-linked tutorials in the CamlP4 wiki, the section about grammars in the old manual is still very useful.

For example, this piece of code: Sources and Final Thoughts CamlP4 gives OCaml programmers much of the power of metaprogramming available in Lisp languages, added with static type checking and customizable components.

CamlP4 itself is written in this syntax. However, there is a good source of examples of the revised syntax: The user defines a grammar using a special notation, and CamlP4 generates a parser for it.


tutorlal Several libraries are provided which facilitate the specific manipulation of OCaml syntax trees. Instead, each template has gaps that must be filled with data which depends on the situation. External Documents The semi-official documentation: If you know the revised syntax, you can start to use quotations to generate OCaml code.

A (meta-)guide to CamlP4: Metaprogramming in OCaml

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 tutorixl. To learn CamlP4, you must learn this alt syntax.

Grammars and Extensible Parsers: The new CamlP4 wiki has useful stuff, although it is incomplete both as a tutorial and as a reference.

The generated AST can be emitted by a pretty-printer, showing code in a readable format for humans. A proper tutorial would be quite useful, but it would also demand much more time from me, so I decided to do the next best thing: CamlP4 makes it easy to create parsers, because it includes an embedded notation for parser generation.

Putting it all together: This post is meant to be a guide to the available documentation and tutorials about CamlP4, assuming no previous experience with it. The good thing about grammars and parsers in CamlP4 is that they are extensible. This tree can also be filtered before printing. 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.

You could feed the output of the pretty-printer to the OCaml compiler, thus effectively activating the syntax extension. Actually, although the old CamlP4 had an official reference manual and tutorial, the new CamlP4 has neither.

tuhorial The series of posts on CamlP4 over at Ambassador at the Computers is a good source, with some caveats. It is also possible to define new printers, though most of the time this is not very useful. However, it is a quite complex piece of software and this is sometimes exposed to users. It’s been replaced cwmlp4 extensions points and ppx rewriters Camlp4the 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.


This is basic in code generation: This can be used for defining parsers or extending existing ones.

Camlp4 stands for Caml Preprocessor and Pretty-Printer and one of its camlp important applications is the definition of domain-specific extensions of the syntax of OCaml. I just found out about it as I was almost finished writing this post.

The difference is only at the level of the concrete syntax, since these three versions are equivalent representations of the same abstract syntax tree. So as tutorrial example: Camlp4 includes a domain-specific language as it provides syntax extensions which ease the development of syntax extensions.

Quotations and Abstract Syntax Quotations allow the programmer to treat a piece of code as data instead of being part of the program itself. To learn tutorjal to use quotations to generate OCaml AST nodes, you can look at this appendix from the same manual.