| 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::Recognizer - NAIF recognizers |
| 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 Synopsis |
| 27 |
|
| 28 |
=for Marpa::R2::Display |
| 29 |
name: Engine Synopsis Unambiguous Parse |
| 30 |
partial: 1 |
| 31 |
normalize-whitespace: 1 |
| 32 |
|
| 33 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 34 |
$recce->read( 'Number', 42 ); |
| 35 |
$recce->read('Multiply'); |
| 36 |
$recce->read( 'Number', 1 ); |
| 37 |
$recce->read('Add'); |
| 38 |
$recce->read( 'Number', 7 ); |
| 39 |
|
| 40 |
=for Marpa::R2::Display::End |
| 41 |
|
| 42 |
=head1 Description |
| 43 |
|
| 44 |
This document describes recognizers for Marpa's |
| 45 |
named argument interface (NAIF). |
| 46 |
If you are a beginner, |
| 47 |
or are not sure which interface you are interested in, |
| 48 |
or do not know what the NAIF interfaces is, |
| 49 |
you probably are looking for |
| 50 |
L<the document on recognizers for the SLIF |
| 51 |
interface|Marpa::R2::Scanless::R>. |
| 52 |
|
| 53 |
To create a recognizer object, use L<the C<new> method|/new()>. |
| 54 |
|
| 55 |
To read input, use L<the C<read> method|/read()>. |
| 56 |
|
| 57 |
To evaluate a parse tree, based on the input, use L<the C<value> method|/value()>. |
| 58 |
|
| 59 |
=head2 Token streams |
| 60 |
|
| 61 |
By default, Marpa uses the token-stream model of input. |
| 62 |
The token-stream model is standard -- so standard the most documents about |
| 63 |
parsing do not bother to describe it. |
| 64 |
In the token-stream model, each read adds a token at the current location, |
| 65 |
then advances the current location by one. |
| 66 |
The location before any input is numbered 0 |
| 67 |
and if I<N> tokens are parsed, |
| 68 |
they fill the locations from 1 to I<N>. |
| 69 |
|
| 70 |
This document will describe only the token-stream model of input. |
| 71 |
Marpa allows other models of the input, but their use |
| 72 |
requires special method calls, |
| 73 |
which are described in L<the |
| 74 |
document on alternative input models|Marpa::R2::Deprecated::NAIF::Input_Models>. |
| 75 |
|
| 76 |
=head1 Constructor |
| 77 |
|
| 78 |
=head2 new() |
| 79 |
|
| 80 |
=for Marpa::R2::Display |
| 81 |
name: Engine Synopsis Unambiguous Parse |
| 82 |
partial: 1 |
| 83 |
normalize-whitespace: 1 |
| 84 |
|
| 85 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 86 |
|
| 87 |
=for Marpa::R2::Display::End |
| 88 |
|
| 89 |
The C<new> method creates a recognizer object. |
| 90 |
The C<new> method either returns a new recognizer object or throws an exception. |
| 91 |
|
| 92 |
The arguments to the C<new> method |
| 93 |
are references to hashes of named |
| 94 |
arguments. |
| 95 |
In each key/value pair of these hashes, the key is the argument name, |
| 96 |
and the hash value is the value of the argument. |
| 97 |
The named arguments are described L<below|/"Named arguments">. |
| 98 |
|
| 99 |
=head1 Accessors |
| 100 |
|
| 101 |
=head2 check_terminal() |
| 102 |
|
| 103 |
=for Marpa::R2::Display |
| 104 |
name: Recognizer check_terminal Synopsis |
| 105 |
normalize-whitespace: 1 |
| 106 |
|
| 107 |
my $is_symbol_a_terminal = $recce->check_terminal('Document'); |
| 108 |
|
| 109 |
=for Marpa::R2::Display::End |
| 110 |
|
| 111 |
Returns a Perl true when its argument is the name of a terminal symbol. |
| 112 |
Otherwise, returns a Perl false. |
| 113 |
Not often needed. |
| 114 |
|
| 115 |
=head2 events() |
| 116 |
|
| 117 |
=for Marpa::R2::Display |
| 118 |
name: Recognizer events() Synopsis |
| 119 |
normalize-whitespace: 1 |
| 120 |
|
| 121 |
my @expected_symbols = |
| 122 |
map { $_->[1]; } |
| 123 |
grep { $_->[0] eq 'SYMBOL_EXPECTED' } @{ $recce->events() }; |
| 124 |
|
| 125 |
=for Marpa::R2::Display::End |
| 126 |
|
| 127 |
Returns a reference to an array of the events |
| 128 |
from the last L</read()> method call. |
| 129 |
Each element of the array is a subarray or 1 or 2 elements. |
| 130 |
The first element of the subarray is the name of an event type, |
| 131 |
as described in L</"Recognizer events">. |
| 132 |
The second element is the event value of the event, |
| 133 |
where that is applicable. |
| 134 |
For more detail, see |
| 135 |
L</"Recognizer events">. |
| 136 |
|
| 137 |
=head2 exhausted() |
| 138 |
|
| 139 |
=for Marpa::R2::Display |
| 140 |
name: Recognizer exhausted Synopsis |
| 141 |
normalize-whitespace: 1 |
| 142 |
|
| 143 |
$recce->exhausted() and die 'Recognizer exhausted'; |
| 144 |
|
| 145 |
=for Marpa::R2::Display::End |
| 146 |
|
| 147 |
The C<exhausted> method returns a Perl true if parsing |
| 148 |
in a recognizer is exhausted, and a Perl false |
| 149 |
otherwise. |
| 150 |
Parsing is exhausted when the recognizer will not accept |
| 151 |
any further input. |
| 152 |
By default, a recognizer event occurs if parsing |
| 153 |
is exhausted. |
| 154 |
An attempt to read input into an exhausted parser |
| 155 |
causes an exception to be thrown. |
| 156 |
The recognizer event and the exception are all that |
| 157 |
many applications require, |
| 158 |
but this method allows the recognizer's exhaustion |
| 159 |
status to be discovered directly. |
| 160 |
|
| 161 |
=head2 latest_earley_set() |
| 162 |
|
| 163 |
=for Marpa::R2::Display |
| 164 |
name: latest_earley_set() Synopsis |
| 165 |
normalize-whitespace: 1 |
| 166 |
|
| 167 |
my $latest_earley_set = $recce->latest_earley_set(); |
| 168 |
|
| 169 |
=for Marpa::R2::Display::End |
| 170 |
|
| 171 |
Return the location of the latest (in other words, |
| 172 |
the most recent) |
| 173 |
Earley set. |
| 174 |
In the places where it is most often needed, |
| 175 |
the latest Earley set is the default, |
| 176 |
and there is usually no need to request |
| 177 |
the explicit value |
| 178 |
of the latest Earley set. |
| 179 |
|
| 180 |
=head2 progress() |
| 181 |
|
| 182 |
Given the location (Earley set ID) as its argument, |
| 183 |
returns an array that describes the parse progress |
| 184 |
at that location. |
| 185 |
Details on progress reports can be found in |
| 186 |
L<their own document|Marpa::R2::Deprecated::NAIF::Progress>. |
| 187 |
|
| 188 |
=head2 terminals_expected() |
| 189 |
|
| 190 |
=for Marpa::R2::Display |
| 191 |
name: Recognizer terminals_expected Synopsis |
| 192 |
partial: 1 |
| 193 |
normalize-whitespace: 1 |
| 194 |
|
| 195 |
my $terminals_expected = $recce->terminals_expected(); |
| 196 |
|
| 197 |
=for Marpa::R2::Display::End |
| 198 |
|
| 199 |
Returns a reference to a list of strings, |
| 200 |
where the strings are the |
| 201 |
names of the terminals |
| 202 |
acceptable at the current location. |
| 203 |
In the default input model, the presence of a terminal |
| 204 |
in this list means that terminal will be acceptable |
| 205 |
in the next C<read> method call. |
| 206 |
This is highly useful for Ruby Slippers parsing. |
| 207 |
|
| 208 |
=head1 Mutators |
| 209 |
|
| 210 |
=head2 expected_symbol_event_set() |
| 211 |
|
| 212 |
=for Marpa::R2::Display |
| 213 |
name: Recognizer expected_symbol_event_set() Synopsis |
| 214 |
normalize-whitespace: 1 |
| 215 |
|
| 216 |
$recce->expected_symbol_event_set( 'endmark', 1 ); |
| 217 |
|
| 218 |
=for Marpa::R2::Display::End |
| 219 |
|
| 220 |
Marpa can generate a recognizer event when |
| 221 |
a symbol is expected at the current earleme. |
| 222 |
This method takes a symbol name as its first argument, |
| 223 |
and turns the expected-symbol event for that |
| 224 |
symbol on or off, |
| 225 |
according to whether its second argument is 1 or 0. |
| 226 |
Always succeeds or throws an exception. |
| 227 |
|
| 228 |
Events can occur at location 0 -- when the recognizer is first created. |
| 229 |
However, the event setting of C<expected_symbol_event_set()> |
| 230 |
cannot have an effect until after the first token is read -- |
| 231 |
after location 0. |
| 232 |
In cases where this is an issue, |
| 233 |
the L</event_if_expected> named argument of the |
| 234 |
L</new()> method can be used to set an expected-symbol event. |
| 235 |
|
| 236 |
=head2 read() |
| 237 |
|
| 238 |
=for Marpa::R2::Display |
| 239 |
name: Engine Synopsis Unambiguous Parse |
| 240 |
partial: 1 |
| 241 |
normalize-whitespace: 1 |
| 242 |
|
| 243 |
$recce->read( 'Number', 42 ); |
| 244 |
$recce->read('Multiply'); |
| 245 |
$recce->read( 'Number', 1 ); |
| 246 |
$recce->read('Add'); |
| 247 |
$recce->read( 'Number', 7 ); |
| 248 |
|
| 249 |
=for Marpa::R2::Display::End |
| 250 |
|
| 251 |
The C<read> method reads one token at the current parse location. |
| 252 |
It then advances the current location by 1. |
| 253 |
|
| 254 |
C<read> takes two arguments: a B<token name> and a B<token value>. |
| 255 |
The token name is required. |
| 256 |
It must be the name of a valid terminal symbol. |
| 257 |
The token value is optional. |
| 258 |
It defaults to a Perl C<undef>. |
| 259 |
For details about terminal symbols, |
| 260 |
see L<Marpa::R2::Deprecated::NAIF::Grammar/"Terminal symbols">. |
| 261 |
|
| 262 |
The parser may accept or reject the token. |
| 263 |
If the parser accepted the token, |
| 264 |
the C<read> method returns |
| 265 |
the number of recognizer events that occurred during the |
| 266 |
C<read>. |
| 267 |
For more about events, see |
| 268 |
L</"Recognizer events">. |
| 269 |
|
| 270 |
Marpa may reject a token because it is not one of those |
| 271 |
acceptable at the current location. |
| 272 |
When this happens, C<read> returns a Perl C<undef>. |
| 273 |
A rejected token need not end parsing -- |
| 274 |
it is perfectly possible to retry the C<read> call |
| 275 |
with another token. |
| 276 |
This is, in fact, an important technique in Ruby |
| 277 |
Slippers parsing. |
| 278 |
For details, |
| 279 |
see L<the section on Ruby Slippers |
| 280 |
parsing|/"Ruby Slippers parsing">. |
| 281 |
|
| 282 |
For other failures, |
| 283 |
including an attempt to C<read> a token |
| 284 |
into an exhausted parser, |
| 285 |
Marpa throws an exception. |
| 286 |
|
| 287 |
=head2 set() |
| 288 |
|
| 289 |
=for Marpa::R2::Display |
| 290 |
name: Recognizer set Synopsis |
| 291 |
normalize-whitespace: 1 |
| 292 |
|
| 293 |
$recce->set( { max_parses => 10, } ); |
| 294 |
|
| 295 |
=for Marpa::R2::Display::End |
| 296 |
|
| 297 |
The C<set> method's arguments are references to hashes of named |
| 298 |
arguments. |
| 299 |
The C<set> method |
| 300 |
can be used to set or change named arguments after the recognizer |
| 301 |
has been created. |
| 302 |
Details of the named arguments are L<below|/"Named arguments">. |
| 303 |
|
| 304 |
=head2 value() |
| 305 |
|
| 306 |
=for Marpa::R2::Display |
| 307 |
name: Engine Synopsis Unambiguous Parse |
| 308 |
partial: 1 |
| 309 |
normalize-whitespace: 1 |
| 310 |
|
| 311 |
my $value_ref = $recce->value; |
| 312 |
my $value = $value_ref ? ${$value_ref} : 'No Parse'; |
| 313 |
|
| 314 |
=for Marpa::R2::Display::End |
| 315 |
|
| 316 |
Because Marpa parses ambiguous grammars, every parse |
| 317 |
is a series of zero or more parse trees. |
| 318 |
There are zero parse trees if there was no valid parse |
| 319 |
of the input according to the grammar. |
| 320 |
|
| 321 |
The C<value> method call evaluates the next parse tree |
| 322 |
in the parse series, |
| 323 |
and returns a reference to the parse result for that parse tree. |
| 324 |
If there are no more parse trees, |
| 325 |
the C<value> method returns C<undef>. |
| 326 |
|
| 327 |
=head2 reset_evaluation() |
| 328 |
|
| 329 |
=for Marpa::R2::Display |
| 330 |
name: reset_evaluation Synopsis |
| 331 |
normalize-whitespace: 1 |
| 332 |
|
| 333 |
$recce->reset_evaluation(); |
| 334 |
$recce->set( { end => $loc, max_parses => 999, } ); |
| 335 |
|
| 336 |
=for Marpa::R2::Display::End |
| 337 |
|
| 338 |
The C<reset_evaluation()> method ends a parse series, |
| 339 |
and starts another. |
| 340 |
It can be used to "restart" the parse series. |
| 341 |
Restarting the parse series with |
| 342 |
the C<reset_evaluation()> method |
| 343 |
allows the |
| 344 |
application to specify new |
| 345 |
values for |
| 346 |
the C<closures>, |
| 347 |
C<end> |
| 348 |
and C<ranking_method> named arguments. |
| 349 |
Once a parse series is underway, |
| 350 |
these values cannot be changed. |
| 351 |
|
| 352 |
The most common use for |
| 353 |
C<reset_evaluation()> method |
| 354 |
is to parse |
| 355 |
a single input stream |
| 356 |
at different end points. |
| 357 |
This can also be done by creating a new recognizer |
| 358 |
and re-reading the input |
| 359 |
from the beginning, |
| 360 |
but it is much more efficient to |
| 361 |
evaluate a single recognizer run |
| 362 |
several times, |
| 363 |
using different parse end locations. |
| 364 |
After the parse is restarted using |
| 365 |
the C<reset_evaluation()> method, |
| 366 |
the recognizer's C<set()> |
| 367 |
method |
| 368 |
and its C<end> named argument |
| 369 |
can be used to change the |
| 370 |
parse end location. |
| 371 |
|
| 372 |
=head1 Trace accessors |
| 373 |
|
| 374 |
=head2 show_progress() |
| 375 |
|
| 376 |
=for Marpa::R2::Display |
| 377 |
name: show_progress Synopsis |
| 378 |
partial: 1 |
| 379 |
normalize-whitespace: 1 |
| 380 |
|
| 381 |
print $recce->show_progress() |
| 382 |
or die "print failed: $ERRNO"; |
| 383 |
|
| 384 |
=for Marpa::R2::Display::End |
| 385 |
|
| 386 |
Returns a string describing the progress of the parse. |
| 387 |
With no arguments, |
| 388 |
the string contains reports for |
| 389 |
the current location. |
| 390 |
With a single integer argument I<N>, |
| 391 |
the string contains reports for location I<N>. |
| 392 |
With two numeric arguments, I<N> and I<M>, the arguments are interpreted |
| 393 |
as a range of locations and the returned string contains |
| 394 |
reports for all locations in the range. |
| 395 |
("Location" as referred to in this section, |
| 396 |
and elsewhere |
| 397 |
in this document, |
| 398 |
is what is also called the Earley set ID.) |
| 399 |
|
| 400 |
If an argument is negative, |
| 401 |
I<-N>, |
| 402 |
it indicates |
| 403 |
the I<N>th location counting backward |
| 404 |
from the furthest location of the parse. |
| 405 |
For example, if 42 was the furthest location, |
| 406 |
-1 would be location 42 and -2 would be location 41. |
| 407 |
For example, the method call |
| 408 |
C<< $recce->show_progress(-3, -1) >> |
| 409 |
returns reports for the last three locations of the parse. |
| 410 |
The method call C<< $recce->show_progress(0, -1) >> |
| 411 |
will print progress reports for the entire parse. |
| 412 |
|
| 413 |
C<show_progress> is |
| 414 |
Marpa's most powerful tool for |
| 415 |
debugging application grammars. |
| 416 |
It can also be used to track the |
| 417 |
progress of a parse or |
| 418 |
to investigate how a parse works. |
| 419 |
A much fuller description, |
| 420 |
with an example, |
| 421 |
is in |
| 422 |
L<the document on debugging Marpa |
| 423 |
grammars|Marpa::R2::Deprecated::NAIF::Progress>. |
| 424 |
|
| 425 |
=head1 Named arguments |
| 426 |
|
| 427 |
The recognizer's named arguments are |
| 428 |
accepted by its |
| 429 |
C<new> and C<set> methods. |
| 430 |
|
| 431 |
=head2 closures |
| 432 |
|
| 433 |
The value of C<closures> named argument |
| 434 |
must be |
| 435 |
a reference to a hash. |
| 436 |
In each key/value pair of this hash, |
| 437 |
the key must be an action name. |
| 438 |
The hash value |
| 439 |
must be a CODE ref. |
| 440 |
The C<closures> named argument |
| 441 |
is not allowed once evaluation has begun. |
| 442 |
|
| 443 |
When an action name is a key in |
| 444 |
the |
| 445 |
C<closures> named argument, |
| 446 |
the usual action resolution mechanism of the semantics |
| 447 |
is bypassed. |
| 448 |
One common use of |
| 449 |
the C<closures> named argument is to |
| 450 |
allow anonymous |
| 451 |
subroutines to be semantic actions. |
| 452 |
For more details, see L<the document on |
| 453 |
semantics|Marpa::R2::Deprecated::NAIF::Semantics>. |
| 454 |
|
| 455 |
=head2 end |
| 456 |
|
| 457 |
The C<end> named argument |
| 458 |
specifies the parse end location. |
| 459 |
The default is for the parse to end where the input did, |
| 460 |
so that the parse returned is of the entire input. |
| 461 |
The C<end> named argument is not allowed |
| 462 |
once evaluation has begun. |
| 463 |
"Location" as referred to here and elsewhere |
| 464 |
in this document is what is also called |
| 465 |
an Earley set ID. |
| 466 |
|
| 467 |
=head2 event_if_expected |
| 468 |
|
| 469 |
The value of the |
| 470 |
C<event_if_expected> named argument |
| 471 |
must be a reference to an array |
| 472 |
of symbol names. |
| 473 |
Expected-symbol events will be turned on for those symbol |
| 474 |
names. |
| 475 |
Expected-symbol events may be turned off (or back on) |
| 476 |
using the L</expected_symbol_event_set()> method. |
| 477 |
The advantage of the C<event_if_expected> named argument |
| 478 |
is that it takes effect as soon as the recognizer is created, |
| 479 |
while events set |
| 480 |
using the L</expected_symbol_event_set()> method |
| 481 |
cannot occur until after the first token is read. |
| 482 |
|
| 483 |
=head2 grammar |
| 484 |
|
| 485 |
The C<new> method is required to have |
| 486 |
a C<grammar> named argument. Its |
| 487 |
value must be |
| 488 |
a precomputed Marpa grammar object. |
| 489 |
The C<grammar> named argument is not allowed anywhere else. |
| 490 |
|
| 491 |
=head2 max_parses |
| 492 |
|
| 493 |
If non-zero, causes a fatal error when that number |
| 494 |
of parse results is exceeded. |
| 495 |
C<max_parses> is useful to |
| 496 |
limit CPU usage and output length when testing |
| 497 |
and debugging. |
| 498 |
Stable and production applications may |
| 499 |
prefer to count the number of parses, |
| 500 |
and take a less Draconian response when the |
| 501 |
count is exceeded. |
| 502 |
|
| 503 |
The value must be an integer. |
| 504 |
If it is zero, there will be no |
| 505 |
limit on the number of parse results returned. |
| 506 |
The default is for |
| 507 |
there to be no limit. |
| 508 |
|
| 509 |
=head2 ranking_method |
| 510 |
|
| 511 |
The value must be a string: |
| 512 |
one of "C<none>", |
| 513 |
"C<rule>", |
| 514 |
or "C<high_rule_only>". |
| 515 |
When the value is "C<none>", Marpa returns the parse results |
| 516 |
in arbitrary order. |
| 517 |
This is the default. |
| 518 |
The C<ranking_method> named argument is not allowed |
| 519 |
once evaluation has begun. |
| 520 |
|
| 521 |
The "C<rule>" |
| 522 |
and "C<high_rule_only>" ranking methods |
| 523 |
allows the user |
| 524 |
to control the order |
| 525 |
in which parse results are returned by |
| 526 |
the C<value> method, |
| 527 |
and to exclude some parse results from the parse series. |
| 528 |
For details, see L<the document |
| 529 |
on parse order|Marpa::R2::Deprecated::NAIF::Semantics::Order>. |
| 530 |
|
| 531 |
=head2 too_many_earley_items |
| 532 |
|
| 533 |
The C<too_many_earley_items> argument is optional. |
| 534 |
If specified, it sets the B<Earley item warning threshold>. |
| 535 |
If an Earley set becomes larger than the |
| 536 |
Earley item warning threshold, |
| 537 |
a recognizer event is generated, |
| 538 |
and |
| 539 |
a warning is printed to the trace file handle. |
| 540 |
|
| 541 |
Marpa parses from any BNF, |
| 542 |
and can handle grammars and inputs which produce large |
| 543 |
Earley sets. |
| 544 |
But parsing that involves large Earley sets can be slow. |
| 545 |
Large Earley sets |
| 546 |
are something most applications can, |
| 547 |
and will wish to, avoid. |
| 548 |
|
| 549 |
By default, Marpa calculates |
| 550 |
an Earley item warning threshold |
| 551 |
based on the size of the |
| 552 |
grammar. |
| 553 |
The default threshold will never be less than 100. |
| 554 |
If the Earley item warning threshold is set to 0, |
| 555 |
no recognizer event is generated, |
| 556 |
and |
| 557 |
warnings about large Earley sets are turned off. |
| 558 |
|
| 559 |
=head2 trace_actions |
| 560 |
|
| 561 |
The |
| 562 |
C<trace_actions> named argument |
| 563 |
is a boolean. |
| 564 |
If the boolean value is true, Marpa prints tracing information |
| 565 |
as it resolves action names to |
| 566 |
Perl closures. |
| 567 |
A boolean value of false turns tracing off, which is the default. |
| 568 |
Traces are written to the trace file handle. |
| 569 |
|
| 570 |
=head2 trace_file_handle |
| 571 |
|
| 572 |
The value is a file handle. |
| 573 |
Traces and warning messages |
| 574 |
go to the trace file handle. |
| 575 |
By default the trace file handle is inherited |
| 576 |
from the grammar used to create the recognizer. |
| 577 |
|
| 578 |
=head2 trace_terminals |
| 579 |
|
| 580 |
Very handy in debugging, and often useful |
| 581 |
even when the problem is not in the lexing. |
| 582 |
The value is a trace level. |
| 583 |
When the trace level is 0, |
| 584 |
tracing of terminals is off. |
| 585 |
This is the default. |
| 586 |
|
| 587 |
At a trace level of 1 or higher, |
| 588 |
Marpa produces a trace message |
| 589 |
for each terminal as it is accepted or rejected |
| 590 |
by the recognizer. |
| 591 |
At a trace level of 2 or higher, |
| 592 |
the trace messages include, for |
| 593 |
every location, a list of the |
| 594 |
terminals expected. |
| 595 |
In practical grammars, output from |
| 596 |
trace level 2 can be voluminous. |
| 597 |
|
| 598 |
=head2 trace_values |
| 599 |
|
| 600 |
The C<trace_values> named argument |
| 601 |
is a numeric trace level. |
| 602 |
If the numeric trace level is 1, Marpa |
| 603 |
prints tracing information |
| 604 |
as values are computed in the evaluation stack. |
| 605 |
A trace level of 0 turns value tracing off, |
| 606 |
which is the default. |
| 607 |
Traces are written to the trace file handle. |
| 608 |
|
| 609 |
=head2 warnings |
| 610 |
|
| 611 |
The value is a boolean. |
| 612 |
Warnings are written to the trace file handle. |
| 613 |
By default, the recognizer's warnings are on. |
| 614 |
Usually, an application will want to leave them on. |
| 615 |
|
| 616 |
=head1 Recognizer events |
| 617 |
|
| 618 |
The recognizer's C<read()> method can generate events. |
| 619 |
To access events, use the recognizer's L</events()> method. |
| 620 |
|
| 621 |
The C<EARLEY_ITEM_THRESHOLD> and |
| 622 |
The C<EXHAUSTED> events are enabled by default. |
| 623 |
Events optionally have an "event value", |
| 624 |
as specified in the description of each event. |
| 625 |
The following events are possible. |
| 626 |
|
| 627 |
=head2 EARLEY_ITEM_THRESHOLD |
| 628 |
|
| 629 |
The Earley item threshold was exceeded. |
| 630 |
For more about the |
| 631 |
Earley item warning threshold, |
| 632 |
see L</too_many_earley_items>. |
| 633 |
No event value is defined for this event. |
| 634 |
This event is enabled by default. |
| 635 |
|
| 636 |
=head2 EXHAUSTED |
| 637 |
|
| 638 |
"Exhaustion" |
| 639 |
means that the next C<read> call must fail, |
| 640 |
because there is no token that will be acceptable to it. |
| 641 |
More details on "exhaustion" are in L<a |
| 642 |
section below|/"Parse exhaustion">. |
| 643 |
No event value is defined for this event. |
| 644 |
This event is enabled by default. |
| 645 |
|
| 646 |
=head2 SYMBOL_EXPECTED |
| 647 |
|
| 648 |
A "symbol expected" event means that a symbol is expected |
| 649 |
at that point. |
| 650 |
The event value of this event is the symbol |
| 651 |
whose expectation caused the event. |
| 652 |
This event is disabled by default. |
| 653 |
For details, see L</expected_symbol_event_set()>. |
| 654 |
|
| 655 |
=head1 Parse exhaustion |
| 656 |
|
| 657 |
A parse is B<exhausted> when it will accept no more input. |
| 658 |
An B<exhausted> parse is not necessarily a failed parse. |
| 659 |
Grammars are often written so that once they "find what |
| 660 |
they are looking for", no further input is acceptable. |
| 661 |
Grammars of that kind become exhausted when they succeed. |
| 662 |
|
| 663 |
By default, |
| 664 |
a recognizer event occurs whenever the parse is |
| 665 |
exhausted. |
| 666 |
An application can also check for exhaustion |
| 667 |
explicitly, using the recognizer's |
| 668 |
L<C<exhausted> method|/exhausted()>. |
| 669 |
|
| 670 |
=head1 Ruby Slippers parsing |
| 671 |
|
| 672 |
=for Marpa::R2::Display |
| 673 |
name: Engine Synopsis Interactive Parse |
| 674 |
partial: 1 |
| 675 |
normalize-whitespace: 1 |
| 676 |
|
| 677 |
$recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 678 |
|
| 679 |
my @tokens = ( |
| 680 |
[ 'Number', 42 ], |
| 681 |
['Multiply'], [ 'Number', 1 ], |
| 682 |
['Add'], [ 'Number', 7 ], |
| 683 |
); |
| 684 |
|
| 685 |
TOKEN: for ( my $token_ix = 0; $token_ix <= $#tokens; $token_ix++ ) { |
| 686 |
defined $recce->read( @{ $tokens[$token_ix] } ) |
| 687 |
or fix_things( $recce, $token_ix, \@tokens ) |
| 688 |
or die q{Don't know how to fix things}; |
| 689 |
} |
| 690 |
|
| 691 |
=for Marpa::R2::Display::End |
| 692 |
|
| 693 |
Marpa is able to tell the application |
| 694 |
which symbols are acceptable as tokens at the next location |
| 695 |
in the parse. |
| 696 |
The L<C<terminals_expected> method|/terminals_expected()> |
| 697 |
returns the list of tokens that B<will> be accepted by |
| 698 |
the next C<read>. |
| 699 |
The application can use this information to change the |
| 700 |
input "on the fly" |
| 701 |
so that it is acceptable to the parser. |
| 702 |
|
| 703 |
An application can also take a "try it and see" |
| 704 |
approach. |
| 705 |
If an application is not sure whether a token is |
| 706 |
acceptable or not, the application can |
| 707 |
try to read the dubious token using |
| 708 |
L<the C<read> method|/read()>. |
| 709 |
If the token is rejected, |
| 710 |
L<the C<read> method|/read()> call will return a |
| 711 |
Perl C<undef>. |
| 712 |
At that point, |
| 713 |
the application can retry the C<read> with a different token. |
| 714 |
|
| 715 |
=head2 An example |
| 716 |
|
| 717 |
Marpa's HTML parser, L<Marpa::HTML>, is |
| 718 |
an example of how Ruby Slippers parsing can help |
| 719 |
with a non-trivial, real-life application. |
| 720 |
When a token is rejected in L<Marpa::HTML>, it changes |
| 721 |
the input to match |
| 722 |
the parser's expectations by |
| 723 |
|
| 724 |
=over |
| 725 |
|
| 726 |
=item * Modifying existing tokens, and |
| 727 |
|
| 728 |
=item * Creating new tokens. |
| 729 |
|
| 730 |
=back |
| 731 |
|
| 732 |
The second technique, the creation of |
| 733 |
new "virtual" tokens, |
| 734 |
is used |
| 735 |
by L<Marpa::HTML> |
| 736 |
to deal with omitted start and end tags. |
| 737 |
The actual HTML grammar that |
| 738 |
L<Marpa::HTML> uses takes |
| 739 |
an oversimplified view of the HTML -- |
| 740 |
it assumes, |
| 741 |
even when the HTML standards do not require it, |
| 742 |
that start and end tags are always present. |
| 743 |
For most HTML files of interest, |
| 744 |
this assumption will be |
| 745 |
contrary to fact. |
| 746 |
|
| 747 |
Ruby Slippers parsing is used to make the grammar's |
| 748 |
over-simplistic view of the world come true for it. |
| 749 |
Whenever a token is rejected, |
| 750 |
L<Marpa::HTML> looks at the expected tokens list. |
| 751 |
If it sees that a start or end tag is expected, |
| 752 |
L<Marpa::HTML> creates a token for it -- |
| 753 |
a completely new "virtual" token that gives the parser exactly what it expects. |
| 754 |
L<Marpa::HTML> then resumes input at the point in the original input stream |
| 755 |
where it left off. |
| 756 |
|
| 757 |
=head1 Copyright and License |
| 758 |
|
| 759 |
=for Marpa::R2::Display |
| 760 |
ignore: 1 |
| 761 |
|
| 762 |
Copyright 2022 Jeffrey Kegler |
| 763 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 764 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 765 |
General Public License as published by the Free Software Foundation, |
| 766 |
either version 3 of the License, or (at your option) any later version. |
| 767 |
|
| 768 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 769 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 770 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 771 |
Lesser General Public License for more details. |
| 772 |
|
| 773 |
You should have received a copy of the GNU Lesser |
| 774 |
General Public License along with Marpa::R2. If not, see |
| 775 |
http://www.gnu.org/licenses/. |
| 776 |
|
| 777 |
=for Marpa::R2::Display::End |
| 778 |
|
| 779 |
=cut |
| 780 |
|
| 781 |
# Local Variables: |
| 782 |
# mode: cperl |
| 783 |
# cperl-indent-level: 4 |
| 784 |
# fill-column: 100 |
| 785 |
# End: |
| 786 |
# vim: expandtab shiftwidth=4: |