1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
2
"http://www.w3.org/TR/REC-html40/loose.dtd">
6
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7
<META name="GENERATOR" content="hevea 1.06">
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>
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 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>
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>
32
Streams are presented by the following extensions to the syntactic
33
classes of expressions:
37
| [< stream-component { ; stream-component } >]
38
| parser [pattern] stream-matching
39
| match expr with parser [pattern] stream-matching
43
stream-matching ::= stream-pattern [pattern] -> expr
44
{ | stream-pattern [pattern] -> expr }
47
| [< stream-pat-comp
48
{ ; stream-pat-comp [ ?? expr ] }>]
50
' pattern [ when expr ]
56
Stream expressions are bracketed by <CODE>[<</CODE> and <CODE>>]</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>[<'1; s; t; '2>]</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>[< >]</CODE>.<BR>
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>
75
Stream patterns, also bracketed by <CODE>[<</CODE> and <CODE>>]</CODE>, describe
76
initial segments of streams. In particular, the stream pattern
77
<CODE>[< >]</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
89
Stream matching proceeds destructively: once a component has been
90
matched, it is discarded from the stream (by in-place modification).<BR>
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>
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>
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
121
<I><FONT COLOR=maroon>
123
For remarks about Camlp4, write to:
124
<img src="http://cristal.inria.fr/~ddr/images/email.jpg" alt=email align=top>
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>