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.
|Published (Last):||18 June 2010|
|PDF File Size:||20.48 Mb|
|ePub File Size:||9.85 Mb|
|Price:||Free* [*Free Regsitration Required]|
Besides the above-linked tutorials in the CamlP4 wiki, the section about grammars in the old manual is still very useful. The generated AST can be emitted by a pretty-printer, showing code in a readable format for humans. The only thing that the developer has to do is write a Camlp4 grammar which converts the domain-specific language in question into a regular OCaml program. I know this sounds confusing, as messing with quotations can often be, but in most cases it is easier to learn them by example.
Camlp4 underwent a major revision between the version included in ocaml 3. De Rauglaudre maintains a separate backward-compatible version, which has been renamed Camlp5. Between parsing and printing, it is possible to define AST Filters that can transform the tree, including maps and folds over it.
Warning this page contains advanced material. You could feed the output of the pretty-printer to the OCaml compiler, thus effectively activating the syntax extension. This internal representation constitutes the abstract syntax tree AST. The relevant files are pointed later, in the section about parsers. The CamlP4 wiki has a page on quotationsand a page making an analogy between quotations and strings. The sequence could start better. Its original author is Daniel de Rauglaudre.
Domain-specific languages are a major application of Camlp4.
From Wikipedia, the free encyclopedia. With quotations this is easier.
Actually, although the old CamlP4 had an official reference manual and tutorial, the new CamlP4 has neither. I just found out about it as I was almost finished writing this post.
A (meta-)guide to CamlP4: Metaprogramming in OCaml
The good thing about grammars and parsers in CamlP4 is that they are extensible. All of the examples below are for Camlp5 or the previous version of Camlp4 versions 3. A Camlp4 preprocessor operates by loading a collection of compiled modules which define a parser as well as a pretty-printer: The user defines a grammar using a special notation, and CamlP4 generates a parser for it.
CamlP4 itself is written in this syntax. As with most things in CamlP4, quotations are also customizable. It provides a new keywordmemowhich can be used as a replacement for function and provides automatic memoization of functions with pattern matching.
It can be output in a binary form, e. As I mentioned, this is not a tutorial on CamlP4. It is or was completely compatible with the old versions of CamlP4, although apparently it is now introducing incompatible changes.
They are widely used in Lisp because of its uniform representation for code and data, and are widely used when programming in CamlP4 because they make it camkp4 to generate code. Before attempting to learn CamlP4, it is recommended to learn how to program in OCaml reasonably well, and to have at least some familiarity with parsing and programming language tools. Part 2 and tuutorial 3 are about quotations from the perspective of a user, while part 8 and part 9 are about implementing new quotations and antiquotations.
Any loaded module can extend a grammar titorial in another module, and an extension can not only add new productions, but also change existing ones or even delete them. CamlP4 is generally used to generate OCaml code, one way or another. So as an example: I hope this post helps people get up to damlp4 in using this handy tool.
CamlP4 is mostly used as a metaprogramming tool for OCaml, but it can be used in many ways: This page in the CamlP4 wiki has a good overview about using CamlP4 by itself and together with a compiler. CamlP4 gives OCaml programmers much of the power of metaprogramming available in Lisp languages, added with static type checking and customizable components.
A (meta-)guide to CamlP4: Metaprogramming in OCaml · Andrei Formiga
Tjtorial and Extensible Parsers CamlP4 makes it easy to create parsers, because it includes an embedded notation for parser generation. Quotations and Abstract Syntax Quotations allow the programmer to treat a piece of code as data instead of being part of the program itself.
Given textual input, CamlP4 parses the input into an abstract syntax tree, which is then printed in some format.