1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
<!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,v 6.3 2012-01-09 14:22:20 deraugla Exp $ -->
<!-- Copyright (c) INRIA 2007-2012 -->
<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" />
<style type="text/css"><!--
table { margin-left: 1cm }
td { padding-right: 2mm }
--></style>
</head>
<body>
<div id="menu">
</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">
</div>
<h2>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>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>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>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>
<div class="trailer">
</div>
</div>
</body>
</html>
|