Contribute to ocaml/camlp4 development by creating an account on GitHub. #toc Camlp4 is part of the standard OCaml distribution and Camlp5 is distributed separately. name change. Camlp5 Reference Manual (by Daniel de Rauglaudre). Camlp4 stands for Caml Preprocessor and Pretty-Printer and one of its most important applications is the definition of domain-specific.
|Published (Last):||17 July 2009|
|PDF File Size:||2.36 Mb|
|ePub File Size:||15.76 Mb|
|Price:||Free* [*Free Regsitration Required]|
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. Between parsing and printing, it is possible to define AST Filters that can transform the tree, including maps and folds over it. I know this sounds confusing, as messing with quotations can often be, but in most cases it is easier to learn them by example.
This brings up another source of confusion: However, this has some disadvanges: This can be used for janual parsers or extending existing ones.
However, this has some disadvanges:. Expanders can also generate strings instead of AST nodes, although this is less useful. CamlP4 comes with parsers for the syntax of OCaml revised and original variants, possibly others. Other target languages can also be used, such as C. Camlp4 stands for Caml Preprocessor manuzl Pretty-Printer and one of its most important applications is the definition of domain-specific extensions of the syntax of OCaml.
For example, this piece of code:. 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.
The section about the Revised syntax in the latest official reference manual for the old CamlP4 version 3. So CamlP4 parses its input and then builds an abstract syntax tree out of it.
The notion of concrete syntax refers to the format in which the abstract syntax is represented. The good thing about grammars and parsers in CamlP4 is that they are extensible. The abstract syntax tree is at the center of the caml;4 extensions, which are in fact OCaml programs. All of the examples below are for Camlp5 or the previous version of Camlp4 versions 3.
The Camlp4 syntax extension
This tree can also be filtered before printing. Support for manuaal original syntax inside quotations was added in OCaml 3. As this list shows, it is a very powerful and useful tool for parsing and metaprogramming. 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 is mostly used as a metaprogramming tool for OCaml, but it can be used in many ways: Domain-specific languages are a major application of Camlp4.
A (meta-)guide to CamlP4: Metaprogramming in OCaml
However, there is a good source of examples of the revised syntax: And now for the punchline: So the first order of business if campp4 want to learn to use CamlP4 is learning the Revised syntax. Filters and Printers So CamlP4 parses its input and then builds an abstract syntax tree out of it. Quotations and Abstract Canlp4 Quotations allow the programmer to treat a piece of code as data instead of being part of the program itself.
The new CamlP4 wiki has useful stuff, although it is incomplete both as a tutorial and as a reference. The latter one is defined as an extension of the former, so you may need to consult manua. The marshaled tree also includes location information, which allows the compiler to report errors correctly for the input source.
The intention is to give the Big Picture, so that the details can be worked out later. It provides a new keywordmemowhich can be used as a replacement for function and provides automatic memoization of functions with pattern matching.
Camlp4 – Wikipedia
They would all converge to an abstract syntax tree in a unique format that a compiler can handle. Daniel de Rauglaudre wrote the original CamlP4, which was available for OCaml since its early versions. To learn how to use quotations to generate OCaml AST nodes, you can look at this appendix from the same manual.