~ubuntu-branches/ubuntu/hardy/ocaml-doc/hardy

« back to all changes in this revision

Viewing changes to camlp4.html/manual003.html

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Mimram
  • Date: 2007-09-08 01:49:22 UTC
  • mfrom: (0.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070908014922-lvihyehz0ndq7suu
Tags: 3.10-1
* New upstream release.
* Removed camlp4 documentation since it is not up-to-date.
* Updated to standards version 3.7.2, no changes needed.
* Updated my email address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
2
 
            "http://www.w3.org/TR/REC-html40/loose.dtd">
3
 
<HTML>
4
 
<HEAD>
5
 
 
6
 
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7
 
<META name="GENERATOR" content="hevea 1.06">
8
 
<TITLE>
9
 
 Streams and parsers
10
 
</TITLE>
11
 
</HEAD>
12
 
<BODY TEXT=black BGCOLOR=white>
13
 
<A HREF="manual002.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
14
 
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
15
 
<A HREF="manual004.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
16
 
<HR>
17
 
<TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%">
18
 
<TR><TD BGCOLOR="#2de52d"><DIV ALIGN=center><TABLE>
19
 
<TR><TD><A NAME="htoc8"><B><FONT SIZE=6>Chapter&nbsp;2</FONT></B></A></TD>
20
 
<TD WIDTH="100%" ALIGN=center><B><FONT SIZE=6>Streams and parsers</FONT></B></TD>
21
 
</TR></TABLE></DIV></TD>
22
 
</TR></TABLE>
23
 
<A NAME="c:manstrm"></A>
24
 
Objective Caml comprises a library type for streams (possibly infinite
25
 
sequences of elements, that are evaluated on demand), and associated
26
 
stream expressions, to build streams, and stream patterns, to
27
 
destructure streams. Streams and stream patterns provide a natural
28
 
approach to the writing of recursive-descent parsers.<BR>
29
 
<BR>
30
 
 
31
 
 
32
 
Streams are presented by the following extensions to the syntactic
33
 
classes of expressions:
34
 
<PRE>
35
 
             expr ::= ...
36
 
                    | [&lt; &gt;]
37
 
                    | [&lt; stream-component  { ; stream-component } &gt;]
38
 
                    | parser [pattern]  stream-matching
39
 
                    | match expr with parser  [pattern]  stream-matching
40
 
 stream-component ::=
41
 
                      ' expr
42
 
                    | expr
43
 
  stream-matching ::= stream-pattern [pattern] -&gt; expr
44
 
                          { | stream-pattern [pattern] -&gt;  expr }
45
 
   stream-pattern ::=
46
 
                      [&lt; &gt;]
47
 
                    | [&lt; stream-pat-comp
48
 
                             { ; stream-pat-comp  [ ?? expr ] }&gt;]
49
 
  stream-pat-comp ::=
50
 
                      ' pattern  [ when expr ]
51
 
                    | pattern =  expr
52
 
                    | ident
53
 
</PRE>
54
 
 
55
 
 
56
 
Stream expressions are bracketed by <CODE>[&lt;</CODE> and <CODE>&gt;]</CODE>. They
57
 
represent the concatenation of their components. The component ' expr
58
 
represents the one-element stream whose element is the value of
59
 
expr. The component expr represents a sub-stream. For instance, if
60
 
both s and t are streams of integers, then <CODE>[&lt;'1; s; t; '2&gt;]</CODE> is a
61
 
stream of integers containing the element 1, then the elements of s,
62
 
then those of t, and finally 2. The empty stream is denoted by <CODE>[&lt; &gt;]</CODE>.<BR>
63
 
<BR>
64
 
 
65
 
 
66
 
Unlike any other kind of expressions in the language, stream
67
 
expressions are submitted to lazy evaluation: the components are not
68
 
evaluated when the stream is built, but only when they are accessed
69
 
during stream matching. The components are evaluated once, the first
70
 
time they are accessed; the following accesses reuse the value
71
 
computed the first time.<BR>
72
 
<BR>
73
 
 
74
 
 
75
 
Stream patterns, also bracketed by <CODE>[&lt;</CODE> and <CODE>&gt;]</CODE>, describe
76
 
initial segments of streams. In particular, the stream pattern
77
 
<CODE>[&lt; &gt;]</CODE> matches all streams. Stream pattern components are
78
 
matched against the corresponding elements of a stream. The component
79
 
<CODE>' pattern</CODE> matches the corresponding stream element against the
80
 
pattern; if followed by when, the match is accepted only if the result
81
 
of the guard expression is true. The component <CODE>pattern = expr</CODE>
82
 
applies the function denoted by expr to the current stream, then
83
 
matches the result of the function against pattern. Finally, the
84
 
component ident simply binds the identifier to the stream being
85
 
matched.<BR>
86
 
<BR>
87
 
 
88
 
 
89
 
Stream matching proceeds destructively: once a component has been
90
 
matched, it is discarded from the stream (by in-place modification).<BR>
91
 
<BR>
92
 
 
93
 
 
94
 
Stream matching proceeds in two steps: first, a pattern is selected by
95
 
matching the stream against the first components of the stream
96
 
patterns; then, the following components of the selected pattern are
97
 
checked against the stream. If the following components do not match,
98
 
the exception Stream.Error is raised. There is no backtracking here:
99
 
stream matching commits to the pattern selected according to the first
100
 
element. If none of the first components of the stream patterns match,
101
 
the exception Stream.Failure is raised. The Stream.Failure exception
102
 
causes the next alternative to be tried, if it occurs during the
103
 
matching of the first element of a stream, before matching has
104
 
committed to one pattern.<BR>
105
 
<BR>
106
 
 
107
 
 
108
 
The streams hold the count of their elements discarded. The optional
109
 
pattern before the first stream pattern is bound to the stream count
110
 
before the matching. The one after each stream pattern (optional, too)
111
 
is bound to the stream count after the matching.<BR>
112
 
<BR>
113
 
 
114
 
 
115
 
The exception Stream.Error has a string parameter coming from the
116
 
optional <CODE>?? expr</CODE> after the stream pattern components (its
117
 
default is the empty string). This expression is evaluated only in
118
 
case of error.
119
 
<BR>
120
 
<BR>
121
 
<I><FONT COLOR=maroon>
122
 
<br>
123
 
For remarks about Camlp4, write to:
124
 
<img src="http://cristal.inria.fr/~ddr/images/email.jpg" alt=email align=top>
125
 
</FONT></I><HR>
126
 
<A HREF="manual002.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
127
 
<A HREF="index.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
128
 
<A HREF="manual004.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
129
 
</BODY>
130
 
</HTML>