Debian Codegraph @

pod/Advanced/Input_Models.pod

raw
1 # Copyright 2022 Jeffrey Kegler
2 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
3 # redistribute it and/or modify it under the terms of the GNU Lesser
4 # General Public License as published by the Free Software Foundation,
5 # either version 3 of the License, or (at your option) any later version.
6 #
7 # Marpa::R2 is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser
13 # General Public License along with Marpa::R2. If not, see
14 # http://www.gnu.org/licenses/.
15
16 =head1 NAME
17
18 Marpa::R2::Advanced::Input_Models - Alternative input models
19
20 =head1 About this document
21
22 The alternative input models described in this document are an
23 advanced technique.
24 While you are starting out with Marpa, you
25 probably want to ignore this document.
26 If you are an experienced Marpa user,
27 it is still safe to ignore this document,
28 but you might find the possibilities it discusses
29 interesting.
30
31 This document describes the input models at a conceptual,
32 rather than an implementation level.
33 The reader should be familiar with
34 L<the recognizer's external scanning
35 capability|Marpa::R2::Scanless::R/"External scanning">.
36 A reader interested in implementation is assumed to
37 be familiar with the methods described in
38 L<Marpa::R2::Scanless::R/"Mutators for external scanning">,
39
40 =head1 Marpa has two different ideas of location
41
42 In the other Marpa documentation,
43 we have spoken of "location",
44 and assumed the standard input model.
45 The locations actually in use by the methods
46 described for the standard input model were Earley set
47 ordinals.
48 (An Earley set's ordinal is also its ID.)
49
50 Marpa actually has two different ideas of location --
51 Earley set ordinal and earleme.
52 This is ignored in the other Marpa documents,
53 and it can be, because they
54 assume the standard input model.
55 Use of the standard input model
56 guarantees that earleme
57 and Earley set ordinal
58 will always be exactly the same.
59
60 This document introduces methods which make it
61 possible (and in fact likely) that earleme and
62 Earley set ordinal will differ.
63 From here on,
64 the reader will need to pay careful attention
65 to the distinction.
66
67 =head1 What is an alternative input model?
68
69 An alternative input model
70 is anything that is not the default, token-stream model.
71 More helpfully, Marpa allows variable-length tokens and ambiguous tokens,
72 and an alternative input model is any input model which
73
74 =over
75
76 =item * Allows a token whose length is not exactly 1, or
77
78 =item * Allows locations which have more than one token.
79
80 =back
81
82 To do either of these things,
83 a user must use
84 L<external scanning|Marpa::R2::Scanless::R/"External scanning">
85 and
86 L<the recognizer'x external scanning
87 methods|Marpa::R2::Scanless::R/"Mutators for external scanning">.
88 In particular,
89 if an application is not directly
90 using the recognizer's
91 C<lexeme_read> or
92 C<lexeme_alternative> method calls,
93 that application is not using an alternative input method.
94
95 Many concepts, such as parsing location,
96 parse exhaustion,
97 and the end of parsing,
98 are somewhat more complicated when alternative
99 input models are involved.
100 These concepts were explained in L<the main document for
101 the recognizer|Marpa::R2::Scanless:R> on the assumption
102 that the default input model was in use.
103 This document revises those explanations as necessary
104 to take into
105 account the alternative input models.
106
107 =head1 Token streams
108
109 Marpa's default input model is the traditional one --
110 a token stream.
111 Token streams are very standard in parsing applications --
112 so much so
113 that most texts do not take the trouble
114 of defining the term.
115 A B<token stream> is input structured as
116 a sequence of tokens,
117 where each token occupies one location
118 and every location has a token.
119 In the token stream model, all tokens are
120 of the same length.
121
122 Conventionally, all tokens are of length 1,
123 and the token stream starts at location 0.
124 Following this convention,
125 the I<N>th token would start at
126 location I<N-1> and end
127 at location I<N>.
128 For example,
129 the first token would start at location 0 and end at location 1.
130 The second token would start at location 1 and end at location 2.
131
132 =head1 Earlemes
133
134 For most parsers, position is location in a token stream.
135 To deal with variable-length and overlapping tokens,
136 Marpa needs a more flexible idea of location.
137
138 Marpa's tracks position in terms of B<earlemes>.
139 B<Earlemes> are named after Jay Earley,
140 the inventor of the first algorithm
141 in Marpa's lineage.
142 Every token has a start earleme and an end earleme.
143
144 The token stream model may also be called the token-per-earleme
145 model.
146 In the token stream model,
147 token location and earleme location
148 are exactly identical.
149 More formally, in the token stream model,
150 if the token location is I<N>,
151 then the earleme location is also I<N>.
152 If a user's application uses the token stream model,
153 the user can ignore the existence of earlemes,
154 and can think entirely in terms of
155 tokens and their position in a token stream.
156 Because of this, the main Marpa documents
157 often speak
158 simply of the "location" in the parse.
159
160 =head1 The furthest earleme
161
162 The B<furthest earleme> is the last earleme at which a token ends.
163 In the default input model,
164 the furthest earleme and the current earleme
165 are always the same.
166 As a result,
167 in the default input model, the furthest earleme is not an important
168 concept.
169
170 In alternative input models,
171 tokens may be longer than 1 earleme, and
172 the furthest earleme and the current earleme may be far apart.
173 This becomes an issue when
174 parsing is finished.
175 Alternative input models use
176 one or more calls to the recognizer's
177 L<C<lexeme_complete()>|Marpa::R2::Scanless::R/"lexeme_complete()">
178 method to ensure
179 that processing of input catches up to the furthest earleme.
180
181 =head1 The latest Earley set and latest earleme
182
183 The B<latest earleme> is the earleme location of the latest
184 Earley set.
185 In the default input model, the latest earleme is always the
186 same as the current earleme.
187
188 In alternative input models,
189 there may not be an Earley set at a given earleme location.
190 When that is the case for the current earleme,
191 then the latest Earley set is not at the current earleme,
192 and the latest earleme and current earlemes are different.
193
194 =head1 Ambiguous lexing
195
196 Marpa allows ambiguous tokens.
197 Several Marpa tokens can start at a single parsing location.
198 Ambiguous tokens can be of various lengths.
199 Tokens can also overlap.
200
201 B<Potentially
202 ambiguous lexing>
203 occurs when more than one token starts
204 at a single earleme.
205 When potentially ambiguous lexing occurs,
206 it becomes possible for there to be more
207 than one sequence of tokens.
208
209 An B<actual lexical ambiguity> only occurs if
210 more than one of the potential token sequences is consistent with
211 the grammar.
212 If there is no actual lexical ambiguity,
213 Marpa will use the only token choice that is
214 consistent with the grammar.
215
216 When lexing is B<actually ambiguous>, Marpa
217 will use all the alternatives
218 consistent with the grammar.
219 When the lexing in a parse is actually ambiguous,
220 the parse will be ambiguous.
221 From the point of view of Marpa's semantics,
222 ambiguities caused by lexing look the
223 same as ambiguities caused by an ambiguous grammar.
224
225 In the standard
226 terminology,
227 if a grammar produces more than one parse tree
228 for any input,
229 then that grammar must be ambiguous.
230 In Marpa this is not strictly true.
231 In Marpa,
232 if the input is ambiguous,
233 even an unambiguous grammar can produce more than one parse.
234
235 =head1 Duplicate tokens
236
237 A duplicate token is a token of the same type
238 and the same length as another
239 that was read at the same earleme.
240 Duplicate tokens are impossible in the default, token-stream,
241 model.
242 This is because in the token-stream model only one token can be
243 read at each earleme.
244
245 In alternative models, more than one token may be read at
246 an earleme, and duplicates B<are> possible.
247 Marpa detects duplicate tokens and treats them as
248 "hard errors" --
249 Marpa throws an exception
250 when it sees a duplicate token.
251 Marpa's assumption is that
252 duplicate tokens indicate
253 an error at the application level.
254
255 An application can retry input after
256 a duplicate token, if it
257 catches the exception.
258 In the future, if recovery from duplicate tokens is found
259 to be a useful technique, Marpa may provide an option to change
260 its behavior, so that a soft failure is returned
261 when there is a duplicate token.
262
263 =head1 Earlemes: the details
264
265 While scanning, Marpa keeps track of the B<current earleme>.
266 Earlemes in a parse start at earleme 0 and increase numerically.
267 The earleme immediately following earleme 0 is earleme 1,
268 the earleme immediately following earleme 1 is earleme 2,
269 and so on.
270 The earleme immediately following earleme I<N> is always earleme I<N+1>.
271
272 B<Distance> in the earleme stream is
273 what you would intuitively expect it to be.
274 The distance between earleme I<X> and earleme I<Y> is
275 the absolute value of the difference between I<X> and I<Y>,
276 I<|X-Y|>.
277 The distance from earleme 3 to earleme 6,
278 for example, is 3 earlemes.
279
280 Whenever a token is given to Marpa to be scanned,
281 it starts at the current earleme.
282 In addition to the type and value of the token,
283 Marpa must be told the token's B<length> in earlemes.
284 The length of a Marpa token must be greater than zero.
285
286 This earleme length will become
287 the distance from the start of the
288 token to the end of the token.
289 If the length of the token is I<L>,
290 and the current earleme is I<C>,
291 the end of the token will be at earleme I<C+L>.
292
293 =head1 The character-per-earleme model
294
295 Many different models of the relationship between tokens and earlemes
296 are possible, but two are particularly important.
297 One is the one-token-per-earleme model,
298 which is the default,
299 and which has already been described.
300 The other is the one-character-per-earleme model.
301
302 In the one-character-per-earleme model,
303 every character will be treated as being exactly one
304 earleme in length.
305 If a token is more than one character in length,
306 that token will span earlemes.
307 When the lexing is ambiguous, tokens may overlap.
308
309 When a one-character-per-earleme model of input is used,
310 there may be many earlemes at which no tokens start.
311 For example,
312 in a straightforward character-per-earleme implementation
313 of a grammar for a language that allows
314 comments,
315 no tokens will start at
316 any earlemes which correspond to character locations inside
317 a comment.
318
319 =head1 Other input models
320
321 So far only the token-per-earleme and
322 character-per-earleme models have seen any
323 real use in Marpa programs.
324 But other models are certainly possible.
325 Using earlemes,
326 you can structure your input in almost any way you like.
327
328 There are only three restrictions:
329
330 =over 4
331
332 =item 1
333
334 Scanning always starts at earleme 0.
335
336 =item 2
337
338 All tokens starting at
339 earleme I<N> must be scanned before
340 any tokens starting at earleme I<N+1>.
341 In other words, the tokens must be scanned in non-decreasing order
342 by start earleme.
343
344 =item 3
345
346 Every token must have a length, in earlemes,
347 which is greater than zero.
348 In other words,
349 token length can never
350 be zero or negative.
351
352 =back
353
354 =head1 Copyright and License
355
356 =for Marpa::R2::Display
357 ignore: 1
358
359 Copyright 2022 Jeffrey Kegler
360 This file is part of Marpa::R2. Marpa::R2 is free software: you can
361 redistribute it and/or modify it under the terms of the GNU Lesser
362 General Public License as published by the Free Software Foundation,
363 either version 3 of the License, or (at your option) any later version.
364
365 Marpa::R2 is distributed in the hope that it will be useful,
366 but WITHOUT ANY WARRANTY; without even the implied warranty of
367 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
368 Lesser General Public License for more details.
369
370 You should have received a copy of the GNU Lesser
371 General Public License along with Marpa::R2. If not, see
372 http://www.gnu.org/licenses/.
373
374 =for Marpa::R2::Display::End
375
376 =cut
377