File: tutorial008.html

package info (click to toggle)
ocaml-doc 3.09-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 10,428 kB
  • ctags: 4,963
  • sloc: ml: 9,244; makefile: 2,413; ansic: 122; sh: 49; asm: 17
file content (194 lines) | stat: -rw-r--r-- 8,577 bytes parent folder | download | duplicates (2)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.06">
<TITLE>
 The camlp4 commands
</TITLE>
</HEAD>
<BODY TEXT=black BGCOLOR=white>
<A HREF="tutorial007.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<HR>
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc66"><B><FONT SIZE=6>Chapter&nbsp;8</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>The camlp4 commands</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE>
<A NAME="c:tutcomm"></A>
Camlp4 is a command which you can type from your shell. It has several
versions: <CODE>camlp4</CODE>, <CODE>camlp4o</CODE>, <CODE>camlp4r</CODE>. Actually, the
basic command is <CODE>camlp4</CODE>, the other ones being shortcuts for
this command with syntaxes previously loaded (see man page).<BR>
<BR>


Its options are in two parts: the ``load'' options and the ``other''
options. The input file is among the ``other'' options, generally a
<CODE>.mli</CODE> interface file or <CODE>.ml</CODE> implementation file.<BR>
<BR>
<A NAME="toc53"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc67"><B><FONT SIZE=5>8.1</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>The load options</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
The ``load'' options *must* come first. They are a list of OCaml
object files (cmo) or library file (cma) which are loaded in camlp4
core before any other operation. These files are found in a path. You
can use the option <CODE>-I</CODE> to add directories in this path. By
default, the path contains only the camlp4 library directory
(depending on the installation).<BR>
<BR>


Notice that the current directory is *not* by default in the path: to
load a file belonging to the current directory, you have to use either
the option <CODE>-I .</CODE> or to type the object file prefixed by a dot
and a slash.<BR>
<BR>


In fact, the command <CODE>camlp4</CODE> alone, without any load option does
nothing with the input file and fails. You need to specify at least
one parser and one printer. If you don't specify any parser, you get
the message ``Failure: no loaded parsing module'', if you don't
specify any printer, ``Failure: no printer''. When it runs,
<CODE>camlp4</CODE> applies the parser to the input file, which builds a
syntax tree and this syntax tree is printed using the printer.<BR>
<BR>
<A NAME="toc54"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc68"><B><FONT SIZE=5>8.2</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>The other options</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
The other options follow. If there is a risk of ambiguities, you can
use <CODE>--</CODE> to separate the load options and the other ones: indeed,
the other options can be extended by the object files loaded in the
core: in particular, the provided object file <CODE>pr_depend.cmo</CODE>
adds <CODE>-I</CODE> among the other options.<BR>
<BR>


To see the list of available options, type <CODE>camlp4 -help</CODE> or, since
the object files can extend the options, type
<CODE>camlp4 &lt;load-options&gt; -help</CODE> where <CODE>&lt;load-options&gt;</CODE> is
your load options. Type, for example, and compare the results:
<PRE>
            camlp4 -help
            camlp4 pa_r.cmo pa_extend.cmo -help
            camlp4 pr_depend.cmo -help
