Previous Contents Next
Chapter 4 Syntax extensions
This chapter introduces the way to extend the syntax of OCaml, and how to redefine the whole syntax of the language.

4.1 The abstract syntax tree

A syntax extension or redefinition holds grammar rules which must return abstract syntax tree nodes. The provided file ``q_MLast.cmo'' holds quotations expanders allowing to create such nodes using a concrete syntax: the description of the abstract syntax tree is not necessary. The complete description of these quotations is given in appendix A.

4.2 Extending the language

The language predefined syntaxes, ``pa_o.cmo'' and ``pa_r.cmo'' use the Camlp4 grammar system: so they are extensible.

The module ``Pcaml'' holds grammars and entries variables used by the language predefined syntaxes files. Syntax extensions can be done by extensions of the appropriate entries.

In some entries, ``pa_o.cmo'' and ``pa_r.cmo'' define labels allowing to locate some levels and to add new rules relative to these levels. The entries and their labels are:

4.3 Examples of language extensions

All examples must be compiled using ``camlp4''. As they use the ``EXTEND'' instruction, Camlp4 must load ``pa_extend.cmo''. A