| 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::Deprecated::NAIF::Input_Models - Other NAIF input models |
| 19 |
|
| 20 |
=head1 THE NAIF INTERFACE IS DEPRECATED |
| 21 |
|
| 22 |
This document describes the NAIF interface, |
| 23 |
which is deprecated. |
| 24 |
PLEASE DO NOT USE IT FOR NEW DEVELOPMENT. |
| 25 |
|
| 26 |
=head1 About this document |
| 27 |
|
| 28 |
The alternative input models described in this document are an |
| 29 |
advanced technique. |
| 30 |
If you are starting out with Marpa, you |
| 31 |
probably want to ignore this document. |
| 32 |
If you are an experienced Marpa user, |
| 33 |
it is still safe to ignore this document, |
| 34 |
but you might find the possibilities it discusses |
| 35 |
interesting. |
| 36 |
|
| 37 |
=head1 Marpa has two different ideas of location |
| 38 |
|
| 39 |
In the other Marpa documentation, |
| 40 |
we have spoken of "location", |
| 41 |
and assumed the standard input model. |
| 42 |
The locations actually in use by the methods |
| 43 |
described for the standard input model were Earley set |
| 44 |
ordinals. |
| 45 |
(An Earley set's ordinal is also its ID.) |
| 46 |
|
| 47 |
Marpa actually has two different ideas of location -- |
| 48 |
Earley set ordinal and earleme. |
| 49 |
This is ignored in the other Marpa documents, |
| 50 |
and it can be, because they |
| 51 |
assume the standard input model. |
| 52 |
Use of the standard input model |
| 53 |
guarantees that earleme |
| 54 |
and Earley set ordinal |
| 55 |
will always be exactly the same. |
| 56 |
|
| 57 |
This document introduces methods which make it |
| 58 |
possible (and in fact likely) that earleme and |
| 59 |
Earley set ordinal will differ. |
| 60 |
From here on, |
| 61 |
the reader will need to pay careful attention |
| 62 |
to the distinction. |
| 63 |
|
| 64 |
=head1 What is an alternative input model? |
| 65 |
|
| 66 |
An alternative input model |
| 67 |
is anything that is not the default, token-stream model. |
| 68 |
More helpfully, Marpa allows variable-length tokens and ambiguous tokens, |
| 69 |
and an alternative input model is any input model which |
| 70 |
|
| 71 |
=over |
| 72 |
|
| 73 |
=item * Allows a token whose length is not exactly 1, or |
| 74 |
|
| 75 |
=item * Allows locations which have more than one token. |
| 76 |
|
| 77 |
=back |
| 78 |
|
| 79 |
To do either of these things, |
| 80 |
a user must use the recognizer's L<C<alternative>|/"alternative()"> |
| 81 |
method. |
| 82 |
In other words, |
| 83 |
if an application is not directly |
| 84 |
using the recognizer's C<alternative> method call, |
| 85 |
that application is not using an alternative input method. |
| 86 |
|
| 87 |
Many concepts, such as parsing location, |
| 88 |
parse exhaustion, |
| 89 |
and the end of parsing, |
| 90 |
are somewhat more complicated when alternative |
| 91 |
input models are involved. |
| 92 |
These concepts were explained in L<the main document for |
| 93 |
the recognizer|Marpa::R2::Deprecated::NAIF::Recognizer> on the assumption |
| 94 |
that the default input model was in use. |
| 95 |
This document revises those explanations as necessary |
| 96 |
to take into |
| 97 |
account the alternative input models. |
| 98 |
|
| 99 |
=head1 Token streams |
| 100 |
|
| 101 |
Marpa's default input model is the traditional one -- |
| 102 |
a token stream. |
| 103 |
Token streams are very standard in parsing applications -- |
| 104 |
so much so |
| 105 |
that most texts do not take the trouble |
| 106 |
of defining the term. |
| 107 |
A B<token stream> is input structured as |
| 108 |
a sequence of tokens, |
| 109 |
where each token occupies one location |
| 110 |
and every location has a token. |
| 111 |
In the token stream model, all tokens are |
| 112 |
of the same length. |
| 113 |
|
| 114 |
Conventionally, all tokens are of length 1, |
| 115 |
and the token stream starts at location 0. |
| 116 |
Following this convention, |
| 117 |
the I<N>th token would start at |
| 118 |
location I<N-1> and end |
| 119 |
at location I<N>. |
| 120 |
For example, |
| 121 |
the first token would start at location 0 and end at location 1. |
| 122 |
The second token would start at location 1 and end at location 2. |
| 123 |
|
| 124 |
=head1 Earlemes |
| 125 |
|
| 126 |
For most parsers, position is location in a token stream. |
| 127 |
To deal with variable-length and overlapping tokens, |
| 128 |
Marpa needs a more flexible idea of location. |
| 129 |
|
| 130 |
Marpa's tracks position in terms of B<earlemes>. |
| 131 |
B<Earlemes> are named after Jay Earley, |
| 132 |
the inventor of the first algorithm |
| 133 |
in Marpa's lineage. |
| 134 |
Every token has a start earleme and an end earleme. |
| 135 |
|
| 136 |
The token stream model may also be called the token-per-earleme |
| 137 |
model. |
| 138 |
In the token stream model, |
| 139 |
token location and earleme location |
| 140 |
are exactly identical. |
| 141 |
More formally, in the token stream model, |
| 142 |
if the token location is I<N>, |
| 143 |
then the earleme location is also I<N>. |
| 144 |
If a user's application uses the token stream model, |
| 145 |
the user can ignore the existence of earlemes, |
| 146 |
and can think entirely in terms of |
| 147 |
tokens and their position in a token stream. |
| 148 |
Because of this, the main Marpa documents |
| 149 |
often speak |
| 150 |
simply of the "location" in the parse. |
| 151 |
|
| 152 |
=head1 The furthest earleme |
| 153 |
|
| 154 |
The B<furthest earleme> is the last earleme at which a token ends. |
| 155 |
In the default input model, |
| 156 |
the furthest earleme and the current earleme |
| 157 |
are always the same. |
| 158 |
As a result, |
| 159 |
in the default input model, the furthest earleme is not an important |
| 160 |
concept. |
| 161 |
|
| 162 |
In alternative input models, |
| 163 |
tokens may be longer than 1 earleme, and |
| 164 |
the furthest earleme and the current earleme may be far apart. |
| 165 |
This becomes an issue when |
| 166 |
parsing is finished. |
| 167 |
Alternative input models use |
| 168 |
the recognizer's L<C<end_input>|/"end_input()"> method to ensure |
| 169 |
that processing of input catches up to the furthest earleme. |
| 170 |
|
| 171 |
=head1 The latest Earley set and latest earleme |
| 172 |
|
| 173 |
The B<latest earleme> is the earleme location of the latest |
| 174 |
Earley set. |
| 175 |
In the default input model, the latest earleme is always the |
| 176 |
same as the current earleme. |
| 177 |
|
| 178 |
In alternative input models, |
| 179 |
there may not be an Earley set at a given earleme location. |
| 180 |
When that is the case for the current earleme, |
| 181 |
then the latest Earley set is not at the current earleme, |
| 182 |
and the latest earleme and current earlemes are different. |
| 183 |
|
| 184 |
=head1 Methods |
| 185 |
|
| 186 |
=head2 alternative() |
| 187 |
|
| 188 |
=for Marpa::R2::Display |
| 189 |
name: Recognizer alternative Synopsis |
| 190 |
normalize-whitespace: 1 |
| 191 |
|
| 192 |
$recce->alternative( 'a', \42, 1 ) or return 'First alternative failed'; |
| 193 |
|
| 194 |
=for Marpa::R2::Display::End |
| 195 |
|
| 196 |
The C<alternative> method is the most general method for reading |
| 197 |
input, and is used in alternative input models. |
| 198 |
It takes three arguments, only the first of which is required. |
| 199 |
|
| 200 |
The first two arguments are |
| 201 |
the token type and a reference to the token value. |
| 202 |
If the reference to the token value is omitted, |
| 203 |
or is C<undef>, |
| 204 |
the value of the token will be a Perl C<undef>. |
| 205 |
|
| 206 |
The third argument to the C<alternative> method is a token |
| 207 |
length. |
| 208 |
If omitted, token length defaults to 1, |
| 209 |
which is the correct value for the token stream model. |
| 210 |
Its value can be any integer B<greater> than zero. |
| 211 |
Marpa does not allow zero length tokens in any input model. |
| 212 |
|
| 213 |
Unlike the C<read> method, the C<alternative> method does |
| 214 |
B<not> advance the current location |
| 215 |
(current earleme) on each call. |
| 216 |
This allows the application to read several tokens |
| 217 |
at the same earleme. |
| 218 |
This is how ambiguous input is created. |
| 219 |
To advance the current earleme when |
| 220 |
input is read using the C<alternative> method, |
| 221 |
the C<complete_earleme> method must be called. |
| 222 |
|
| 223 |
On success, C<alternative> returns a Perl true value. |
| 224 |
If the token is rejected, C<alternative> returns a Perl false. |
| 225 |
On other failures, C<alternative> throws an exception. |
| 226 |
|
| 227 |
=head2 current_earleme() |
| 228 |
|
| 229 |
=for Marpa::R2::Display |
| 230 |
name: current_earleme Example |
| 231 |
normalize-whitespace: 1 |
| 232 |
|
| 233 |
$current_earleme = $recce->current_earleme(); |
| 234 |
|
| 235 |
=for Marpa::R2::Display::End |
| 236 |
|
| 237 |
Returns the current parse location, |
| 238 |
also known as the current earleme. |
| 239 |
Not often needed. |
| 240 |
|
| 241 |
=head2 earleme() |
| 242 |
|
| 243 |
=for Marpa::R2::Display |
| 244 |
name: earleme() Synopsis |
| 245 |
normalize-whitespace: 1 |
| 246 |
|
| 247 |
my $origin_earleme = $recce->earleme($origin_earley_set_id); |
| 248 |
|
| 249 |
=for Marpa::R2::Display::End |
| 250 |
|
| 251 |
Given an Earley set ID as its argument, |
| 252 |
the C<earleme()> recognizer method |
| 253 |
returns the corresponding earleme. |
| 254 |
Every integer in the range from 0 |
| 255 |
to the ID of the latest Earley |
| 256 |
is a valid Earley set ID, |
| 257 |
and every valid Earley set ID corresponds to an |
| 258 |
earleme. |
| 259 |
If the argument of C<earleme()> is |
| 260 |
greater than the latest Earley set ID, |
| 261 |
C<earleme()> returns Perl C<undef>. |
| 262 |
|
| 263 |
There is currently no method that translates from |
| 264 |
earleme to Earley set. |
| 265 |
Earley set to earleme translation is a well-behaved |
| 266 |
one-to-one function |
| 267 |
in all input models -- for every Earley set there is |
| 268 |
a earleme, |
| 269 |
and every earleme is mapped to by at most one Earley set. |
| 270 |
Earleme to Earley set translation is far less well-behaved. |
| 271 |
In many input models, it is a partial function -- there |
| 272 |
are some earlemes that are |
| 273 |
in the valid range of earlemes |
| 274 |
but do not map to any Earley set. |
| 275 |
|
| 276 |
Earleme to Earley set translation is often not needed. |
| 277 |
When it is, it can be implemented at the application |
| 278 |
level, with the application taking advantage of what |
| 279 |
it knows about its choice of input model. |
| 280 |
|
| 281 |
=head2 earleme_complete() |
| 282 |
|
| 283 |
=for Marpa::R2::Display |
| 284 |
name: Recognizer earleme_complete Synopsis |
| 285 |
normalize-whitespace: 1 |
| 286 |
|
| 287 |
$recce->earleme_complete(); |
| 288 |
|
| 289 |
=for Marpa::R2::Display::End |
| 290 |
|
| 291 |
Processes all tokens at the current earleme and advances the current |
| 292 |
earleme by 1. |
| 293 |
If the earleme cannot be completed, |
| 294 |
an exception is thrown. |
| 295 |
Otherwise, an "event" count is returned. |
| 296 |
If zero is returned, the earleme was completed without event. |
| 297 |
In this context, |
| 298 |
an "event" is one of a list of occurrences of special interest during |
| 299 |
the successful completion of an earleme, |
| 300 |
as described for |
| 301 |
the L<recognizer's C<read> method|Marpa::R2::Deprecated::NAIF::Recognizer/"read()">. |
| 302 |
|
| 303 |
All tokens read using the C<alternative> method start at one |
| 304 |
location -- the current earleme. |
| 305 |
When reading input using the C<alternative> method, |
| 306 |
C<earleme_complete> is used to complete processing |
| 307 |
at the current earleme and move |
| 308 |
forward in |
| 309 |
the input stream. |
| 310 |
|
| 311 |
C<earleme_complete> may be called even if the C<alternative> |
| 312 |
method has been not called since the last |
| 313 |
call to C<earleme_complete>. |
| 314 |
This will create an earleme with no tokens. |
| 315 |
In certain input models, |
| 316 |
such as the character-per-earleme model, |
| 317 |
this can be useful. |
| 318 |
|
| 319 |
=head2 end_input() |
| 320 |
|
| 321 |
=for Marpa::R2::Display |
| 322 |
name: Recognizer end_input Synopsis |
| 323 |
normalize-whitespace: 1 |
| 324 |
|
| 325 |
$recce->end_input(); |
| 326 |
|
| 327 |
=for Marpa::R2::Display::End |
| 328 |
|
| 329 |
Indicates that input is finished. |
| 330 |
Calling C<end_input> is not necessary |
| 331 |
or useful |
| 332 |
in the default input model, |
| 333 |
because in the default input model no token |
| 334 |
has a length greater than 1. |
| 335 |
|
| 336 |
The C<end_input> method takes no arguments. |
| 337 |
The C<end_input> method returns a Perl true value on success. |
| 338 |
On failure, it throws an exception. |
| 339 |
|
| 340 |
In alternative input models, calling the C<earleme_complete> method |
| 341 |
once input is finished does not |
| 342 |
ensure that all input has been processed. |
| 343 |
The C<earleme_complete> method completes the current earleme, |
| 344 |
but in alternative models, tokens may extend well past the current earleme. |
| 345 |
The C<end_input> method ensures that all input is processed. |
| 346 |
|
| 347 |
Calling C<end_input> multiple times on the same recognizer object |
| 348 |
is harmless, |
| 349 |
but useless. |
| 350 |
The second and subsequent calls will |
| 351 |
return a Perl true, |
| 352 |
but will have no effect. |
| 353 |
|
| 354 |
=head1 Alternative models and C<read()> |
| 355 |
|
| 356 |
A recognizer can mix |
| 357 |
calls to |
| 358 |
its L<C<read>|Marpa::R2::Deprecated::NAIF::Recognizer/"read()"> |
| 359 |
method with calls to its L<C<alternative>|/"alternative()"> |
| 360 |
method. |
| 361 |
The C<read> method has the same effect as a single call |
| 362 |
to the C<alternative> method, followed immediately |
| 363 |
by a call of the C<earleme_complete> method. |
| 364 |
|
| 365 |
=head1 Ambiguous lexing |
| 366 |
|
| 367 |
Marpa allows ambiguous tokens. |
| 368 |
Several Marpa tokens can start at a single parsing location. |
| 369 |
Ambiguous tokens can be of various lengths. |
| 370 |
Tokens can also overlap. |
| 371 |
|
| 372 |
B<Potentially |
| 373 |
ambiguous lexing> |
| 374 |
occurs when more than one token starts |
| 375 |
at a single earleme. |
| 376 |
When potentially ambiguous lexing occurs, |
| 377 |
it becomes possible for there to be more |
| 378 |
than one sequence of tokens. |
| 379 |
|
| 380 |
An B<actual lexical ambiguity> only occurs if |
| 381 |
more than one of the potential token sequences is consistent with |
| 382 |
the grammar. |
| 383 |
If there is no actual lexical ambiguity, |
| 384 |
Marpa will use the only token choice that is |
| 385 |
consistent with the grammar. |
| 386 |
|
| 387 |
When lexing is B<actually ambiguous>, Marpa |
| 388 |
will use all the alternatives |
| 389 |
consistent with the grammar. |
| 390 |
When the lexing in a parse is actually ambiguous, |
| 391 |
the parse will be ambiguous. |
| 392 |
From the point of view of Marpa's semantics, |
| 393 |
ambiguities caused by lexing look the |
| 394 |
same as ambiguities caused by an ambiguous grammar. |
| 395 |
|
| 396 |
In the standard |
| 397 |
terminology, |
| 398 |
if a grammar produces more than one parse tree |
| 399 |
for any input, |
| 400 |
then that grammar must be ambiguous. |
| 401 |
In Marpa this is not strictly true. |
| 402 |
In Marpa, |
| 403 |
if the input is ambiguous, |
| 404 |
even an unambiguous grammar can produce more than one parse. |
| 405 |
|
| 406 |
=head1 Duplicate tokens |
| 407 |
|
| 408 |
A duplicate token is a token of the same type |
| 409 |
and the same length as another |
| 410 |
that was read at the same earleme. |
| 411 |
Duplicate tokens are impossible in the default, token-stream, |
| 412 |
model. |
| 413 |
This is because in the token-stream model only one token can be |
| 414 |
read at each earleme. |
| 415 |
|
| 416 |
In alternative models, more than one token may be read at |
| 417 |
an earleme, and duplicates B<are> possible. |
| 418 |
Marpa detects duplicate tokens and treats them as |
| 419 |
"hard errors" -- |
| 420 |
Marpa throws an exception |
| 421 |
when it sees a duplicate token. |
| 422 |
Marpa's assumption is that |
| 423 |
duplicate tokens indicate |
| 424 |
an error at the application level. |
| 425 |
|
| 426 |
An application can retry input after |
| 427 |
a duplicate token, if it |
| 428 |
catches the exception. |
| 429 |
In the future, if recovery from duplicate tokens is found |
| 430 |
to be a useful technique, Marpa may provide an option to change |
| 431 |
its behavior, so that a soft failure is returned |
| 432 |
when there is a duplicate token. |
| 433 |
|
| 434 |
=head1 Earlemes: the details |
| 435 |
|
| 436 |
While scanning, Marpa keeps track of the B<current earleme>. |
| 437 |
Earlemes in a parse start at earleme 0 and increase numerically. |
| 438 |
The earleme immediately following earleme 0 is earleme 1, |
| 439 |
the earleme immediately following earleme 1 is earleme 2, |
| 440 |
and so on. |
| 441 |
The earleme immediately following earleme I<N> is always earleme I<N+1>. |
| 442 |
|
| 443 |
B<Distance> in the earleme stream is |
| 444 |
what you would intuitively expect it to be. |
| 445 |
The distance between earleme I<X> and earleme I<Y> is |
| 446 |
the absolute value of the difference between I<X> and I<Y>, |
| 447 |
I<|X-Y|>. |
| 448 |
The distance from earleme 3 to earleme 6, |
| 449 |
for example, is 3 earlemes. |
| 450 |
|
| 451 |
Whenever a token is given to Marpa to be scanned, |
| 452 |
it starts at the current earleme. |
| 453 |
In addition to the type and value of the token, |
| 454 |
Marpa must be told the token's B<length> in earlemes. |
| 455 |
The length of a Marpa token must be greater than zero. |
| 456 |
|
| 457 |
This earleme length will become |
| 458 |
the distance from the start of the |
| 459 |
token to the end of the token. |
| 460 |
If the length of the token is I<L>, |
| 461 |
and the current earleme is I<C>, |
| 462 |
the end of the token will be at earleme I<C+L>. |
| 463 |
|
| 464 |
=head1 The character-per-earleme model |
| 465 |
|
| 466 |
Many different models of the relationship between tokens and earlemes |
| 467 |
are possible, but two are particularly important. |
| 468 |
One is the one-token-per-earleme model, |
| 469 |
which is the default, |
| 470 |
and which has already been described. |
| 471 |
The other is the one-character-per-earleme model. |
| 472 |
|
| 473 |
In the one-character-per-earleme model, |
| 474 |
every character will be treated as being exactly one |
| 475 |
earleme in length. |
| 476 |
If a token is more than one character in length, |
| 477 |
that token will span earlemes. |
| 478 |
When the lexing is ambiguous, tokens may overlap. |
| 479 |
|
| 480 |
When a one-character-per-earleme model of input is used, |
| 481 |
there may be many earlemes at which no tokens start. |
| 482 |
For example, |
| 483 |
in a straightforward character-per-earleme implementation |
| 484 |
of a grammar for a language that allows |
| 485 |
comments, |
| 486 |
no tokens will start at |
| 487 |
any earlemes which correspond to character locations inside |
| 488 |
a comment. |
| 489 |
|
| 490 |
=head1 Other input models |
| 491 |
|
| 492 |
So far only the token-per-earleme and |
| 493 |
character-per-earleme models have seen any |
| 494 |
real use in Marpa programs. |
| 495 |
But other models are certainly possible. |
| 496 |
Using earlemes, |
| 497 |
you can structure your input in almost any way you like. |
| 498 |
|
| 499 |
There are only three restrictions: |
| 500 |
|
| 501 |
=over 4 |
| 502 |
|
| 503 |
=item 1 |
| 504 |
|
| 505 |
Scanning always starts at earleme 0. |
| 506 |
|
| 507 |
=item 2 |
| 508 |
|
| 509 |
All tokens starting at |
| 510 |
earleme I<N> must be scanned before |
| 511 |
any tokens starting at earleme I<N+1>. |
| 512 |
In other words, the tokens must be scanned in non-decreasing order |
| 513 |
by start earleme. |
| 514 |
|
| 515 |
=item 3 |
| 516 |
|
| 517 |
Every token must have a length, in earlemes, |
| 518 |
which is greater than zero. |
| 519 |
In other words, |
| 520 |
token length can never |
| 521 |
be zero or negative. |
| 522 |
|
| 523 |
=back |
| 524 |
|
| 525 |
=head1 Copyright and License |
| 526 |
|
| 527 |
=for Marpa::R2::Display |
| 528 |
ignore: 1 |
| 529 |
|
| 530 |
Copyright 2022 Jeffrey Kegler |
| 531 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 532 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 533 |
General Public License as published by the Free Software Foundation, |
| 534 |
either version 3 of the License, or (at your option) any later version. |
| 535 |
|
| 536 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 537 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 538 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 539 |
Lesser General Public License for more details. |
| 540 |
|
| 541 |
You should have received a copy of the GNU Lesser |
| 542 |
General Public License along with Marpa::R2. If not, see |
| 543 |
http://www.gnu.org/licenses/. |
| 544 |
|
| 545 |
=for Marpa::R2::Display::End |
| 546 |
|
| 547 |
=cut |
| 548 |
|