Debian Codegraph @

pod/Algorithm.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::Algorithm - The Earley algorithm
19
20 =head1 Description
21
22 For most purposes, the user does not need to know anything
23 about the Earley algorithm or Marpa algorithm,
24 and can ignore this document.
25 This document describes those aspects of the Earley
26 and Marpa
27 algorithms which
28 are relevant in certain cases,
29 when using Marpa.
30 These cases include
31 L<tracing and
32 debugging|Marpa::R2::Progress.pod>
33 and
34 L<use of the recognizer's ranking
35 methods|Marpa::R2::Rank.pod>.
36
37 Resources for readers who want to know
38 the details of the Marpa algorithm that are
39 beyond the scope of this document are
40 given L<below|/"Resources on the Marpa algorithm">.
41 A reader of this document should
42 be familiar with the basic parsing terminology
43 described in L<Marpa::R2::Vocabulary.pod>.
44
45 Marpa is based on Jay Earley's algorithm for parsing.
46 The idea behind Earley's algorithm is that you can
47 parse by building a table of rules
48 and where you are in those rules.
49 "Where" means two things: location in the rule relative to the rule's
50 symbols,
51 and location relative to the parse's input stream.
52
53 =head1 Dotted Rules
54
55 To understand this document,
56 it is important to understand what a dotted rule is.
57 An acquaintance with
58 dotted rules is also important in understanding
59 Marpa's progress reports.
60 Dotted rules
61 are thoroughly described
62 L<in the progress report
63 documentation|Marpa::R2::Progress>.
64 This section repeats the main ideas
65 from the perspective of this document.
66
67 Recall that a rule is a B<LHS> (left hand side)
68 and a B<RHS> (right hand side).
69 The B<LHS> is always exactly one symbol.
70 The B<RHS> is zero or more symbols.
71 Consider the following example of a rule,
72 given in the syntax of Marpa's DSL.
73
74 =for Marpa::R2::Display
75 ignore: 1
76
77 S ::= A B C
78
79 =for Marpa::R2::Display
80
81 Dotted rules are used to track the
82 progress of a parse through a rule.
83 Within a rule, position relative to the symbols of the rule
84 is traditionally indicated with a dot.
85 In fact, the symbol-relative
86 rule position is usually called the B<dot location>.
87 Taken as
88 a pair, a rule and a dot location are called a B<dotted rule>.
89 The symbol before the dot is called
90 the B<predot symbol>.
91
92 In parsing this rule, we can be at any of four possible locations.
93 One location is at the beginning, before all of the symbols.
94 The other three locations are immediately after each of the rule's
95 three symbols.
96 The following is an example of a dotted rule,
97 with the dot after the second RHS symbol.
98
99 =for Marpa::R2::Display
100 ignore: 1
101
102 S ::= A B . C
103
104 =for Marpa::R2::Display
105
106 In this rule, C<B> is the B<predot symbol>.
107 This rule indicates that we have recognized
108 an C<A>, followed by a C<B>,
109 but that we have yet to see a C<C>.
110
111 When the dot is after the last symbol of the RHS,
112 the dotted rule is called a B<completed rule>,
113 or a B<completion>.
114 A completion indicates that a rule has been fully recognized.
115
116 Here is the completion for the above rule:
117
118 =for Marpa::R2::Display
119 ignore: 1
120
121 S ::= A B C .
122
123 =for Marpa::R2::Display
124
125 In this completion example, the symbol C<C>
126 is the predot symbol.
127
128 In every grammar,
129 a special rule is reserved as
130 the start rule.
131 When the rule of a dotted rule is the start rule,
132 and the dot is before the first symbol of the RHS,
133 the dotted rule is the B<start dotted rule>.
134 The start dotted rule does not have a predot symbol.
135
136 When the dot is before the first symbol of the RHS,
137 and the rule is not the start rule,
138 the dotted rule is called a B<predicted rule>,
139 or a B<prediction>.
140 Here is the prediction of the rule we've been
141 using for our examples:
142
143 =for Marpa::R2::Display
144 ignore: 1
145
146 S ::= . A B C
147
148 =for Marpa::R2::Display
149
150 A prediction indicates
151 that we have not yet recognized any of the symbols in the rule.
152 All a prediction does is to predict that the rule will occur.
153 In predictions, there is no predot symbol.
154
155 =head1 Earley items
156
157 The dotted rules contain
158 all but one piece of the information
159 that Marpa
160 needs to track.
161 The missing piece is the second of the two "wheres": where in the input stream.
162 To associate input stream location and dotted rules, Marpa uses what are now called Earley items.
163
164 A convenient way to think of an B<Earley item> is as a triple, or 3-tuple,
165 consisting of dotted rule, origin and current location.
166 The B<origin> is the location in the input stream where the dotted rule starts.
167 The B<current location>
168 is the location in the input stream which corresponds to the
169 dot position.
170
171 Two noteworthy consequences
172 follow from the way in which origin and current location are defined.
173 First,
174 if a dotted rule is a prediction,
175 then origin and current location will always be the same.
176 Second,
177 the location where a rule ends is not tracked unless the
178 dotted rule is a completion.
179 If its dotted rule is not a completion,
180 an Earley item does not tell us
181 if a rule will
182 ever be completed,
183 much less at which location.
184
185 =head1 Confluences
186
187 In every parse,
188 the Marpa algorithm starts
189 with
190
191 =over 4
192
193 =item *
194
195 an B<Earley table>
196 containing
197 a B<start Earley item>, and
198
199 =item *
200
201 a sequence of sets of tokens.
202
203 =back
204
205 The Marpa algorithm then performs one of a set of operations until no more Earley items
206 can be added to the Earley table.
207 (We will not describe these operations in detail here.
208 See L<below|Resources on the Marpa algorithm>.)
209
210 If we count addition of the start Earley item as an "operation",
211 this means that every Earley item has at least one reason for
212 being in the Earley table.
213 (Duplicate Earley items are not added to the table,
214 so that, in an ambiguous parse,
215 a single Earley item may have more than one reason
216 for being in the table.)
217
218 For describing the reasons for the presence of Earley items
219 in the Earley table, we use hydrological terminology.
220 Intuitively, the tokens and/or Earley items are
221 "upstream" from the Earley items they cause.
222
223 Every Earley item has one or more B<confluences> which
224 record the reason for
225 that Earley item's presence in the Earley table.
226 Each confluence has two B<inflows>,
227 but both of the inflows may be B<ill-defined>.
228
229 The first inflow is the B<mainstem>.
230 When well-defined, a mainstem is always an Earley item.
231 The second inflow is the B<tributary>.
232 When well-defined, a tributary is an Earley item or a
233 token.
234
235 =over 4
236
237 =item *
238
239 The start Earley item always has exactly one
240 confluence.
241 Both inflows of its confluence are ill-defined.
242
243 =item *
244
245 A predicted Earley item may have many confluences.
246 The mainstem of a predicted Earley item is always
247 another Earley item.
248 The tributary of a predicted Earley item is always
249 ill-defined.
250
251 =item *
252
253 An Earley item whose predot symbol is a terminal is
254 a scanned Earley item.
255 A scanned Earley item may have multiple confluences.
256 The mainstem of a scanned Earley item is always
257 another Earley item.
258 The tributary of a scanned Earley item is always
259 a token.
260
261 =item *
262
263 An Earley item whose predot symbol is a non-terminal is
264 a reduction.
265 A reduction may have many confluences.
266 The mainstem of a reduction is always
267 another Earley item.
268 The tributary of a reduction is always
269 a completed Earley item.
270
271 =back
272
273 =head1 Resources on the Marpa algorithm
274
275 This document is not intended as an full explanation
276 of the workings of the Marpa algorithm.
277 Those who want to learn more about the Marpa
278 algorithm,
279 and who are not already familiar with Earley's
280 algorithm,
281 should consult an introductory presentation of
282 it.
283 One good start is the
284 Wikipedia article
285 L<https://en.wikipedia.org/wiki/Earley_parser>.
286 For the workings of the Marpa algorithm
287 there are L<two arxiv.org
288 papers|Marpa::R2::Advanced::Bibliography/"Marpa papers">.
289
290 =head1 Earley item correctness
291
292 For the definition of Earley item correctness,
293 we show symbols in angle brackets, for example C<< <A> >>,
294 and sentential forms in double angle brackets, for example C<< <<alpha>> >>.
295
296 Without loss of generality, we call
297 our grammar C<G> and we call our input C<W>.
298 The input is a sequence of tokens.
299 C<W[i]> is the C<i>'th terminal of C<W>,
300 so that the first terminal of C<W> is
301 C<W[0]>.
302 We write C<W[i...j]> for the segment of C<W> whose
303 first term is C<W[i]> and whose last term is C<W[j]>.
304 As a special case, C<W[a...b]> is the empty string
305 if C<b> is less than C<a>.
306
307 Let C<< <<alpha>> =>* <<beta>> >> mean that,
308 using grammar C<G>,
309 the sentential form C<< <<alpha>> >>
310 derives the sentential form C<< <<beta>> >>
311 in zero or more steps.
312 We recall from L<the vocabulary
313 document|Marpa::R2::Vocabulary>
314 that a sentential form is a sequence of zero or
315 more symbols from the grammar.
316 In a derivation, a single symbol represents the sentential form
317 of length 1 consisting only of that symbol.
318
319 Also without loss of generality,
320 we let the grammar C<G> be such that we have the following:
321
322 =over 4
323
324 =item *
325
326 C<Syms> is the set of symbols in C<G>.
327
328 =item *
329
330 Exactly one of the symbols
331 in C<Syms> is distinguished as the
332 "start symbol".
333 Call the "start symbol", C<< <Start> >>.
334
335 =item *
336
337 Let C<Terms> be a subset of C<Syms>
338 called the "terminals" of C<G>.
339 The symbols of the tokens in the input C<W> are
340 elements of C<Terms>.
341
342 =item *
343
344 C<G> contains the rule
345
346 <A> ::= <<beta>> <<gamma>>
347
348 where C<< <<beta>> >> and C<< <<gamma>> >> are sentential forms.
349
350 =back
351
352 We say the Earley item
353
354 [ [ <A> ::= <<beta>> . <<gamma>> ], origin, current ]
355
356 is B<correct> if and only if,
357 where C<< <<alpha>> >> and C<< <<delta>> >> are sentential forms,
358 we have all of the following:
359
360 1. <Start> => <<alpha>> <A> <<delta>>
361
362 2. <<alpha>> => W[0...(origin-1)]
363
364 3. <<beta>> => W[origin...(current-1)]
365
366 A proof that the Earley algorithm adds all correct Earley items,
367 and only correct Earley items,
368 to the Earley table
369 can be found in L<Jay Earley's original
370 paper|Marpa::R2::Advanced::Bibliography/"Earley 1970">,
371 and in L<Aho and Ullmann's 1972
372 textbook|Marpa::R2::Advanced::Bibliography/"Aho and Ullman 1972">.
373
374 =head1 Copyright and License
375
376 =for Marpa::R2::Display
377 ignore: 1
378
379 Copyright 2022 Jeffrey Kegler
380 This file is part of Marpa::R2. Marpa::R2 is free software: you can
381 redistribute it and/or modify it under the terms of the GNU Lesser
382 General Public License as published by the Free Software Foundation,
383 either version 3 of the License, or (at your option) any later version.
384
385 Marpa::R2 is distributed in the hope that it will be useful,
386 but WITHOUT ANY WARRANTY; without even the implied warranty of
387 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
388 Lesser General Public License for more details.
389
390 You should have received a copy of the GNU Lesser
391 General Public License along with Marpa::R2. If not, see
392 http://www.gnu.org/licenses/.
393
394 =for Marpa::R2::Display::End
395
396 =cut
397