| 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::Progress - Progress reports for the NAIF |
| 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 |
This document describes |
| 29 |
Marpa's progress reports for its named argument interface (NAIF). |
| 30 |
If you are a beginner, |
| 31 |
or are not sure which interface you are interested in, |
| 32 |
or do not know what the NAIF interfaces is, |
| 33 |
you probably are looking for |
| 34 |
L<the document on progress reports for the SLIF |
| 35 |
interface|Marpa::R2::Progress>. |
| 36 |
|
| 37 |
Progress reports allow an application to know |
| 38 |
exactly where it is in the parse at any |
| 39 |
point. |
| 40 |
For parse locations of the user's choosing, |
| 41 |
progress reports list all the rules in play, |
| 42 |
and indicate the location at which the rule started, |
| 43 |
and how far into the rule parsing has progressed. |
| 44 |
|
| 45 |
For those new to parsing, this "situational awareness" |
| 46 |
might seem to be a feature that they can expect |
| 47 |
from any servicable parser. |
| 48 |
In fact, |
| 49 |
situation awareness in most parsers is extremely limited, |
| 50 |
and it tends to be more limited in those |
| 51 |
parsers considered production quality. |
| 52 |
Marpa is not just unusual in the amount of feedback |
| 53 |
it offers the application, |
| 54 |
it breaks new ground. |
| 55 |
|
| 56 |
Progress reports are extremely useful in |
| 57 |
debugging grammars. |
| 58 |
Because debugging creates a |
| 59 |
clear "narrative", |
| 60 |
the detailed example in |
| 61 |
this document is a debugging situation. |
| 62 |
Readers specifically interested in debugging |
| 63 |
a grammar |
| 64 |
should read L<the document |
| 65 |
on tracing |
| 66 |
problems|Marpa::R2::Deprecated::NAIF::Tracing> before |
| 67 |
reading this document. |
| 68 |
|
| 69 |
=head1 Introduction to Earley parsing |
| 70 |
|
| 71 |
To read the |
| 72 |
L<C<show_progress>|Marpa::R2::Deprecated::NAIF::Recognizer/"show_progress()"> |
| 73 |
output, it is important to have a |
| 74 |
basic idea of what |
| 75 |
Earley items are, |
| 76 |
and of what the information in them means. |
| 77 |
Everything that the user needs to know |
| 78 |
is explained in this section. |
| 79 |
|
| 80 |
=head2 Dotted rules |
| 81 |
|
| 82 |
The idea behind Earley's algorithm is that you can |
| 83 |
parse by building a table of rules |
| 84 |
and where you are in those rules. |
| 85 |
"Where" means two things: location in the rule relative to the rule's |
| 86 |
symbols, |
| 87 |
and location relative to the parse's input stream. |
| 88 |
|
| 89 |
Let's look at an example of a rule in a context-free grammar. |
| 90 |
Here's the rule for assignment from the Perl distribution's C<perly.y> |
| 91 |
|
| 92 |
S<C<E<nbsp>E<nbsp>E<nbsp>E<nbsp>termbinop -E<gt> term ASSIGNOP term>> |
| 93 |
|
| 94 |
C<ASSIGNOP> is C<perly.y>'s internal name for |
| 95 |
the assignment operator. |
| 96 |
In plain Perl terms, this is the "C<=>" character. |
| 97 |
|
| 98 |
In parsing this rule, we can be at any of four possible locations. |
| 99 |
One location is at the beginning, before all of the symbols. |
| 100 |
The other three locations are immediately after each of the rule's |
| 101 |
three symbols. |
| 102 |
|
| 103 |
Within a rule, position relative to the symbols of the rule |
| 104 |
is traditionally indicated with a dot. In fact, the symbol-relative |
| 105 |
rule position is very often called the B<dot location>. Taken as |
| 106 |
a pair, a rule and a dot location are called a B<dotted rule>. |
| 107 |
|
| 108 |
Here's our rule with a dot location indicated: |
| 109 |
|
| 110 |
S<C<E<nbsp>E<nbsp>E<nbsp>E<nbsp>termbinop -E<gt> E<middot> term ASSIGNOP term>> |
| 111 |
|
| 112 |
The dot location in this dotted rule is at the beginning. |
| 113 |
A dot location at the beginning of a dotted rule means |
| 114 |
that we have not recognized any symbols in the rule yet. |
| 115 |
All we are doing is predicting that the rule will occur. |
| 116 |
A dotted rule with the dot before all of its symbols is called a B<prediction> |
| 117 |
or a B<predicted rule>. |
| 118 |
|
| 119 |
Here's another dotted rule: |
| 120 |
|
| 121 |
S<C<E<nbsp>E<nbsp>E<nbsp>E<nbsp>termbinop -E<gt> term E<middot> ASSIGNOP term>> |
| 122 |
|
| 123 |
In this dotted rule, |
| 124 |
we are saying we have seen a C<term>, but have not yet recognized |
| 125 |
an C<ASSIGNOP>. |
| 126 |
|
| 127 |
There's another special kind of dotted rule, a completion. |
| 128 |
A B<completion> (also called a B<completed rule>) |
| 129 |
is a dotted rule with the dot after all of the symbols. |
| 130 |
Here is the completion for the rule that we have been using as an example: |
| 131 |
|
| 132 |
S<C<E<nbsp>E<nbsp>E<nbsp>E<nbsp>termbinop -E<gt> term ASSIGNOP term E<middot>>> |
| 133 |
|
| 134 |
A completion indicates that a rule has been fully recognized. |
| 135 |
|
| 136 |
=head2 Earley items |
| 137 |
|
| 138 |
The dotted rules contain |
| 139 |
all but one piece of the information |
| 140 |
that Earley's algorithm |
| 141 |
needs to track. |
| 142 |
The missing piece is the second of the two "wheres": where in the input stream. |
| 143 |
To associate input stream location and dotted rules, Earley's algorithm uses what are now called Earley items. |
| 144 |
|
| 145 |
A convenient way to think of an B<Earley item> is as a triple, or 3-tuple, |
| 146 |
consisting of dotted rule, origin and current location. |
| 147 |
The B<origin> is the location in the input stream where the dotted rule starts. |
| 148 |
The B<current location> (also called the B<dot location>) |
| 149 |
is the location in the input stream which corresponds to the |
| 150 |
dot position. |
| 151 |
|
| 152 |
Two noteworthy consequences |
| 153 |
follow from the way in which origin and current location are defined. |
| 154 |
First, |
| 155 |
if a dotted rule is a prediction, |
| 156 |
then origin and current location will always be the same. |
| 157 |
Second, |
| 158 |
the input stream location where a rule ends is not tracked unless the |
| 159 |
dotted rule is a completion. |
| 160 |
In other cases, |
| 161 |
an Earley item does not tell us |
| 162 |
if a rule will |
| 163 |
ever be completed, |
| 164 |
much less at which location. |
| 165 |
|
| 166 |
=head1 The example |
| 167 |
|
| 168 |
For this example of debugging, |
| 169 |
I've taken a very common example |
| 170 |
of a grammar |
| 171 |
and deliberately introduced a problem. |
| 172 |
(All the code and the full trace outputs |
| 173 |
for this example are in the Appendix.) |
| 174 |
I've commented out the correct start rule: |
| 175 |
|
| 176 |
=for Marpa::R2::Display |
| 177 |
name: Debug Example Part 1 |
| 178 |
partial: 1 |
| 179 |
normalize-whitespace: 1 |
| 180 |
|
| 181 |
## { lhs => 'Expression', rhs => [qw/Term/] }, |
| 182 |
|
| 183 |
=for Marpa::R2::Display::End |
| 184 |
|
| 185 |
and replaced it with another start rule, |
| 186 |
one which will cause problems: |
| 187 |
|
| 188 |
=for Marpa::R2::Display |
| 189 |
name: Debug Example Part 1 |
| 190 |
partial: 1 |
| 191 |
normalize-whitespace: 1 |
| 192 |
|
| 193 |
{ lhs => 'Expression', rhs => [qw/Factor/] }, |
| 194 |
|
| 195 |
=for Marpa::R2::Display::End |
| 196 |
|
| 197 |
In what follows, we'll pretend we don't already know |
| 198 |
where the problem is, |
| 199 |
and use the Marpa diagnostics and tracing facilities |
| 200 |
to "discover" it. |
| 201 |
|
| 202 |
=head1 First warning |
| 203 |
|
| 204 |
Right off the bat, we get two warning messages: |
| 205 |
|
| 206 |
=for Marpa::R2::Display |
| 207 |
name: Debug Example Trace Output |
| 208 |
partial: 1 |
| 209 |
normalize-whitespace: 1 |
| 210 |
|
| 211 |
Inaccessible symbol: Add |
| 212 |
Inaccessible symbol: Term |
| 213 |
|
| 214 |
=for Marpa::R2::Display::End |
| 215 |
|
| 216 |
If we were alert, these would be enough to tell us there is |
| 217 |
a serious problem. |
| 218 |
"Inaccessible" symbols are symbols which cannot be reached |
| 219 |
from the start symbol. |
| 220 |
This means that the grammar will never produce them, |
| 221 |
and that parses will never find them in the input. |
| 222 |
|
| 223 |
Since C<Add> and C<Term> are both important symbols |
| 224 |
in our application, |
| 225 |
that should tell us our grammar has a serious problem. |
| 226 |
In fact, |
| 227 |
these warning messages would often be enough |
| 228 |
to point us to the error. |
| 229 |
But, in order to look at more of Marpa's |
| 230 |
tracing facilities, let's pretend we have not |
| 231 |
had our morning coffee, |
| 232 |
and that we miss the significance of these warning messages. |
| 233 |
|
| 234 |
=head1 Output from trace_terminals() |
| 235 |
|
| 236 |
Before looking at Marpa's progress reports, |
| 237 |
it is usually best to orient yourself by looking |
| 238 |
at the output from |
| 239 |
L<C<trace_terminals>|Marpa::R2::Deprecated::NAIF::Recognizer/trace_terminals>. |
| 240 |
Typically, you will be interested in the last tokens |
| 241 |
to be accepted, and perhaps also |
| 242 |
in the tokens the recognizer was |
| 243 |
looking for when it didn't find what it wanted. |
| 244 |
Sometimes that information alone is enough to make it clear |
| 245 |
where the problem is. |
| 246 |
|
| 247 |
The full |
| 248 |
L<C<trace_terminals>|Marpa::R2::Deprecated::NAIF::Recognizer/trace_terminals> |
| 249 |
output for this example is in the Appendix. |
| 250 |
We see that the recognizer seems to accept "C<42*1>" but it fails |
| 251 |
when it confronts the plus sign ("C<+>"). |
| 252 |
The last two lines are: |
| 253 |
|
| 254 |
=for Marpa::R2::Display |
| 255 |
name: Debug Example Trace Output |
| 256 |
partial: 1 |
| 257 |
normalize-whitespace: 1 |
| 258 |
|
| 259 |
Accepted "Number" at 2-3 |
| 260 |
Expecting "Multiply" at 3 |
| 261 |
|
| 262 |
=for Marpa::R2::Display::End |
| 263 |
|
| 264 |
A note in passing: Marpa shows the location of the tokens it accepts |
| 265 |
as a range of locations. For C<Number>, the range is "C<2-3>", indicating that |
| 266 |
the token starts at location 2 and ends at location 3. |
| 267 |
In its default input model, all tokens have length 1, |
| 268 |
so this is clearly information overkill. |
| 269 |
But Marpa allows other input models, and in those models the information |
| 270 |
about start and end location of the token is important. |
| 271 |
|
| 272 |
Returning to the problem at hand: |
| 273 |
We notice that at location 3 we are expecting a C<Multiply> operator, |
| 274 |
but not an C<Add> operator. |
| 275 |
That should strike us as strange, |
| 276 |
and send us back to the grammar. |
| 277 |
But for the sake of our example we will |
| 278 |
assume that we are slow on the uptake today, |
| 279 |
and that this does not clue us in. |
| 280 |
We move on. |
| 281 |
|
| 282 |
=head1 Output from show_progress() |
| 283 |
|
| 284 |
Marpa's most powerful tool for debugging |
| 285 |
grammars is its progress report, which |
| 286 |
shows the |
| 287 |
Earley items being worked on. |
| 288 |
In the Appendix, progress reports for the entire parse are shown. |
| 289 |
Our example in this document is a very small one, |
| 290 |
so that producing progress reports for the entire parse is |
| 291 |
a reasonable thing to do in this case. |
| 292 |
If a parse is at all large, you will usually need to be selective. |
| 293 |
|
| 294 |
The progress report that is usually of most interest is |
| 295 |
the one for the Earley set |
| 296 |
that you were working on when the error occurred. |
| 297 |
This is called the B<current location>. |
| 298 |
In our example the current location is location 3. |
| 299 |
By default, |
| 300 |
L<C<show_progress>|Marpa::R2::Deprecated::NAIF::Recognizer/"show_progress()"> |
| 301 |
prints out only the progress reports for the current location. |
| 302 |
|
| 303 |
Here are the progress reports for the current location, |
| 304 |
location 3, |
| 305 |
from our example. |
| 306 |
|
| 307 |
=for Marpa::R2::Display |
| 308 |
name: Debug Example Progress Report |
| 309 |
partial: 1 |
| 310 |
normalize-whitespace: 1 |
| 311 |
|
| 312 |
F0 @0-3 Expression -> Factor . |
| 313 |
F2 @2-3 Factor -> Number . |
| 314 |
R4:1 x2 @0,2-3 Factor -> Factor . Multiply Factor |
| 315 |
F4 @0-3 Factor -> Factor Multiply Factor . |
| 316 |
|
| 317 |
=for Marpa::R2::Display::End |
| 318 |
|
| 319 |
=head2 Progress report lines |
| 320 |
|
| 321 |
The last field of |
| 322 |
each Progress Report line shows, |
| 323 |
in fully expanded form, |
| 324 |
the dotted rule we were working on. |
| 325 |
Since that is the most important information, |
| 326 |
it may be tempting to skip the rest of this section, |
| 327 |
and move directly forward with the debugging. |
| 328 |
|
| 329 |
In fact, you might want to do exactly that -- |
| 330 |
skip to the beginning of the next section. |
| 331 |
What follows talks about the details of the format of the |
| 332 |
first few fields in each progress report line. |
| 333 |
These first few fields, while helpful, |
| 334 |
are also usually one or more of obvious in their meaning, |
| 335 |
not relevant to our example, |
| 336 |
and repetitive of information which can |
| 337 |
be deduced from other fields. |
| 338 |
|
| 339 |
=for Marpa::R2::Display |
| 340 |
name: Debug Example Progress Report |
| 341 |
partial: 1 |
| 342 |
normalize-whitespace: 1 |
| 343 |
|
| 344 |
F0 @0-3 Expression -> Factor . |
| 345 |
|
| 346 |
=for Marpa::R2::Display::End |
| 347 |
|
| 348 |
Prefixed to the dotted rule are two fields: "C<F0 @0-3>". |
| 349 |
The "C<F0>" says that this is a completed or B<final> rule, |
| 350 |
and that it is rule number 0. |
| 351 |
The rule number is used in |
| 352 |
other tracing and debugging output, |
| 353 |
when displaying the whole rule would take too much space. |
| 354 |
In what follows we won't need the rule number. |
| 355 |
|
| 356 |
The "C<@0-3>" describes the location of the dotted rule in |
| 357 |
the parse. In its simplest form, the location field is two |
| 358 |
location numbers, separated by a hyphen. |
| 359 |
The first location number is the origin, the place where Marpa |
| 360 |
first started recognizing the rule. |
| 361 |
The last location number is the dot location, the location location |
| 362 |
of the dot in a dotted rule. |
| 363 |
"C<@0-3>" say that this rule began at location 0, and that the |
| 364 |
dot is at location 3. |
| 365 |
|
| 366 |
The current location is location 3, and this is no coincidence. |
| 367 |
Whenever we are displaying the progress report for an location, |
| 368 |
all the progress report lines will have their dot location at |
| 369 |
that location. |
| 370 |
|
| 371 |
As an aside, notice that |
| 372 |
the left hand side symbol is C<Expression>. |
| 373 |
That is the start symbol. |
| 374 |
The presence of a completed start rule in our |
| 375 |
progress report indicates that if our input ended at location 3, it would be a valid sentence |
| 376 |
in the language of our grammar. |
| 377 |
|
| 378 |
Let's look at another progress report line: |
| 379 |
|
| 380 |
=for Marpa::R2::Display |
| 381 |
name: Debug Example Progress Report |
| 382 |
partial: 1 |
| 383 |
normalize-whitespace: 1 |
| 384 |
|
| 385 |
R4:1 x2 @0,2-3 Factor -> Factor . Multiply Factor |
| 386 |
|
| 387 |
=for Marpa::R2::Display::End |
| 388 |
|
| 389 |
Here the "C<R4:1>" indicates that this is rule number 4 |
| 390 |
(the "C<R>" stands for rule number) and that its dot |
| 391 |
position is after the first symbol on the right hand side. |
| 392 |
Symbol positions are numbered using the |
| 393 |
ordinal of the symbol just before the position. |
| 394 |
Symbols are numbered starting with 1, and |
| 395 |
symbol position 1 is the position immediately after |
| 396 |
symbol 1. |
| 397 |
|
| 398 |
The next field ("C<x2>") is new. |
| 399 |
It is a count. |
| 400 |
A progress report can contain multiple instances |
| 401 |
of the same dotted rule, and when there is more |
| 402 |
than one, a count field is included in |
| 403 |
the progress report line. |
| 404 |
Here the "C<x2>" indicates that there are two |
| 405 |
instances of C<Factor -E<gt> Factor . Multiply Factor> at |
| 406 |
this location. |
| 407 |
|
| 408 |
Multiple instances of a dotted rule |
| 409 |
will differ in their origin, |
| 410 |
and where they do, this is shown in |
| 411 |
the location field of the |
| 412 |
progress report line. |
| 413 |
Here the location field is "C<@0,2-3>", |
| 414 |
which indicates that one instance of this dotted rule has |
| 415 |
its origin at location 0, and the other has its origin |
| 416 |
at location 2. |
| 417 |
All instances reported on a single progress report line |
| 418 |
will always have the same dot location, and in this case |
| 419 |
it is location 3. |
| 420 |
|
| 421 |
Predicted rules also appear in progress reports: |
| 422 |
|
| 423 |
=for Marpa::R2::Display |
| 424 |
name: Debug Example Progress Report |
| 425 |
partial: 1 |
| 426 |
normalize-whitespace: 1 |
| 427 |
|
| 428 |
P2 @2-2 Factor -> . Number |
| 429 |
|
| 430 |
=for Marpa::R2::Display::End |
| 431 |
|
| 432 |
Here the "C<P>" in the summary field means "predicted". |
| 433 |
As with much of the information in the summary field, |
| 434 |
this only repeats what is obvious from the full expansion |
| 435 |
of the dotted rule later in the line. |
| 436 |
But final (or completed) and predicted rules can be important |
| 437 |
and the initial C<"F"> and C<"P"> make these lines easy to spot. |
| 438 |
|
| 439 |
Notice that in the predicted rule, |
| 440 |
the origin is the same as the dot location. |
| 441 |
This will always be the case with predicted rules. |
| 442 |
|
| 443 |
For any given location, |
| 444 |
no predicted rule has more than one instance. |
| 445 |
For other dotted rules, |
| 446 |
there may be many instances of the dotted |
| 447 |
rule at a single location. |
| 448 |
In grammars with right recursion, |
| 449 |
the number of instances |
| 450 |
is limited only by the |
| 451 |
length of the recursion. |
| 452 |
The length of a recursion is limited primarily by |
| 453 |
the available memory. |
| 454 |
|
| 455 |
When there are many |
| 456 |
instances of a dotted rule at a single location, |
| 457 |
it is inconvenient to show all the origins in a comma-separated |
| 458 |
list. |
| 459 |
In that case the origins in the location field are shown as a |
| 460 |
range, with the earliest separated from the most recent |
| 461 |
by a "C<...>". |
| 462 |
The example in this document contains no lines with |
| 463 |
a large number of |
| 464 |
instances, but here is an example from another grammar. |
| 465 |
This is the progress report line for |
| 466 |
the completed rule in a right recursion of length 20. |
| 467 |
|
| 468 |
=for Marpa::R2::Display |
| 469 |
name: Debug Leo Example Progress Report |
| 470 |
partial: 1 |
| 471 |
normalize-whitespace: 1 |
| 472 |
|
| 473 |
F1 x19 @0...18-20 Top_sequence -> Top Top_sequence . |
| 474 |
|
| 475 |
=for Marpa::R2::Display::End |
| 476 |
|
| 477 |
=head2 OK! Now to find the bug |
| 478 |
|
| 479 |
Here again are progress reports |
| 480 |
at the location where things went wrong: |
| 481 |
|
| 482 |
=for Marpa::R2::Display |
| 483 |
name: Debug Example Progress Report |
| 484 |
partial: 1 |
| 485 |
normalize-whitespace: 1 |
| 486 |
|
| 487 |
F0 @0-3 Expression -> Factor . |
| 488 |
F2 @2-3 Factor -> Number . |
| 489 |
R4:1 x2 @0,2-3 Factor -> Factor . Multiply Factor |
| 490 |
F4 @0-3 Factor -> Factor Multiply Factor . |
| 491 |
|
| 492 |
=for Marpa::R2::Display::End |
| 493 |
|
| 494 |
We see that we have completed rules for C<Expression>, and C<Factor>, |
| 495 |
as expected. |
| 496 |
We also see two Earley items that show |
| 497 |
that we are in the process of building another C<Factor>, |
| 498 |
and that it is expecting a C<Multiply> symbol. |
| 499 |
This is not the rule we want, but it explains why the C<trace_terminals> |
| 500 |
output showed that the recognizer was expecting a |
| 501 |
C<Multiply> symbol. |
| 502 |
|
| 503 |
What we want to know is, |
| 504 |
why is the recognizer B<not> expecting an C<Add> symbol? |
| 505 |
Looking back at the grammar, we see that only one rule uses |
| 506 |
the C<Add> symbol: the rule "C<Term -E<gt> Term Add Term>". |
| 507 |
The next step is to look at the Earley items for this rule. |
| 508 |
But there is a problem. |
| 509 |
We don't find any. |
| 510 |
|
| 511 |
Next, we ask ourselves, what is the earliest place the |
| 512 |
"C<Term -E<gt> Term Add Term>" rule should be appearing? |
| 513 |
The answer is that |
| 514 |
there should be a prediction of "C<Term -E<gt> Term Add Term>" at location 0. |
| 515 |
So we look at the predictions at location 0. |
| 516 |
|
| 517 |
=for Marpa::R2::Display |
| 518 |
name: Debug Example Progress Report |
| 519 |
partial: 1 |
| 520 |
normalize-whitespace: 1 |
| 521 |
|
| 522 |
P0 @0-0 Expression -> . Factor |
| 523 |
P2 @0-0 Factor -> . Number |
| 524 |
P4 @0-0 Factor -> . Factor Multiply Factor |
| 525 |
|
| 526 |
=for Marpa::R2::Display::End |
| 527 |
|
| 528 |
No "C<Term -E<gt> Term Add Term>" rule. |
| 529 |
We are never even predicting a |
| 530 |
"C<Term -E<gt> Term Add Term>" rule. |
| 531 |
We look back at the grammar, and start from |
| 532 |
the beginning. |
| 533 |
|
| 534 |
=for Marpa::R2::Display: |
| 535 |
name: Debug Example Part 1 |
| 536 |
partial: 1 |
| 537 |
normalize-whitespace: 1 |
| 538 |
|
| 539 |
{ lhs => 'Expression', rhs => [qw/Factor/] }, |
| 540 |
{ lhs => 'Term', rhs => [qw/Factor/] }, |
| 541 |
{ lhs => 'Factor', rhs => [qw/Number/] }, |
| 542 |
{ lhs => 'Term', |
| 543 |
rhs => [qw/Term Add Term/], |
| 544 |
action => 'do_add' |
| 545 |
}, |
| 546 |
{ lhs => 'Factor', |
| 547 |
rhs => [qw/Factor Multiply Factor/], |
| 548 |
action => 'do_multiply' |
| 549 |
}, |
| 550 |
|
| 551 |
=for Marpa::R2::Display::End |
| 552 |
|
| 553 |
The start symbol is C<Expression> and |
| 554 |
we do see a rule with |
| 555 |
C<Expression> |
| 556 |
on the left hand side. |
| 557 |
C<Expression> in turn produces a C<Factor> symbol, |
| 558 |
and there are two rules |
| 559 |
with C<Factor> on the left |
| 560 |
hand side. |
| 561 |
|
| 562 |
But none of these rules ever produce a C<Term>. |
| 563 |
In fact, however far we follow the productions, |
| 564 |
no rule ever produces a C<Term>. |
| 565 |
At this point we see the problem: |
| 566 |
If we start at the start symbol, |
| 567 |
and follow the rules of our grammar, we will never get to a C<Term> |
| 568 |
symbol. |
| 569 |
Which is exactly what that first warning message was saying. |
| 570 |
|
| 571 |
Now that we know what is wrong, |
| 572 |
we can reread our grammar, |
| 573 |
and see that our C<Expression -E<gt> Factor> rule is wrong. |
| 574 |
It should be C<Expression -E<gt> Term>. |
| 575 |
Change that and the problem is fixed. |
| 576 |
|
| 577 |
=head1 Empty rules |
| 578 |
|
| 579 |
When a symbol is nulled in your parse, |
| 580 |
L<C<show_progress>|Marpa::R2::Deprecated::NAIF::Recognizer/"show_progress()"> |
| 581 |
does not show its expansion into rules. |
| 582 |
This reduces clutter, |
| 583 |
and seems to be the intuitive way to treat nulled rules. |
| 584 |
|
| 585 |
=head1 Access to the "raw" progress report information |
| 586 |
|
| 587 |
This section deals with the C<progress()> recognizer method, |
| 588 |
which allows access to the raw progress report information. |
| 589 |
This method is not needed for typical debugging and tracing |
| 590 |
situations. |
| 591 |
It is intended for applications which want to leverage |
| 592 |
Marpa's "situational awareness" in innovative ways. |
| 593 |
|
| 594 |
=head2 progress() |
| 595 |
|
| 596 |
=for Marpa::R2::Display |
| 597 |
name: progress(0) example |
| 598 |
normalize-whitespace: 1 |
| 599 |
|
| 600 |
my $report0 = $recce->progress(0); |
| 601 |
|
| 602 |
=for Marpa::R2::Display::End |
| 603 |
|
| 604 |
Z<> |
| 605 |
|
| 606 |
=for Marpa::R2::Display |
| 607 |
name: progress() example |
| 608 |
normalize-whitespace: 1 |
| 609 |
|
| 610 |
my $latest_report = $recce->progress(); |
| 611 |
|
| 612 |
=for Marpa::R2::Display::End |
| 613 |
|
| 614 |
Given the parse location (Earley set ID) as its argument, |
| 615 |
the C<progress()> recognizer method returns a reference |
| 616 |
to an array of "report items". |
| 617 |
The parse location may be negative. |
| 618 |
An argument of I<-X> |
| 619 |
will be interpreted as location I<N+X+1>, where I<N> is |
| 620 |
the latest Earley set. |
| 621 |
This means that an argument of -1 indicates the latest |
| 622 |
Earley set, |
| 623 |
an argument of -2 indicates the Earley set just before |
| 624 |
the latest one, etc. |
| 625 |
|
| 626 |
Each report item is a triple: an array of three elements. |
| 627 |
The three elements are, in order, |
| 628 |
rule ID, dot position, and origin. |
| 629 |
The data returned by the two displays above, |
| 630 |
as well as the data for the other parse locations |
| 631 |
in our example, |
| 632 |
are shown L<below|"progress() outputs">. |
| 633 |
|
| 634 |
The rule ID is the same number that Marpa uses |
| 635 |
to identify rules |
| 636 |
in tracing and debugging output. |
| 637 |
Given a rule ID, an application can find |
| 638 |
its LHS and RHS symbols using |
| 639 |
L<the grammar's C<rule()> method|Marpa::R2::Deprecated::NAIF::Grammar/"rule()">. |
| 640 |
|
| 641 |
Dot position is -1 for completions, |
| 642 |
and 0 for predictions. |
| 643 |
Where the report item is not for a completion or |
| 644 |
a prediction, dot position is I<N>, |
| 645 |
where I<N> is the number of RHS symbols |
| 646 |
successfully recognized at the parse location of |
| 647 |
the progress report. |
| 648 |
|
| 649 |
Origin is parse location (Earley set ID) |
| 650 |
at which the rule application reported by |
| 651 |
the report item began. |
| 652 |
For a prediction, origin will always be |
| 653 |
the same as the location of the parse report. |
| 654 |
|
| 655 |
=head2 Progress reports and efficiency |
| 656 |
|
| 657 |
When progress reports are used for production parsing, |
| 658 |
instead of just for debugging and tracing, |
| 659 |
efficiency considerations become significant. |
| 660 |
We will start with the good news. |
| 661 |
C<progress()> is implemented in C, so that the |
| 662 |
application can usually expect calls to |
| 663 |
C<progress()> to be extremely fast. |
| 664 |
|
| 665 |
Now, as to the potential bad news: |
| 666 |
Applications planning frequent use of calls |
| 667 |
to C<progress()> need to be aware that, |
| 668 |
where there is a right recursion at a parse location, |
| 669 |
C<progress()> needs to follow the entire chain |
| 670 |
of recursions to create the progress report. |
| 671 |
That this is happening will always be evident |
| 672 |
from the parse report itself, |
| 673 |
which will contain one report item |
| 674 |
for every completion in the right-recursive |
| 675 |
chain of completions. |
| 676 |
If an application tries to |
| 677 |
produce progress reports for a large number of parse locations, |
| 678 |
and these parse locations have long right recursive chains, |
| 679 |
it can prove computationally expensive. |
| 680 |
|
| 681 |
The efficiency consideration just mentioned |
| 682 |
for following right recursions |
| 683 |
is not an issue for left recursions. |
| 684 |
Left recursions only produce at most two report |
| 685 |
items per parse location |
| 686 |
and are extremely fast to process. |
| 687 |
It is also not an issue for |
| 688 |
Marpa's sequence rules, because sequence rules |
| 689 |
are implemented internally as left recursions. |
| 690 |
|
| 691 |
=head1 Appendix: full code and output for the example |
| 692 |
|
| 693 |
Below are the code, the trace outputs |
| 694 |
and the progress report |
| 695 |
for the example used in this |
| 696 |
document. |
| 697 |
|
| 698 |
=head2 Code |
| 699 |
|
| 700 |
=for Marpa::R2::Display: |
| 701 |
name: Debug Example Part 1 |
| 702 |
perltidy: '-dcsc -sil=0' |
| 703 |
|
| 704 |
my $grammar = Marpa::R2::Grammar->new( |
| 705 |
{ start => 'Expression', |
| 706 |
actions => 'My_Actions', |
| 707 |
default_action => 'first_arg', |
| 708 |
rules => [ |
| 709 |
## This is a deliberate error in the grammar |
| 710 |
## The next line should be: |
| 711 |
## { lhs => 'Expression', rhs => [qw/Term/] }, |
| 712 |
## I have changed the Term to 'Factor' which |
| 713 |
## will cause problems. |
| 714 |
{ lhs => 'Expression', rhs => [qw/Factor/] }, |
| 715 |
{ lhs => 'Term', rhs => [qw/Factor/] }, |
| 716 |
{ lhs => 'Factor', rhs => [qw/Number/] }, |
| 717 |
{ lhs => 'Term', |
| 718 |
rhs => [qw/Term Add Term/], |
| 719 |
action => 'do_add' |
| 720 |
}, |
| 721 |
{ lhs => 'Factor', |
| 722 |
rhs => [qw/Factor Multiply Factor/], |
| 723 |
action => 'do_multiply' |
| 724 |
}, |
| 725 |
], |
| 726 |
} |
| 727 |
); |
| 728 |
|
| 729 |
=for Marpa::R2::Display::End |
| 730 |
|
| 731 |
=for Marpa::R2::Display: |
| 732 |
name: Debug Example Part 2 |
| 733 |
perltidy: '-dcsc -sil=0' |
| 734 |
|
| 735 |
$grammar->precompute(); |
| 736 |
|
| 737 |
my @tokens = ( |
| 738 |
[ 'Number', 42 ], |
| 739 |
[ 'Multiply', q{*} ], |
| 740 |
[ 'Number', 1 ], |
| 741 |
[ 'Add', q{+} ], |
| 742 |
[ 'Number', 7 ], |
| 743 |
); |
| 744 |
|
| 745 |
sub My_Actions::do_add { |
| 746 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 747 |
return $t1 + $t2; |
| 748 |
} |
| 749 |
|
| 750 |
sub My_Actions::do_multiply { |
| 751 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 752 |
return $t1 * $t2; |
| 753 |
} |
| 754 |
|
| 755 |
sub My_Actions::first_arg { shift; return shift; } |
| 756 |
|
| 757 |
my $recce = Marpa::R2::Recognizer->new( |
| 758 |
{ grammar => $grammar, trace_terminals => 2 } ); |
| 759 |
|
| 760 |
my $token_ix = 0; |
| 761 |
|
| 762 |
TOKEN: for my $token_and_value (@tokens) { |
| 763 |
last TOKEN if not defined $recce->read( @{$token_and_value} ); |
| 764 |
} |
| 765 |
|
| 766 |
$progress_report = $recce->show_progress( 0, -1 ); |
| 767 |
|
| 768 |
=for Marpa::R2::Display::End |
| 769 |
|
| 770 |
=head2 Trace output |
| 771 |
|
| 772 |
=for Marpa::R2::Display |
| 773 |
name: Debug Example Trace Output |
| 774 |
remove-display-indent: 1 |
| 775 |
remove-blank-last-line: 1 |
| 776 |
|
| 777 |
Inaccessible symbol: Add |
| 778 |
Inaccessible symbol: Term |
| 779 |
Setting trace_terminals option |
| 780 |
Expecting "Number" at earleme 0 |
| 781 |
Accepted "Number" at 0-1 |
| 782 |
Expecting "Multiply" at 1 |
| 783 |
Accepted "Multiply" at 1-2 |
| 784 |
Expecting "Number" at 2 |
| 785 |
Accepted "Number" at 2-3 |
| 786 |
Expecting "Multiply" at 3 |
| 787 |
Rejected "Add" at 3-4 |
| 788 |
|
| 789 |
=for Marpa::R2::Display::End |
| 790 |
|
| 791 |
Note the use of the term "earleme". |
| 792 |
If you are using the default input model, |
| 793 |
you can assume that earleme means "location": |
| 794 |
the earleme |
| 795 |
and the location will always be exactly the same. |
| 796 |
Advanced users, using alternative input models, |
| 797 |
may set it up so that earleme and location are |
| 798 |
two different things, |
| 799 |
and in that case the distinction will matter. |
| 800 |
|
| 801 |
=head2 show_progress() output |
| 802 |
|
| 803 |
=for Marpa::R2::Display |
| 804 |
name: Debug Example Progress Report |
| 805 |
remove-display-indent: 1 |
| 806 |
remove-blank-last-line: 1 |
| 807 |
|
| 808 |
P0 @0-0 Expression -> . Factor |
| 809 |
P2 @0-0 Factor -> . Number |
| 810 |
P4 @0-0 Factor -> . Factor Multiply Factor |
| 811 |
F0 @0-1 Expression -> Factor . |
| 812 |
F2 @0-1 Factor -> Number . |
| 813 |
R4:1 @0-1 Factor -> Factor . Multiply Factor |
| 814 |
P2 @2-2 Factor -> . Number |
| 815 |
P4 @2-2 Factor -> . Factor Multiply Factor |
| 816 |
R4:2 @0-2 Factor -> Factor Multiply . Factor |
| 817 |
F0 @0-3 Expression -> Factor . |
| 818 |
F2 @2-3 Factor -> Number . |
| 819 |
R4:1 x2 @0,2-3 Factor -> Factor . Multiply Factor |
| 820 |
F4 @0-3 Factor -> Factor Multiply Factor . |
| 821 |
|
| 822 |
=for Marpa::R2::Display::End |
| 823 |
|
| 824 |
=head2 progress() outputs |
| 825 |
|
| 826 |
These section contains the output of the C<progress()> |
| 827 |
method -- the progress reports in their "raw" format. |
| 828 |
The output is shown in L<Data::Dumper> format, |
| 829 |
with C<Data::Dumper::Indent> set to 0 |
| 830 |
and C<Data::Dumper::Terse> set to 1. |
| 831 |
|
| 832 |
The C<Data::Dumper> output from C<progress()> at location 0: |
| 833 |
|
| 834 |
=for Marpa::R2::Display |
| 835 |
name: progress() output at location 0 |
| 836 |
remove-display-indent: 1 |
| 837 |
remove-blank-last-line: 1 |
| 838 |
|
| 839 |
[[0,0,0],[2,0,0],[4,0,0]] |
| 840 |
|
| 841 |
=for Marpa::R2::Display::End |
| 842 |
|
| 843 |
The C<Data::Dumper> output from C<progress()> at location 1: |
| 844 |
|
| 845 |
=for Marpa::R2::Display |
| 846 |
name: progress() output at location 1 |
| 847 |
remove-display-indent: 1 |
| 848 |
remove-blank-last-line: 1 |
| 849 |
|
| 850 |
[[0,-1,0],[2,-1,0],[4,1,0]] |
| 851 |
|
| 852 |
=for Marpa::R2::Display::End |
| 853 |
|
| 854 |
The C<Data::Dumper> output from C<progress()> at location 2: |
| 855 |
|
| 856 |
=for Marpa::R2::Display |
| 857 |
name: progress() output at location 2 |
| 858 |
remove-display-indent: 1 |
| 859 |
remove-blank-last-line: 1 |
| 860 |
|
| 861 |
[[2,0,2],[4,0,2],[4,2,0]] |
| 862 |
|
| 863 |
=for Marpa::R2::Display::End |
| 864 |
|
| 865 |
The default C<progress()> output is for the latest Earley set, |
| 866 |
which is location 3 in our example. |
| 867 |
Here is the C<progress()> output for location 3. |
| 868 |
|
| 869 |
=for Marpa::R2::Display |
| 870 |
name: progress() output at location 3 |
| 871 |
remove-display-indent: 1 |
| 872 |
remove-blank-last-line: 1 |
| 873 |
|
| 874 |
[[0,-1,0],[2,-1,2],[4,-1,0],[4,1,0],[4,1,2]] |
| 875 |
|
| 876 |
=for Marpa::R2::Display::End |
| 877 |
|
| 878 |
=head1 Copyright and License |
| 879 |
|
| 880 |
=for Marpa::R2::Display |
| 881 |
ignore: 1 |
| 882 |
|
| 883 |
Copyright 2022 Jeffrey Kegler |
| 884 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 885 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 886 |
General Public License as published by the Free Software Foundation, |
| 887 |
either version 3 of the License, or (at your option) any later version. |
| 888 |
|
| 889 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 890 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 891 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 892 |
Lesser General Public License for more details. |
| 893 |
|
| 894 |
You should have received a copy of the GNU Lesser |
| 895 |
General Public License along with Marpa::R2. If not, see |
| 896 |
http://www.gnu.org/licenses/. |
| 897 |
|
| 898 |
=for Marpa::R2::Display::End |
| 899 |
|
| 900 |
=cut |
| 901 |
|
| 902 |
# Local Variables: |
| 903 |
# mode: cperl |
| 904 |
# cperl-indent-level: 4 |
| 905 |
# fill-column: 100 |
| 906 |
# End: |
| 907 |
# vim: expandtab shiftwidth=4: |