</PRE>
<A NAME="toc55"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc69"><B><FONT SIZE=5>8.3</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>The predefined object files</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
The predefined object files, available in the camlp4 library
directory, are of two kinds:
<UL><LI>The parser files, which start with <CODE>"pa_"</CODE> for syntax
definitions or extensions (``pa'' like ``PArser''), or with <CODE>"q_"</CODE>
for quotations expanders.<BR>
<BR>
<LI>The printer files, which start with <CODE>"pr_"</CODE> (``pr'' like
``PRinter'').</UL>
You can see the list of available parsers and printers in the camlp4
library directory or using the man page. The main one are:
<UL><LI><CODE>pa_o.cmo</CODE>: the parser with normal OCaml syntax<BR>
<BR>
<LI><CODE>pa_r.cmo</CODE>: the parser with revised syntax<BR>
<BR>
<LI><CODE>pr_dump.cmo</CODE>: the printer of the raw abstract syntax tree<BR>
<BR>
<LI><CODE>pr_o.cmo</CODE>: the printer with normal OCaml syntax<BR>
<BR>
<LI><CODE>pr_r.cmo</CODE>: the printer with revised syntax</UL>
Note that <CODE>pr_dump.cmo</CODE> displays the abstract syntax tree in
binary. By default, it is displayed in standard output (except if the
option <CODE>-o</CODE> is used). Thus, pay attention: it is binary material,
if you don't redirect the output to a file, you are likely to
perturb your terminal, because it may find escape sequences and
interpret them. This printer is used as input to the OCaml compiler.<BR>
<BR>


The other printers are useful to display the result of a parsing, test
syntax extensions or quotations expanders. You can print either in
normal syntax or in revised syntax. This can be used also to convert
from normal to revised syntax and vice-versa.<BR>
<BR>


Printers can also be extended. This is undocumented because the
printing facility is not perfected and is a little bit complicated to
use. But you can use the predefined printers. They rebuild the text in
the initial form. For example <CODE>pr_extend.cmo</CODE> rebuilds the
<CODE>EXTEND</CODE> statements.<BR>
<BR>


An interesting predefined quotation expander is <CODE>q_phony.cmo</CODE>
which convert all quotations into phony variables. The quotations are
therefore printed as they are. Useful to pretty print your program
containing quotations without expanding them.<BR>
<BR>
<A NAME="toc56"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc70"><B><FONT SIZE=5>8.4</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Commands camlp4o and camlp4r</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
The commands <CODE>camlp4o</CODE> and <CODE>camlp4r</CODE> are actually shortcuts
for (respectively):
<PRE>
            camlp4 pa_o.cmo pa_op.cmo pr_dump.cmo
            camlp4 pa_r.cmo pa_rp.cmo pr_dump.cmo
</PRE>
You can add parsers and (other) printers to these commands also.<BR>
<BR>
<A NAME="toc57"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
<TR><TD BGCOLOR="#66ff66"><DIV ALIGN=center><TABLE>
<TR><TD><A NAME="htoc71"><B><FONT SIZE=5>8.5</FONT></B></A></TD>
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=5>Toplevel</FONT></B></TD>
</TR></TABLE></DIV></TD>
</TR></TABLE><BR>
To use Camlp4 in the toplevel, you have to first load the camlp4
machinery. Just loading <CODE>pa_r.cmo</CODE> or <CODE>pa_o.cmo</CODE> would not
work: the toplevel need a specific object file which contains 1/ a
plug-in to tell it to parse using the Camlp4 stuff 2/ the camlp4
library 3/ object files used by the camlp4 parsers (the camlp4 pretty
printing does not work in the toplevel).<BR>
<BR>


There are two files which contain all of this: <CODE>camlp4r.cma</CODE>
starting with the revised syntax and <CODE>camlp4o.cma</CODE> starting with
the normal syntax. You must then load one of them by typing either:
<PRE>
            #load "camlp4r.cma";;
    or
            #load "camlp4o.cma";;
</PRE>
From now, you are in the Camlp4 machinery. Everything you type is
parsed by Camlp4. In particular, if you chose the revised syntax,
any other <CODE>#load</CODE> statement must end with an alone semicolon,
as the revised syntax requires. To use the camlp4 grammar system, load
<CODE>pa_extend.cmo</CODE>, to use the predefined quotations for the OCaml
syntax tree, load <CODE>q_MLast.cmo</CODE>.
<BR>
<BR>
<I><FONT COLOR=maroon>
<br>
For remarks about Camlp4, write to:
<img src="http://cristal.inria.fr/~ddr/images/email.jpg" alt=email align=top>
</FONT></I><HR>
<A HREF="tutorial007.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
</BODY>
</HTML>