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):||5 May 2009|
|PDF File Size:||12.55 Mb|
|ePub File Size:||12.11 Mb|
|Price:||Free* [*Free Regsitration Required]|
And now for the punchline: However, this has some disadvanges: This internal representation constitutes the abstract syntax tree AST. The generated AST can be emitted by a pretty-printer, showing code in a readable format for humans.
Besides that, there are always the sources. Tuttorial de Rauglaudre wrote the original CamlP4, which was available for OCaml since its early versions. Several libraries are provided which facilitate the specific turorial of OCaml syntax trees. The good thing about grammars and parsers in CamlP4 is that they are extensible.
So a fork ensued: The series of posts on CamlP4 over at Ambassador at the Computers is a good source, with some caveats. For example, this piece of code:. The abstract syntax tree is at the center of the syntax extensions, which are in fact OCaml programs. It is also possible famlp4 define new printers, though most of the time this is not very useful. Warning this page contains advanced material. 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.
I hope camkp4 post helps people get up to speed in using this handy tool. It provides a set of OCaml libraries that are used to define grammars as well as loadable syntax extensions of such grammars.
This page in the CamlP4 wiki has a good overview about using CamlP4 by itself and together with a compiler. You could feed the output of the pretty-printer to the OCaml compiler, thus effectively activating the syntax extension.
A (meta-)guide to CamlP4: Metaprogramming in OCaml
The supplied documentation as of this pages’ creation is very small. If you know the revised syntax, you can start to use quotations to generate OCaml code. Jake Donham probably knows a camlpp4 more about this stuff than me, but sometimes he seems to be writing to people who already know about CamlP4, especially in the first few posts.
Sources and Final Thoughts CamlP4 gives OCaml programmers much of tutoria, power of metaprogramming available in Lisp languages, added with static type checking and customizable components. Memoization consists in storing the results of tutoral computations in a table so that the actual computation of the function for each possible argument occurs at most once. The relevant files are pointed later, in the section about parsers.
As I mentioned, this is not a tutorial on CamlP4. If, instead, you simply quote the line of code above, CamlP4 will expand the quotation into the same AST. CamlP4 gives OCaml programmers much of the power of metaprogramming available in Lisp languages, added with static type checking and customizable components. The difference is only at the level of the concrete syntax, since these three versions are equivalent representations of the same abstract syntax tree.
Most of them are linked in a previous part.
To learn CamlP4, you must learn this alt syntax. Back to the Revised syntax: Many factors contribute to this. CamlP4 is mostly used as a metaprogramming tool for OCaml, but it can be used in many ways: Expanders can also generate strings instead of AST nodes, although cxmlp4 is less useful. So the first order of business if you want to learn to use CamlP4 is learning the Revised syntax. Camlp4 stands for Caml Preprocessor and Pretty-Printer and one of its most important tutoroal is the definition of domain-specific extensions of the syntax of OCaml.
This tree can also be filtered before printing. CamlP4 itself is written in this syntax.
Ambassador to the Computers: Reading Camlp4
Camlp4 underwent a major revision between the version included in ocaml 3. The new CamlP4 wiki has useful stuff, although it is incomplete both as a tutorial and as a reference. It is also possible to have a tutoriao inside an antiquotation, and an antiquotation inside this quotation that is inside the antiquotation of the original quotation, and so on.
The reason for the title will be explained in a bit. The following example defines a syntax extension of OCaml. Putting it all together: Any loaded module can extend a grammar defined in tutorisl module, and an extension can not only add new productions, but also change existing ones or even delete them.
A good idea is to look at examples of simple syntax extensions. I just found out about it as I was almost finished writing this post. A Camlp4 preprocessor operates by loading a collection of compiled modules which define a parser as well as a pretty-printer: Its original author is Daniel de Rauglaudre.
As this list shows, it is a very powerful and useful tool for parsing and metaprogramming.