<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <!-- $Id: ml_ast.html 1823 2007-12-28 13:43:03Z deraugla $ --> <!-- Copyright (c) 2007-2008 INRIA --> <title>Abstract tree in concrete syntax</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <link rel="stylesheet" type="text/css" href="styles/base.css" title="Normal" /> <link rel="alternate" type="application/rss+xml" href="rss/camlp5.rss" title="Camlp5"/> <style type="text/css"><!-- table { margin-left: 1cm } td { padding-right: 2mm } --></style> </head> <body> <div id="menu"> <h1>- <a href="http://pauillac.inria.fr/~ddr/camlp5">Camlp5</a> -</h1> <p class="subtitle">Version 5.11</p> <ul> <li><a href="index.html">Introduction</a></li> <li><a href="strict.html">Transitional and Strict</a></li> <li><a href="ptools.html">Parsing and printing tools</a></li> </ul> <ul> <li>Parsing tools <ul> <li><a href="parsers.html">Stream parsers</a></li> <li><a href="lexers.html">Stream lexers</a></li> <li><a href="fparsers.html">Functional parsers</a></li> <li><a href="bparsers.html">Backtracking parsers</a></li> <li><a href="grammars.html">Extensible grammars</a></li> </ul> </li> <li>Printing tools <ul> <li><a href="printers.html">Extensible printers</a></li> <li><a href="pprintf.html">Pprintf</a></li> <li><a href="pretty.html">Pretty print</a></li> </ul> </li> <li>Language extensions <ul> <li><a href="locations.html">Locations</a></li> <li><a href="ml_ast.html">Syntax tree</a></li> <li><a href="ast_transi.html">Syntax tree - transi</a></li> <li><a href="ast_strict.html">Syntax tree - strict</a></li> <li><a href="q_ast.html">Quotation kit q_ast.cmo</a></li> <li><a href="pcaml.html">The Pcaml module</a></li> <li><a href="syntext.html">Extensions of syntax</a></li> <li><a href="opretty.html">Extensions of printing</a></li> <li><a href="quot.html">Quotations</a></li> <li><a href="revsynt.html">Revised syntax</a></li> <li><a href="scheme.html">Scheme syntax</a></li> <li><a href="macros.html">Macros</a></li> <li><a href="pragma.html">Pragma directive</a></li> <li><a href="extfun.html">Extensible functions</a></li> </ul> </li> <li>Appendix <ul> <li><a href="commands.html">Commands and Files</a></li> <li><a href="library.html">Library</a></li> <li><a href="sources.html">Camlp5 sources</a></li> <li><a href="about.html">About Camlp5</a></li> </ul> </li> </ul> </div> <div id="content"> <h1 class="top">Syntax tree</h1> <p>In Camlp5, one often uses syntax trees. For example, in grammars of the language (semantic actions), in pretty printing (as patterns), in optimizing syntax code (typically stream parsers). Syntax trees are mainly defined by sum types, one for each kind of tree: "<tt>expr</tt>" for expressions, "<tt>patt</tt>" for patterns, "<tt>ctyp</tt>" for types, "<tt>str_item</tt>" for structure items, and so on. Each node corresponds to a possible value of this type.</p> <div id="tableofcontents"> <ol> <li><a href="#a:Transitional-and-Strict-modes">Transitional and Strict modes</a></li> <li><a href="#a:Compatibility">Compatibility</a></li> <li><a href="#a:Two-quotations-expanders">Two quotations expanders</a></li> <li><a href="#a:Syntax-tree-and-Quotations-in-the-two-modes">Syntax tree and Quotations in the two modes</a></li> </ol> </div> <h2 id="a:Transitional-and-Strict-modes">Transitional and Strict modes</h2> <p>Since version 5.00 of Camlp5, the definition of the syntax tree has been different according to the mode Camlp5 has been installed:</p> <ul> <li>In <a href="ast_transi.html"><em>transitional</em></a> mode, this definition is the same than in the previous Camlp5 versions.</li> <li>In <a href="ast_strict.html"><em>strict</em></a> mode, many constructor parameters have a type enclosed by the predefined type "<tt>Ploc.vala</tt>".</li> </ul> <p>The advantage of the transitional mode is that the abstract syntax tree is fully compatible with previous versions of Camlp5. Its drawback is that when using the <a href="q_ast.html">syntax tree quotations in user syntax</a>, it is not possible to use antiquotations, a significatant limitation.</p> <p>In strict mode, the abstract syntax is not compatible with versions of Camlp5 previous to 5.00. Most of the parameters of the constructor are enclosed with the type "<tt>Ploc.vala</tt>" whose aim is to allow nodes to be either of the type argument, or an antiquotation. In this mode, the syntax tree quotations in user syntax can be used, with the same power of the previous syntax tree quotations provided by Camlp5.</p> <h2 id="a:Compatibility">Compatibility</h2> <p>As there is a problem of compatibility in strict mode, a good solution, for the programmer, is to always use syntax trees using quotations, which is backward compatible. See the chapter about <a href="ast_strict.html">syntax tree in strict mode</a>.</p> <p>For example, if the program made a value of the syntax tree of the "let" statement, like this:</p> <pre> ExLet loc rf pel e </pre> <p>In strict mode, to be equivalent, this expression should be rewritten like this:</p> <pre> ExLet loc (Ploc.VaVal rf) (Ploc.VaVal pel) e </pre> <p>where "<tt>Ploc.VaVal</tt>" is a value of the type "<tt>vala</tt>" defined in the module <a href="library.html">Ploc</a> (see its section "pervasives").</p> <p>This necessary conversion is a drawback if the programmer wants that his programs remain compilable with previous versions of Camlp5.</p> <p>The recommended solution is to always write this code with quotations, namely, in this example, like this:</p> <pre> <:expr< let $flag:rf$ $list:pel$ in $e$ >> </pre> <p>The quotation expanders ensure that, in strict mode, the variable "rf" is still of type "<tt>bool</tt>", and that the variable "pel" of type "<tt>list (patt * expr)</tt>", by enclosing them around "<tt>Ploc.VaVal</tt>".</p> <p>In transitional mode, it is equivalent to the first form above. In strict mode, it is equivalent to the second form. And the previous versions of Camlp5 also recognizes this form.</p> <h2 id="a:Two-quotations-expanders">Two quotations expanders</h2> <p>Camlp5 provides two quotations expanders of syntax trees: "<tt>q_MLast.cmo</tt>" and "<tt>q_ast.cmo</tt>".</p> <p>Both allow writing syntax trees in concrete syntax as explained in the previous section.</p> <p>The first one, "<tt>q_MLast.cmo</tt>" requires that the contents of the quotation be in <a href="revsynt.html">revised syntax</a> without any syntax extension (even the <a href="parsers.html">stream parsers</a>). It works in transitional and in strict modes.</p> <p>The second one, "<tt>q_ast.cmo</tt>" requires that the contents of the quotation be in the current user syntax (normal, revised, lisp, scheme, or other) and can accept all the syntax extensions he added to compile his program. It fully works only in strict mode. In transitional mode, the antiquotations are not available.</p> <h2 id="a:Syntax-tree-and-Quotations-in-the-two-modes">Syntax tree and Quotations in the two modes</h2> <p>For the detail of the syntax tree and the quotations forms, see the chapters about the <a href="ast_transi.html">syntax tree in transitional mode</a> and the <a href="ast_strict.html">syntax tree in strict mode</a>.</p> <a class="toplink" href="ml_ast.html">↑</a> <div class="trailer"> <hr style="margin:0" /><div style="font-size: 80%"><em>Copyright 2007 Daniel de Rauglaudre (INRIA)</em></div> <p class="bottom"> <a href="http://validator.w3.org/check?uri=referer"><img src="images/valid-xhtml11.png" style="border:0" alt="Valid XHTML 1.1" height="31" width="88" /></a> </p> </div> </div> </body> </html>