| 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::Progress - Progress reports on your parse |
| 19 |
|
| 20 |
=head1 About this document |
| 21 |
|
| 22 |
This document describes |
| 23 |
the progress reports for Marpa's SLIF interface. |
| 24 |
These allow an application to know |
| 25 |
exactly where it is in the parse at any |
| 26 |
point. |
| 27 |
For parse locations of the user's choosing, |
| 28 |
progress reports list all the rules in play, |
| 29 |
and indicate the location at which the rule started, |
| 30 |
and how far into the rule parsing has progressed. |
| 31 |
|
| 32 |
Progress reports are extremely useful in |
| 33 |
debugging grammars and |
| 34 |
the detailed example in |
| 35 |
this document is a debugging instance. |
| 36 |
Readers specifically interested in debugging |
| 37 |
a grammar |
| 38 |
should read L<the document |
| 39 |
on tracing |
| 40 |
problems|Marpa::R2::Tracing> before |
| 41 |
reading this document. |
| 42 |
|
| 43 |
To read the |
| 44 |
L<C<show_progress>|Marpa::R2::Scanless::R/"show_progress()"> |
| 45 |
output, it is important to have a |
| 46 |
basic idea of what |
| 47 |
Earley items are, |
| 48 |
and of what the information in them means. |
| 49 |
Everything that the user needs to know |
| 50 |
is explained in |
| 51 |
L<this |
| 52 |
document|Marpa::R2::Algorithm>. |
| 53 |
|
| 54 |
We recall that |
| 55 |
G1 location is location in terms of the G1 subgrammar's Earley sets. |
| 56 |
When the term "location" is used in this document, it means G1 location unless |
| 57 |
otherwise indicated. |
| 58 |
|
| 59 |
A user often finds it much more convenient to think in terms of line and column position in |
| 60 |
the input stream, instead of G1 location. |
| 61 |
Every G1 location corresponds to a range of positions in the input stream. |
| 62 |
When the term "position" is used in this document, it means input stream position, |
| 63 |
unless otherwise indicated. |
| 64 |
|
| 65 |
=head1 The problem |
| 66 |
|
| 67 |
For this example of debugging, |
| 68 |
I have taken a very simple prototype of a string expression calculator |
| 69 |
and deliberately introduced a problem. |
| 70 |
I've commented out one of the correct rules: |
| 71 |
|
| 72 |
=for Marpa::R2::Display |
| 73 |
name: SLIF debug example, part 1 |
| 74 |
partial: 1 |
| 75 |
normalize-whitespace: 1 |
| 76 |
|
| 77 |
# <numeric assignment> ::= variable '=' <numeric expression> |
| 78 |
|
| 79 |
=for Marpa::R2::Display::End |
| 80 |
|
| 81 |
and replaced it with a altered one: |
| 82 |
|
| 83 |
=for Marpa::R2::Display |
| 84 |
name: SLIF debug example, part 1 |
| 85 |
partial: 1 |
| 86 |
normalize-whitespace: 1 |
| 87 |
|
| 88 |
<numeric assignment> ::= variable '=' expression |
| 89 |
|
| 90 |
=for Marpa::R2::Display::End |
| 91 |
|
| 92 |
For those readers who like to look ahead (and I encourage you to |
| 93 |
be one of those readers) |
| 94 |
all of the code and outputs for this example are collected in |
| 95 |
the L</"Appendix">. |
| 96 |
|
| 97 |
This altered rule contains an mistake of the kind that is |
| 98 |
easy to make in actual practice. |
| 99 |
(In this case, |
| 100 |
a unlucky choice of naming conventions may have contributed.) |
| 101 |
The altered version will cause problems. |
| 102 |
In what follows, we'll pretend we don't already know |
| 103 |
where the problem is, |
| 104 |
and that in desk-checking the grammar our eye does not |
| 105 |
spot the mistake, so |
| 106 |
that we need |
| 107 |
to use the Marpa diagnostics and tracing facilities |
| 108 |
to "discover" it. |
| 109 |
|
| 110 |
=head1 The example |
| 111 |
|
| 112 |
The example we will use is a prototype string calculator. |
| 113 |
It's extremely simple, to make the example easy to follow. |
| 114 |
But it can be seen as a realistic example, |
| 115 |
if it is thought of as a very |
| 116 |
early stage in the incremental development of something useful. |
| 117 |
|
| 118 |
=for Marpa::R2::Display |
| 119 |
name: SLIF debug example, part 1 |
| 120 |
partial: 1 |
| 121 |
normalize-whitespace: 1 |
| 122 |
|
| 123 |
:default ::= action => ::array bless => ::lhs |
| 124 |
:start ::= statements |
| 125 |
statements ::= statement * |
| 126 |
statement ::= assignment | <numeric assignment> |
| 127 |
assignment ::= 'set' variable 'to' expression |
| 128 |
|
| 129 |
# This is a deliberate error in the grammar |
| 130 |
# The next line should be: |
| 131 |
# <numeric assignment> ::= variable '=' <numeric expression> |
| 132 |
# I have changed the <numeric expression> to <expression> which |
| 133 |
# will cause problems. |
| 134 |
<numeric assignment> ::= variable '=' expression |
| 135 |
|
| 136 |
expression ::= |
| 137 |
variable | string |
| 138 |
|| 'string' '(' <numeric expression> ')' |
| 139 |
|| expression '+' expression |
| 140 |
<numeric expression> ::= |
| 141 |
variable | number |
| 142 |
|| <numeric expression> '*' <numeric expression> |
| 143 |
|| <numeric expression> '+' <numeric expression> |
| 144 |
variable ~ [\w]+ |
| 145 |
number ~ [\d]+ |
| 146 |
string ~ ['] <string contents> ['] |
| 147 |
<string contents> ~ [^'\x{0A}\x{0B}\x{0C}\x{0D}\x{0085}\x{2028}\x{2029}]+ |
| 148 |
:discard ~ whitespace |
| 149 |
whitespace ~ [\s]+ |
| 150 |
|
| 151 |
=for Marpa::R2::Display::End |
| 152 |
|
| 153 |
At this stage of developing our string calculator, |
| 154 |
we have assignment, variables, constants, concatenation and conversion of |
| 155 |
numerics. |
| 156 |
For numerics, we have assignment, variables, constants, multiplication and addition. |
| 157 |
|
| 158 |
We decide that, since string expressions and variables are the "default", that in the |
| 159 |
grammar we'll make the symbol names for numeric assignment and expressions explicit: |
| 160 |
C<E<lt>numeric expressionE<gt>> and C<E<lt>numeric assignmentE<gt>>. |
| 161 |
But since strings are the default, we decide to call our string expressions simply C<E<lt>expressionE<gt>>, |
| 162 |
and to call our string assignments simply C<E<lt>assignmentE<gt>>. |
| 163 |
This seems like a good idea, but it is also likely to cause confusion. |
| 164 |
For the sake of our example we will pretend that it did. |
| 165 |
|
| 166 |
=head1 The error message |
| 167 |
|
| 168 |
If we try the following input, |
| 169 |
|
| 170 |
=for Marpa::R2::Display: |
| 171 |
name: SLIF debug example, part 2 |
| 172 |
partial: 1 |
| 173 |
normalize-whitespace: 1 |
| 174 |
|
| 175 |
my $test_input = 'a = 8675309 + 42 * 711'; |
| 176 |
|
| 177 |
=for Marpa::R2::Display::End |
| 178 |
|
| 179 |
we will get this error message, |
| 180 |
|
| 181 |
=for Marpa::R2::Display |
| 182 |
name: SLIF debug example error message |
| 183 |
partial: 1 |
| 184 |
normalize-whitespace: 1 |
| 185 |
|
| 186 |
Error in SLIF parse: No lexemes accepted at line 1, column 18 |
| 187 |
Rejected lexeme #0: '*'; value="*"; length = 1 |
| 188 |
* String before error: a = 8675309 + 42\s |
| 189 |
* The error was at line 1, column 18, and at character 0x002a '*', ... |
| 190 |
* here: * 711 |
| 191 |
|
| 192 |
=for Marpa::R2::Display::End |
| 193 |
|
| 194 |
The error message indicates that Marpa rejected the "C<*>" operator. |
| 195 |
|
| 196 |
=head1 The value of the parse |
| 197 |
|
| 198 |
In debugging this issue, we'll look at the value of the parse first. |
| 199 |
The parse value differs from the other debugging aids we'll discuss. |
| 200 |
Every other debugging tool we will describe is always available, |
| 201 |
no matter how badly the parse failed. |
| 202 |
But if you have a problem parsing, you often won't get a parse value. |
| 203 |
|
| 204 |
Our luck holds. |
| 205 |
Here's a dump of the parse value at the point of failure. |
| 206 |
It's a nice to way to see |
| 207 |
what Marpa thinks the parse was so far. |
| 208 |
|
| 209 |
=for Marpa::R2::Display |
| 210 |
name: SLIF debug example dump of value |
| 211 |
partial: 1 |
| 212 |
normalize-whitespace: 1 |
| 213 |
|
| 214 |
\bless( |
| 215 |
[ |
| 216 |
bless( |
| 217 |
[ |
| 218 |
bless( |
| 219 |
[ |
| 220 |
'a', '=', |
| 221 |
bless( |
| 222 |
[ |
| 223 |
bless( ['8675309'], 'My_Nodes::expression' ), |
| 224 |
'+', |
| 225 |
bless( ['42'], 'My_Nodes::expression' ) |
| 226 |
], |
| 227 |
'My_Nodes::expression' |
| 228 |
) |
| 229 |
], |
| 230 |
'My_Nodes::numeric_assignment' |
| 231 |
) |
| 232 |
], |
| 233 |
'My_Nodes::statement' |
| 234 |
) |
| 235 |
], |
| 236 |
'My_Nodes::statements' |
| 237 |
); |
| 238 |
|
| 239 |
=for Marpa::R2::Display::End |
| 240 |
|
| 241 |
If we were perceptive, we might spot the error here. |
| 242 |
Our parse is not quite right, and that |
| 243 |
shows up in the outer C<My_Nodes::expression> -- |
| 244 |
it should be |
| 245 |
C<My_Nodes::numeric_expression>. |
| 246 |
We'll assume that we don't notice this. |
| 247 |
|
| 248 |
In fact, in the following, we'll pretend we haven't seen the |
| 249 |
dump of the parse value. |
| 250 |
We can't always get a parse value, so we don't want to rely on it. |
| 251 |
|
| 252 |
=head1 Output from trace_terminals() |
| 253 |
|
| 254 |
You can rely on getting the output from |
| 255 |
L<C<trace_terminals>|Marpa::R2::Scanless::R/trace_terminals>, |
| 256 |
and it is a good next place to check. |
| 257 |
Typically, you will be interested in the last tokens |
| 258 |
to be accepted. |
| 259 |
Sometimes that information alone is enough to make it clear |
| 260 |
where the problem is. |
| 261 |
|
| 262 |
The full |
| 263 |
L<C<trace_terminals>|Marpa::R2::Scanless::R/trace_terminals> |
| 264 |
output for this example is in the Appendix. |
| 265 |
We see that the recognizer accepts the input as far |
| 266 |
as the multiplication sign ("C<*>"), |
| 267 |
which it rejects. |
| 268 |
In Marpa, a lexeme is "acceptable" if it fits the grammar |
| 269 |
and the input so far. |
| 270 |
A lexeme is rejected if it is not acceptable. |
| 271 |
|
| 272 |
The last two lines of the C<trace_terminals> output are: |
| 273 |
|
| 274 |
=for Marpa::R2::Display |
| 275 |
name: SLIF debug example trace output |
| 276 |
partial: 1 |
| 277 |
normalize-whitespace: 1 |
| 278 |
|
| 279 |
Discarded lexeme L1c17: whitespace |
| 280 |
Rejected lexeme L1c18: '*'; value="*" |
| 281 |
|
| 282 |
=for Marpa::R2::Display::End |
| 283 |
|
| 284 |
A note in passing: Marpa shows the input string position of the tokens it accepts, |
| 285 |
discard and rejects. |
| 286 |
C<E<lt>whitespaceE<gt>> is supposed to be discarded |
| 287 |
and that was what happened at line 1, column 17. |
| 288 |
But the C<'*'> that was next in the input was rejected, |
| 289 |
and that was not supposed to happen. |
| 290 |
|
| 291 |
=head1 Output from show_progress() |
| 292 |
|
| 293 |
Marpa's most powerful tool for debugging |
| 294 |
grammars is its progress report, which |
| 295 |
shows the |
| 296 |
Earley items being worked on. |
| 297 |
In the Appendix, progress reports for the entire parse are shown. |
| 298 |
Our example in this document is a very small one, |
| 299 |
so that producing progress reports for the entire parse is |
| 300 |
a reasonable thing to do in this case. |
| 301 |
If a parse is at all large, you will usually need to be selective. |
| 302 |
|
| 303 |
The progress report that is usually of most interest is |
| 304 |
the one for the Earley set |
| 305 |
that you were working on when the error occurred. |
| 306 |
This is called the B<current location>. |
| 307 |
In our example the current location is G1 location 5. |
| 308 |
By default, |
| 309 |
L<C<show_progress>|Marpa::R2::Scanless::R/"show_progress()"> |
| 310 |
prints out only the progress reports for the current location. |
| 311 |
|
| 312 |
Here are the progress reports for the current location, |
| 313 |
location 5, |
| 314 |
from our example. |
| 315 |
|
| 316 |
=for Marpa::R2::Display |
| 317 |
name: SLIF debug example progress report |
| 318 |
partial: 1 |
| 319 |
normalize-whitespace: 1 |
| 320 |
|
| 321 |
F0 @0-5 L1c1-16 statements -> statement * . |
| 322 |
P1 @5-5 L1c15-16 statement -> . assignment |
| 323 |
P2 @5-5 L1c15-16 statement -> . <numeric assignment> |
| 324 |
F2 @0-5 L1c1-16 statement -> <numeric assignment> . |
| 325 |
P3 @5-5 L1c15-16 assignment -> . 'set' variable 'to' expression |
| 326 |
P4 @5-5 L1c15-16 <numeric assignment> -> . variable '=' expression |
| 327 |
F4 @0-5 L1c1-16 <numeric assignment> -> variable '=' expression . |
| 328 |
F5 @2-5 L1c3-16 expression -> expression . |
| 329 |
F7 @4-5 L1c13-16 expression -> expression . |
| 330 |
F8 @4-5 L1c13-16 expression -> variable . |
| 331 |
R11:1 @2-5 L1c3-16 expression -> expression . '+' expression |
| 332 |
F11 @2-5 L1c3-16 expression -> expression '+' expression . |
| 333 |
F19 @0-5 L1c1-16 :start -> statements . |
| 334 |
|
| 335 |
=for Marpa::R2::Display::End |
| 336 |
|
| 337 |
=head2 Progress report lines |
| 338 |
|
| 339 |
=for Marpa::R2::Display |
| 340 |
name: SLIF debug example progress report |
| 341 |
partial: 1 |
| 342 |
normalize-whitespace: 1 |
| 343 |
|
| 344 |
F19 @0-5 L1c1-16 :start -> statements . |
| 345 |
|
| 346 |
=for Marpa::R2::Display::End |
| 347 |
|
| 348 |
The last field of |
| 349 |
each progress report line shows, |
| 350 |
in fully expanded form, |
| 351 |
the dotted rule we were working on. |
| 352 |
Prefixed to the dotted rule are three fields. |
| 353 |
In the example just above they are "C<F0 @0-5 L1c1-16>". |
| 354 |
The "C<F0>" says that this is a completed or B<final> rule, |
| 355 |
and that it is rule number 0. |
| 356 |
The rule number is a convenient way to refer to a rule |
| 357 |
and is used when displaying the whole rule would take too much space. |
| 358 |
|
| 359 |
The "C<@0-5>" describes the G1 locations of the dotted rule in |
| 360 |
the parse. In its simplest form, the location field is two |
| 361 |
G1 location numbers, separated by a hyphen. |
| 362 |
The first G1 location number is the origin, the place where Marpa |
| 363 |
first started recognizing the rule. |
| 364 |
The last G1 location number is the dot location, the G1 location |
| 365 |
of the dot in a dotted rule. |
| 366 |
"C<@0-3>" says that this rule began at G1 location 0, and that the |
| 367 |
dot is at G1 location 3. |
| 368 |
|
| 369 |
Following the G1 location is the range of positions in the input string: |
| 370 |
"C<L1c1-16>". |
| 371 |
This indicates that the origin of dotted rule is at line 1, column 1, |
| 372 |
and that its dot position is after line 1, column 16. |
| 373 |
|
| 374 |
The current location is also just after line 1, column 16, and at G1 location 5, |
| 375 |
and this is no coincidence. |
| 376 |
Whenever we are displaying the progress report for a G1 location, |
| 377 |
all the progress report lines will have their dot location at |
| 378 |
that G1 location. |
| 379 |
|
| 380 |
As an aside, notice that |
| 381 |
the left hand side symbol is C<:start>. |
| 382 |
That is the start pseudo-symbol. |
| 383 |
The presence of a completed start rule in our |
| 384 |
progress report indicates that if our input had ended at location 5, |
| 385 |
it would be a valid sentence |
| 386 |
in the language of our grammar. |
| 387 |
(And it is because the input at G1 location 5 was a valid sentence of the grammar, |
| 388 |
that we were able to look at the value |
| 389 |
of the parse at location 5 for debugging purposes.) |
| 390 |
|
| 391 |
Let's look at another progress report line: |
| 392 |
|
| 393 |
=for Marpa::R2::Display |
| 394 |
name: SLIF debug example progress report |
| 395 |
partial: 1 |
| 396 |
normalize-whitespace: 1 |
| 397 |
|
| 398 |
R11:2 @2-4 L1c3-13 expression -> expression '+' . expression |
| 399 |
|
| 400 |
=for Marpa::R2::Display::End |
| 401 |
|
| 402 |
Here the "C<R11:2>" indicates that this is rule number 11 |
| 403 |
(the "C<R>" stands for rule number) and that its dot |
| 404 |
position is after the second symbol on the right hand side. |
| 405 |
Symbol positions are numbered using the |
| 406 |
ordinal of the symbol just before the position. |
| 407 |
Symbols are numbered starting with 1, and |
| 408 |
symbol position 2 is the position immediately after |
| 409 |
symbol 2. |
| 410 |
|
| 411 |
Predicted rules also appear in progress reports: |
| 412 |
|
| 413 |
=for Marpa::R2::Display |
| 414 |
name: SLIF debug example progress report |
| 415 |
partial: 1 |
| 416 |
normalize-whitespace: 1 |
| 417 |
|
| 418 |
P2 @3-3 L1c5-11 statement -> . <numeric assignment> |
| 419 |
|
| 420 |
=for Marpa::R2::Display::End |
| 421 |
|
| 422 |
Here the "C<P>" in the summary field means "predicted". |
| 423 |
Notice that in the predicted rule, |
| 424 |
the origin is the same as the dot location. |
| 425 |
This will always be the case with predicted rules. |
| 426 |
|
| 427 |
=head2 OK! Now to find the bug |
| 428 |
|
| 429 |
If we look again are progress reports |
| 430 |
at the location 5, the location where things went wrong: |
| 431 |
We see that we have completed rules for |
| 432 |
C<E<lt>expressionE<gt>>, |
| 433 |
C<E<lt>numeric assignmentE<gt>>, |
| 434 |
C<E<lt>statementE<gt>>, |
| 435 |
C<E<lt>statementsE<gt>>, |
| 436 |
as expected. |
| 437 |
We also see two Earley items that show |
| 438 |
that we are in the process of building another C<E<lt>expressionE<gt>>, |
| 439 |
and that it is expecting a 'C<+>' symbol. |
| 440 |
|
| 441 |
What we want to know is, |
| 442 |
why is the recognizer B<not> expecting an 'C<*>' symbol? |
| 443 |
Looking back at the grammar, we see that only one rule uses |
| 444 |
the 'C<*>' symbol. |
| 445 |
Here it is as part of a prioritized rule in the DSL: |
| 446 |
|
| 447 |
=for Marpa::R2::Display |
| 448 |
name: SLIF debug example, part 1 |
| 449 |
partial: 1 |
| 450 |
normalize-whitespace: 1 |
| 451 |
|
| 452 |
<numeric expression> ::= |
| 453 |
variable | number |
| 454 |
|| <numeric expression> '*' <numeric expression> |
| 455 |
|| <numeric expression> '+' <numeric expression> |
| 456 |
|
| 457 |
=for Marpa::R2::Display::End |
| 458 |
|
| 459 |
Here it is from the C<show_rules()> listing: |
| 460 |
|
| 461 |
=for Marpa::R2::Display |
| 462 |
name: SLIF debug example show_rules() output |
| 463 |
partial: 1 |
| 464 |
normalize-whitespace: 1 |
| 465 |
|
| 466 |
G1 R17 <numeric expression> ::= <numeric expression> '*' <numeric expression> |
| 467 |
|
| 468 |
=for Marpa::R2::Display::End |
| 469 |
|
| 470 |
It's rule 17 in subgrammar G1, and for convenience we will call it R17. |
| 471 |
The next step is to look at the Earley items for this rule. |
| 472 |
But there is a problem. |
| 473 |
We don't find any. |
| 474 |
|
| 475 |
Next, we ask ourselves, what is the earliest place R17 |
| 476 |
should be appearing? |
| 477 |
The answer is that |
| 478 |
there should be a prediction of R17 at location 0. |
| 479 |
So we look at the predictions at location 0. |
| 480 |
|
| 481 |
=for Marpa::R2::Display |
| 482 |
name: SLIF debug example progress report |
| 483 |
partial: 1 |
| 484 |
normalize-whitespace: 1 |
| 485 |
|
| 486 |
P0 @0-0 L0c0 statements -> . statement * |
| 487 |
P1 @0-0 L0c0 statement -> . assignment |
| 488 |
P2 @0-0 L0c0 statement -> . <numeric assignment> |
| 489 |
P3 @0-0 L0c0 assignment -> . 'set' variable 'to' expression |
| 490 |
P4 @0-0 L0c0 <numeric assignment> -> . variable '=' expression |
| 491 |
P19 @0-0 L0c0 :start -> . statements |
| 492 |
|
| 493 |
=for Marpa::R2::Display::End |
| 494 |
|
| 495 |
No R17 predicted at G1 location 0. |
| 496 |
Next we look through the the entire progress report, at all G1 locations, |
| 497 |
to see if R17 is predicted anywhere. |
| 498 |
No R17. Not anywhere. |
| 499 |
|
| 500 |
The LHS of R17 is C<E<lt>numeric expressionE<gt>>. |
| 501 |
We look in the progress report for dotted rules where |
| 502 |
C<E<lt>numeric expressionE<gt>> is expected -- that is, |
| 503 |
dotted rules where C<E<lt>numeric expressionE<gt>> is the post-dot symbol. |
| 504 |
There are none. |
| 505 |
|
| 506 |
Next we look for places in the progress reports where |
| 507 |
C<E<lt>numeric expressionE<gt>> occurs at all, |
| 508 |
whether post-dot or not. |
| 509 |
In the progress reports, |
| 510 |
C<E<lt>numeric expressionE<gt>> occurs in only two dotted rule instances. |
| 511 |
Here they are: |
| 512 |
|
| 513 |
=for Marpa::R2::Display |
| 514 |
name: SLIF debug example progress report |
| 515 |
partial: 1 |
| 516 |
normalize-whitespace: 1 |
| 517 |
|
| 518 |
P10 @2-2 L1c3 expression -> . 'string' '(' <numeric expression> ')' |
| 519 |
|
| 520 |
=for Marpa::R2::Display::End |
| 521 |
|
| 522 |
=for Marpa::R2::Display |
| 523 |
name: SLIF debug example progress report |
| 524 |
partial: 1 |
| 525 |
normalize-whitespace: 1 |
| 526 |
|
| 527 |
P10 @4-4 L1c13 expression -> . 'string' '(' <numeric expression> ')' |
| 528 |
|
| 529 |
=for Marpa::R2::Display::End |
| 530 |
|
| 531 |
In both cases these are predictions of a string operator, the operator we plan to |
| 532 |
use for converting numerics to strings. |
| 533 |
They are just predictions, predictions which go no further because there is no |
| 534 |
'C<string>' operator in our input. |
| 535 |
That's fine, but why no other, more relevant, occurrences of C<E<lt>numeric expressionE<gt>>? |
| 536 |
|
| 537 |
We look back at the grammar. |
| 538 |
Aside for the rule for the 'C<string>' operator, |
| 539 |
C<E<lt>numeric expressionE<gt>> occurs on a RHS in two places. |
| 540 |
One is in the prioritized rule which defines |
| 541 |
C<E<lt>numeric expressionE<gt>>. |
| 542 |
|
| 543 |
=for Marpa::R2::Display |
| 544 |
name: SLIF debug example, part 1 |
| 545 |
partial: 1 |
| 546 |
normalize-whitespace: 1 |
| 547 |
|
| 548 |
<numeric expression> ::= |
| 549 |
variable | number |
| 550 |
|| <numeric expression> '*' <numeric expression> |
| 551 |
|| <numeric expression> '+' <numeric expression> |
| 552 |
|
| 553 |
=for Marpa::R2::Display::End |
| 554 |
|
| 555 |
This rule |
| 556 |
will never put |
| 557 |
C<E<lt>numeric expressionE<gt>> into the Earley items |
| 558 |
unless there is |
| 559 |
a C<E<lt>numeric expressionE<gt>> already there. |
| 560 |
But that is not its job. |
| 561 |
This rule is just fine and does not need fixing. |
| 562 |
|
| 563 |
That leaves one rule to look at. |
| 564 |
|
| 565 |
=for Marpa::R2::Display |
| 566 |
name: SLIF debug example, part 1 |
| 567 |
partial: 1 |
| 568 |
normalize-whitespace: 1 |
| 569 |
|
| 570 |
<numeric assignment> ::= variable '=' expression |
| 571 |
|
| 572 |
=for Marpa::R2::Display::End |
| 573 |
|
| 574 |
This rule is one that |
| 575 |
should lead to the prediction of a new |
| 576 |
C<E<lt>numeric expressionE<gt>> in our example. |
| 577 |
And now we see our problem. |
| 578 |
This rule is never leading to the prediction of a new |
| 579 |
C<E<lt>numeric expressionE<gt>>, |
| 580 |
because there is no |
| 581 |
C<E<lt>numeric expressionE<gt>> on its RHS, |
| 582 |
or for that matter anywhere else in it. |
| 583 |
On the RHS, where we wrote C<E<lt>expressionE<gt>>, |
| 584 |
we should have written |
| 585 |
C<E<lt>numeric expressionE<gt>>. |
| 586 |
Change that and the problem is fixed. |
| 587 |
|
| 588 |
=head1 Complications |
| 589 |
|
| 590 |
We have finished our main example. |
| 591 |
This section discusses some aspects of debugging which did not |
| 592 |
arise in the example, |
| 593 |
and which might be unexpected. |
| 594 |
|
| 595 |
=head2 Empty rules |
| 596 |
|
| 597 |
When a symbol is nulled in your parse, |
| 598 |
L<C<show_progress>|Marpa::R2::Scanless::R/"show_progress()"> |
| 599 |
shows only the nulled symbol. |
| 600 |
It does not show the symbol's expansion into rules, |
| 601 |
or any of its nulled child symbols. |
| 602 |
This reduces clutter, |
| 603 |
and usually one does not notice the missing nulled rules and symbols. |
| 604 |
Not showing these |
| 605 |
seems to be the intuitive way to treat them. |
| 606 |
|
| 607 |
=head2 Input string ranges |
| 608 |
|
| 609 |
G1 locations run in a monotonic sequence, starting with 0. |
| 610 |
G1 locations never run backwards, they are never visited twice, |
| 611 |
and they leave no gaps. |
| 612 |
|
| 613 |
Input string positions, on the other hand, can do all of these things. |
| 614 |
An application is allowed to jump around in the input. |
| 615 |
An input string position may be encountered more than once. |
| 616 |
It is quite possible to write your application so that it |
| 617 |
encounters, for example, |
| 618 |
line 42 before line 7. |
| 619 |
And your application does not have to visit line 42 on its way from |
| 620 |
line 41 to line 43. |
| 621 |
For that matter, |
| 622 |
an application does not ever have to visit any position in its input. |
| 623 |
|
| 624 |
How does Marpa deal with this when reporting input string ranges? |
| 625 |
Marpa always reports the minimum range that includes all the input string positions |
| 626 |
visited in the dotted rule. |
| 627 |
The range is always reported in increasing numeric order, |
| 628 |
even when the position at |
| 629 |
the end of the range was visited before the input string position |
| 630 |
at the beginning of the range. |
| 631 |
And, if necessary to include all visited input string positions, the range |
| 632 |
may include input string positions which were not visited. |
| 633 |
|
| 634 |
Most applications move forward continuously in the input string, |
| 635 |
and if yours is one of them, you don't have to worry about these issues. |
| 636 |
But if you do unusual things when reading the input, |
| 637 |
it helps to be aware of how input string ranges are reported by Marpa |
| 638 |
when tracing and debugging. |
| 639 |
|
| 640 |
=head2 Multiple instances of dotted rules |
| 641 |
|
| 642 |
It does not happen in our main example for this document, |
| 643 |
but a dotted rule can appear in the same Earley set |
| 644 |
more than once. |
| 645 |
In fact, this happens frequently. |
| 646 |
When it does happen, the lines in the progress report will look like these |
| 647 |
|
| 648 |
=for Marpa::R2::Display |
| 649 |
name: SLIF Leo show_progress() example |
| 650 |
partial: 1 |
| 651 |
normalize-whitespace: 1 |
| 652 |
|
| 653 |
F11 x12 @0...38-41 L1c1-L2c40 <plain assignment> -> 'x' '=' expression . |
| 654 |
|
| 655 |
=for Marpa::R2::Display::End |
| 656 |
|
| 657 |
=for Marpa::R2::Display |
| 658 |
name: SLIF Leo show_progress() example |
| 659 |
partial: 1 |
| 660 |
normalize-whitespace: 1 |
| 661 |
|
| 662 |
F1 x20 @0...38-41 L1c1-L2c40 expression -> assignment . |
| 663 |
|
| 664 |
=for Marpa::R2::Display::End |
| 665 |
|
| 666 |
=for Marpa::R2::Display |
| 667 |
name: SLIF Leo show_progress() example |
| 668 |
partial: 1 |
| 669 |
normalize-whitespace: 1 |
| 670 |
|
| 671 |
F6 x12 @0...38-41 L1c1-L2c40 assignment -> <plain assignment> . |
| 672 |
|
| 673 |
=for Marpa::R2::Display::End |
| 674 |
|
| 675 |
These are some of the progress report lines for |
| 676 |
an indirect right recursion, one that recurses from |
| 677 |
a C<E<lt>plain assignmentE<gt>> symbol to |
| 678 |
an C<E<lt>expressionE<gt>> symbol, and then to |
| 679 |
an C<E<lt>assignmentE<gt>> symbol, |
| 680 |
before completing the recursion by returning to |
| 681 |
a C<E<lt>plain assignmentE<gt>>. |
| 682 |
|
| 683 |
In each of the three lines, notice that a new field appears second. |
| 684 |
This second field is variously |
| 685 |
"C<x12>" or "C<x20>". |
| 686 |
These are counts, |
| 687 |
indicating the number of instances of that dotted rule at |
| 688 |
the dotted rule's G1 dot location. |
| 689 |
Every dotted rule instance will have the same G1 location, |
| 690 |
but the instances may have many different origins -- hundreds or even more. |
| 691 |
In each of the three report lines above, the G1 dot location is 41. |
| 692 |
|
| 693 |
Note that when parsing, Marpa handles the long series of Earley items |
| 694 |
generated by right recursions very efficiently. |
| 695 |
It uses a technique invented by Joop Leo to memoize and eliminate them. |
| 696 |
When a progress report is requested at a G1 location, the Leo-memoization |
| 697 |
is unfolded, and the full list of Earley items is reported. |
| 698 |
|
| 699 |
Each instance may have its own span in the input string, |
| 700 |
and the input string range will include them all. |
| 701 |
When there are many |
| 702 |
instances of a dotted rule at a single location, |
| 703 |
the origins in the location field are shown as a |
| 704 |
range, with the earliest separated from the most recent |
| 705 |
by a "C<...>". |
| 706 |
For example, above, |
| 707 |
where the first four fields were |
| 708 |
"C<F7 x12 @0...38-41 L1c1-L2c40>", |
| 709 |
that tells us |
| 710 |
that the dotted rule is rule 7, which has 12 instances. |
| 711 |
All 12 instances have their dot location at G1 location 41, |
| 712 |
but their origins are in the range from G1 location 0 |
| 713 |
to G1 location 38. |
| 714 |
|
| 715 |
The last field in |
| 716 |
"C<F7 x12 @0...38-41 L1c1-L2c40>" is an input string range. |
| 717 |
"C<L1c1-L2c40>" says that |
| 718 |
input string positions visited |
| 719 |
by the 12 instances start at line 1, column 1, |
| 720 |
and end at line 2, column 40. |
| 721 |
The reported input string range will be the shortest range that includes |
| 722 |
all of the input string positions visited by any of the dotted rule instances. |
| 723 |
|
| 724 |
If there are only a few origins, |
| 725 |
Marpa may explicitly list them all. |
| 726 |
In the follow example, there are only 2 instances |
| 727 |
of this rule, both with a dot location of 41. |
| 728 |
Their origins are at G1 locations 8 and 18. |
| 729 |
The range of input string positions is from line 1, column 17 |
| 730 |
to line 2, column 40. |
| 731 |
|
| 732 |
=for Marpa::R2::Display |
| 733 |
name: SLIF Leo show_progress() example |
| 734 |
partial: 1 |
| 735 |
normalize-whitespace: 1 |
| 736 |
|
| 737 |
F2 x2 @8,18-41 L1c17-L2c40 assignment -> <divide assignment> . |
| 738 |
|
| 739 |
=for Marpa::R2::Display::End |
| 740 |
|
| 741 |
=head1 Access to the "raw" progress report information |
| 742 |
|
| 743 |
This section deals with the C<progress()> recognizer method, |
| 744 |
which allows access to the raw progress report information. |
| 745 |
This method is not needed for typical debugging and tracing |
| 746 |
situations. |
| 747 |
It is intended for applications which want to leverage |
| 748 |
Marpa's "situational awareness" in innovative ways. |
| 749 |
|
| 750 |
=head2 progress() |
| 751 |
|
| 752 |
=for Marpa::R2::Display |
| 753 |
name: SLIF progress(0) example |
| 754 |
normalize-whitespace: 1 |
| 755 |
|
| 756 |
my $report0 = $recce->progress(0); |
| 757 |
|
| 758 |
=for Marpa::R2::Display::End |
| 759 |
|
| 760 |
Z<> |
| 761 |
|
| 762 |
=for Marpa::R2::Display |
| 763 |
name: SLIF progress() example |
| 764 |
normalize-whitespace: 1 |
| 765 |
|
| 766 |
my $latest_report = $recce->progress(); |
| 767 |
|
| 768 |
=for Marpa::R2::Display::End |
| 769 |
|
| 770 |
Given the G1 location (Earley set ID) as its argument, |
| 771 |
the C<progress()> recognizer method returns a reference |
| 772 |
to an array of "report items". |
| 773 |
The G1 location may be given as a negative number. |
| 774 |
An argument of I<-X> |
| 775 |
will be interpreted as G1 location I<N-(X+1)>, where I<N> is |
| 776 |
the latest Earley set. |
| 777 |
This means that an argument of -1 indicates the latest |
| 778 |
Earley set, |
| 779 |
an argument of -2 indicates the Earley set just before |
| 780 |
the latest one, etc. |
| 781 |
|
| 782 |
Each report item is a triple: an array of three elements. |
| 783 |
The three elements are, in order, |
| 784 |
rule ID, dot position, and origin. |
| 785 |
The data returned by the two displays above, |
| 786 |
as well as the data for the other G1 locations |
| 787 |
in our example, |
| 788 |
are shown L<below|"progress() outputs">. |
| 789 |
|
| 790 |
The rule ID is the same number that Marpa uses |
| 791 |
to identify rules |
| 792 |
in tracing and debugging output. |
| 793 |
Given a rule ID, an application can expand it into |
| 794 |
its LHS and RHS symbols using |
| 795 |
L<the SLIF grammar's C<rule_expand()> method|Marpa::R2::Scanless::G/"rule_expand()">. |
| 796 |
Given a symbol ID, |
| 797 |
its name and other information can be found using |
| 798 |
L<other SLIF grammar |
| 799 |
methods|Marpa::R2::Scanless::G/"Accessors">. |
| 800 |
|
| 801 |
Dot position is -1 for completions, |
| 802 |
and 0 for predictions. |
| 803 |
Where the report item is not for a completion or |
| 804 |
a prediction, dot position is I<N>, |
| 805 |
where I<N> is the number of RHS symbols |
| 806 |
successfully recognized at the G1 location of |
| 807 |
the progress report. |
| 808 |
|
| 809 |
Origin is the G1 location (Earley set ID) |
| 810 |
at which the rule application reported by |
| 811 |
the report item began. |
| 812 |
For a prediction, origin will always be |
| 813 |
the same as the G1 location of the parse report. |
| 814 |
|
| 815 |
=head2 Progress reports and efficiency |
| 816 |
|
| 817 |
When progress reports are used for production parsing, |
| 818 |
instead of just for debugging and tracing, |
| 819 |
efficiency considerations become significant. |
| 820 |
Progress reports themselves are implemented in optimized C, |
| 821 |
and that logic is very fast. |
| 822 |
However, the use of progress reports usually implies considerable |
| 823 |
post-processing in Perl. |
| 824 |
It is almost always possible to use Marpa's named events instead |
| 825 |
of progress reports, and solutions using named events are usually |
| 826 |
better targeted, simpler and faster. |
| 827 |
|
| 828 |
If you do decide to use progress reports in an application, |
| 829 |
you should be aware of the efficiency considerations when there |
| 830 |
are right recursions in the grammar. |
| 831 |
For most purposes, |
| 832 |
Marpa optimizes right recursions, |
| 833 |
so that they run in linear time. |
| 834 |
However, to create a progress report every potential right recursion must |
| 835 |
be fully unfolded, and at each G1 location the number of these grows linearly with the length |
| 836 |
of the recursion. |
| 837 |
If you are creating progress reports for more than a limited number of G1 locations, |
| 838 |
this means processing that can be quadratic in the length of the recursion. |
| 839 |
When a right recursion is lengthy, the impact on speed can be be very serious. |
| 840 |
|
| 841 |
If lengthy right recursions are being expanded, |
| 842 |
this will be evident |
| 843 |
from the parse report itself, |
| 844 |
which will contain one report item |
| 845 |
for every completion in the right-recursive |
| 846 |
chain of completions. |
| 847 |
Note that the efficiency consideration just mentioned |
| 848 |
for following right recursions |
| 849 |
is never an issue for left recursions. |
| 850 |
Left recursions produce at most two report |
| 851 |
items per G1 location |
| 852 |
and are extremely fast to process. |
| 853 |
It is also not an issue for |
| 854 |
Marpa's sequence rules, because sequence rules |
| 855 |
are implemented internally as left recursions. |
| 856 |
|
| 857 |
=head1 Appendix |
| 858 |
|
| 859 |
Below are the code, the trace outputs |
| 860 |
and the progress report |
| 861 |
for the example used in this |
| 862 |
document. |
| 863 |
|
| 864 |
=head2 Code |
| 865 |
|
| 866 |
=for Marpa::R2::Display: |
| 867 |
name: SLIF debug example, part 1 |
| 868 |
normalize-whitespace: 1 |
| 869 |
|
| 870 |
my $slif_debug_source = <<'END_OF_SOURCE'; |
| 871 |
:default ::= action => ::array bless => ::lhs |
| 872 |
:start ::= statements |
| 873 |
statements ::= statement * |
| 874 |
statement ::= assignment | <numeric assignment> |
| 875 |
assignment ::= 'set' variable 'to' expression |
| 876 |
|
| 877 |
# This is a deliberate error in the grammar |
| 878 |
# The next line should be: |
| 879 |
# <numeric assignment> ::= variable '=' <numeric expression> |
| 880 |
# I have changed the <numeric expression> to <expression> which |
| 881 |
# will cause problems. |
| 882 |
<numeric assignment> ::= variable '=' expression |
| 883 |
|
| 884 |
expression ::= |
| 885 |
variable | string |
| 886 |
|| 'string' '(' <numeric expression> ')' |
| 887 |
|| expression '+' expression |
| 888 |
<numeric expression> ::= |
| 889 |
variable | number |
| 890 |
|| <numeric expression> '*' <numeric expression> |
| 891 |
|| <numeric expression> '+' <numeric expression> |
| 892 |
variable ~ [\w]+ |
| 893 |
number ~ [\d]+ |
| 894 |
string ~ ['] <string contents> ['] |
| 895 |
<string contents> ~ [^'\x{0A}\x{0B}\x{0C}\x{0D}\x{0085}\x{2028}\x{2029}]+ |
| 896 |
:discard ~ whitespace |
| 897 |
whitespace ~ [\s]+ |
| 898 |
END_OF_SOURCE |
| 899 |
|
| 900 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 901 |
{ |
| 902 |
bless_package => 'My_Nodes', |
| 903 |
source => \$slif_debug_source, |
| 904 |
}); |
| 905 |
|
| 906 |
=for Marpa::R2::Display::End |
| 907 |
|
| 908 |
=for Marpa::R2::Display: |
| 909 |
name: SLIF debug example, part 2 |
| 910 |
perltidy: '-dcsc -sil=0' |
| 911 |
|
| 912 |
my $recce = Marpa::R2::Scanless::R->new( |
| 913 |
{ grammar => $grammar, |
| 914 |
trace_terminals => 1, |
| 915 |
trace_values => 1, |
| 916 |
} ); |
| 917 |
|
| 918 |
my $test_input = 'a = 8675309 + 42 * 711' ; |
| 919 |
my $eval_error = $EVAL_ERROR if not eval { $recce->read( \$test_input ); 1 }; |
| 920 |
|
| 921 |
$progress_report = $recce->show_progress( 0, -1 ); |
| 922 |
|
| 923 |
=for Marpa::R2::Display::End |
| 924 |
|
| 925 |
=head2 Error message |
| 926 |
|
| 927 |
=for Marpa::R2::Display |
| 928 |
name: SLIF debug example error message |
| 929 |
partial: 1 |
| 930 |
remove-display-indent: 1 |
| 931 |
remove-blank-last-line: 1 |
| 932 |
|
| 933 |
Error in SLIF parse: No lexemes accepted at line 1, column 18 |
| 934 |
Rejected lexeme #0: '*'; value="*"; length = 1 |
| 935 |
* String before error: a = 8675309 + 42\s |
| 936 |
* The error was at line 1, column 18, and at character 0x002a '*', ... |
| 937 |
* here: * 711 |
| 938 |
|
| 939 |
=for Marpa::R2::Display::End |
| 940 |
|
| 941 |
=head2 Parse value at error location |
| 942 |
|
| 943 |
Note that when there is a parse error, there will |
| 944 |
not always be a parse value. |
| 945 |
But sometimes the parse is "successful" enough, in a technical sense, |
| 946 |
to produce a value, and in those cases examining the value can be |
| 947 |
helpful in determining what the parser thinks it has seen so far. |
| 948 |
|
| 949 |
=for Marpa::R2::Display |
| 950 |
name: SLIF debug example dump of value |
| 951 |
partial: 1 |
| 952 |
normalize-whitespace: 1 |
| 953 |
|
| 954 |
my $value_ref = $recce->value(); |
| 955 |
my $expected_output = \bless( |
| 956 |
[ |
| 957 |
bless( |
| 958 |
[ |
| 959 |
bless( |
| 960 |
[ |
| 961 |
'a', '=', |
| 962 |
bless( |
| 963 |
[ |
| 964 |
bless( ['8675309'], 'My_Nodes::expression' ), |
| 965 |
'+', |
| 966 |
bless( ['42'], 'My_Nodes::expression' ) |
| 967 |
], |
| 968 |
'My_Nodes::expression' |
| 969 |
) |
| 970 |
], |
| 971 |
'My_Nodes::numeric_assignment' |
| 972 |
) |
| 973 |
], |
| 974 |
'My_Nodes::statement' |
| 975 |
) |
| 976 |
], |
| 977 |
'My_Nodes::statements' |
| 978 |
); |
| 979 |
|
| 980 |
=for Marpa::R2::Display::End |
| 981 |
|
| 982 |
=head2 Trace output |
| 983 |
|
| 984 |
=for Marpa::R2::Display |
| 985 |
name: SLIF debug example trace output |
| 986 |
remove-display-indent: 1 |
| 987 |
remove-blank-last-line: 1 |
| 988 |
|
| 989 |
Setting trace_terminals option |
| 990 |
Setting trace_values option |
| 991 |
Accepted lexeme L1c1 e1: variable; value="a" |
| 992 |
Discarded lexeme L1c2: whitespace |
| 993 |
Accepted lexeme L1c3 e2: '='; value="=" |
| 994 |
Discarded lexeme L1c4: whitespace |
| 995 |
Rejected lexeme L1c5-11: number; value="8675309" |
| 996 |
Accepted lexeme L1c5-11 e3: variable; value="8675309" |
| 997 |
Discarded lexeme L1c12: whitespace |
| 998 |
Rejected lexeme L1c13: '+'; value="+" |
| 999 |
Accepted lexeme L1c13 e4: '+'; value="+" |
| 1000 |
Discarded lexeme L1c14: whitespace |
| 1001 |
Rejected lexeme L1c15-16: number; value="42" |
| 1002 |
Accepted lexeme L1c15-16 e5: variable; value="42" |
| 1003 |
Discarded lexeme L1c17: whitespace |
| 1004 |
Rejected lexeme L1c18: '*'; value="*" |
| 1005 |
|
| 1006 |
=for Marpa::R2::Display::End |
| 1007 |
|
| 1008 |
=head2 show_progress() output |
| 1009 |
|
| 1010 |
=for Marpa::R2::Display |
| 1011 |
name: SLIF debug example progress report |
| 1012 |
remove-display-indent: 1 |
| 1013 |
remove-blank-last-line: 1 |
| 1014 |
|
| 1015 |
P0 @0-0 L0c0 statements -> . statement * |
| 1016 |
P1 @0-0 L0c0 statement -> . assignment |
| 1017 |
P2 @0-0 L0c0 statement -> . <numeric assignment> |
| 1018 |
P3 @0-0 L0c0 assignment -> . 'set' variable 'to' expression |
| 1019 |
P4 @0-0 L0c0 <numeric assignment> -> . variable '=' expression |
| 1020 |
P19 @0-0 L0c0 :start -> . statements |
| 1021 |
R4:1 @0-1 L1c1 <numeric assignment> -> variable . '=' expression |
| 1022 |
R4:2 @0-2 L1c1-3 <numeric assignment> -> variable '=' . expression |
| 1023 |
P5 @2-2 L1c3 expression -> . expression |
| 1024 |
P6 @2-2 L1c3 expression -> . expression |
| 1025 |
P7 @2-2 L1c3 expression -> . expression |
| 1026 |
P8 @2-2 L1c3 expression -> . variable |
| 1027 |
P9 @2-2 L1c3 expression -> . string |
| 1028 |
P10 @2-2 L1c3 expression -> . 'string' '(' <numeric expression> ')' |
| 1029 |
P11 @2-2 L1c3 expression -> . expression '+' expression |
| 1030 |
F0 @0-3 L1c1-11 statements -> statement * . |
| 1031 |
P1 @3-3 L1c5-11 statement -> . assignment |
| 1032 |
P2 @3-3 L1c5-11 statement -> . <numeric assignment> |
| 1033 |
F2 @0-3 L1c1-11 statement -> <numeric assignment> . |
| 1034 |
P3 @3-3 L1c5-11 assignment -> . 'set' variable 'to' expression |
| 1035 |
P4 @3-3 L1c5-11 <numeric assignment> -> . variable '=' expression |
| 1036 |
F4 @0-3 L1c1-11 <numeric assignment> -> variable '=' expression . |
| 1037 |
F5 @2-3 L1c3-11 expression -> expression . |
| 1038 |
F6 @2-3 L1c3-11 expression -> expression . |
| 1039 |
F7 @2-3 L1c3-11 expression -> expression . |
| 1040 |
F8 @2-3 L1c3-11 expression -> variable . |
| 1041 |
R11:1 @2-3 L1c3-11 expression -> expression . '+' expression |
| 1042 |
F19 @0-3 L1c1-11 :start -> statements . |
| 1043 |
P7 @4-4 L1c13 expression -> . expression |
| 1044 |
P8 @4-4 L1c13 expression -> . variable |
| 1045 |
P9 @4-4 L1c13 expression -> . string |
| 1046 |
P10 @4-4 L1c13 expression -> . 'string' '(' <numeric expression> ')' |
| 1047 |
R11:2 @2-4 L1c3-13 expression -> expression '+' . expression |
| 1048 |
F0 @0-5 L1c1-16 statements -> statement * . |
| 1049 |
P1 @5-5 L1c15-16 statement -> . assignment |
| 1050 |
P2 @5-5 L1c15-16 statement -> . <numeric assignment> |
| 1051 |
F2 @0-5 L1c1-16 statement -> <numeric assignment> . |
| 1052 |
P3 @5-5 L1c15-16 assignment -> . 'set' variable 'to' expression |
| 1053 |
P4 @5-5 L1c15-16 <numeric assignment> -> . variable '=' expression |
| 1054 |
F4 @0-5 L1c1-16 <numeric assignment> -> variable '=' expression . |
| 1055 |
F5 @2-5 L1c3-16 expression -> expression . |
| 1056 |
F7 @4-5 L1c13-16 expression -> expression . |
| 1057 |
F8 @4-5 L1c13-16 expression -> variable . |
| 1058 |
R11:1 @2-5 L1c3-16 expression -> expression . '+' expression |
| 1059 |
F11 @2-5 L1c3-16 expression -> expression '+' expression . |
| 1060 |
F19 @0-5 L1c1-16 :start -> statements . |
| 1061 |
|
| 1062 |
=for Marpa::R2::Display::End |
| 1063 |
|
| 1064 |
=head2 show_rules() output |
| 1065 |
|
| 1066 |
This is the G1 portion of the C<show_rules()> output at verbosity level 3. |
| 1067 |
In ordinary work, you'd use verbosity level 1 (the default), |
| 1068 |
but the more verbose output is included here to illustrate the example. |
| 1069 |
|
| 1070 |
=for Marpa::R2::Display |
| 1071 |
name: SLIF debug example show_rules() output |
| 1072 |
remove-display-indent: 1 |
| 1073 |
remove-blank-last-line: 1 |
| 1074 |
partial: 1 |
| 1075 |
|
| 1076 |
G1 Rules: |
| 1077 |
G1 R0 statements ::= statement * |
| 1078 |
Symbol IDs: <16> ::= <17> |
| 1079 |
Internal symbols: <statements> ::= <statement> |
| 1080 |
G1 R1 statement ::= assignment |
| 1081 |
Symbol IDs: <17> ::= <18> |
| 1082 |
Internal symbols: <statement> ::= <assignment> |
| 1083 |
G1 R2 statement ::= <numeric assignment> |
| 1084 |
Symbol IDs: <17> ::= <19> |
| 1085 |
Internal symbols: <statement> ::= <numeric assignment> |
| 1086 |
G1 R3 assignment ::= 'set' variable 'to' expression |
| 1087 |
Symbol IDs: <18> ::= <1> <20> <2> <21> |
| 1088 |
Internal symbols: <assignment> ::= <[Lex-0]> <variable> <[Lex-1]> <expression> |
| 1089 |
G1 R4 <numeric assignment> ::= variable '=' <numeric expression> |
| 1090 |
Symbol IDs: <19> ::= <20> <3> <22> |
| 1091 |
Internal symbols: <numeric assignment> ::= <variable> <[Lex-2]> <numeric expression> |
| 1092 |
G1 R5 expression ::= expression |
| 1093 |
Internal rule top priority rule for <expression> |
| 1094 |
Symbol IDs: <21> ::= <10> |
| 1095 |
Internal symbols: <expression> ::= <expression[0]> |
| 1096 |
G1 R6 expression ::= expression |
| 1097 |
Internal rule for symbol <expression> priority transition from 0 to 1 |
| 1098 |
Symbol IDs: <10> ::= <11> |
| 1099 |
Internal symbols: <expression[0]> ::= <expression[1]> |
| 1100 |
G1 R7 expression ::= expression |
| 1101 |
Internal rule for symbol <expression> priority transition from 1 to 2 |
| 1102 |
Symbol IDs: <11> ::= <12> |
| 1103 |
Internal symbols: <expression[1]> ::= <expression[2]> |
| 1104 |
G1 R8 expression ::= variable |
| 1105 |
Symbol IDs: <12> ::= <20> |
| 1106 |
Internal symbols: <expression[2]> ::= <variable> |
| 1107 |
G1 R9 expression ::= string |
| 1108 |
Symbol IDs: <12> ::= <23> |
| 1109 |
Internal symbols: <expression[2]> ::= <string> |
| 1110 |
G1 R10 expression ::= 'string' '(' <numeric expression> ')' |
| 1111 |
Symbol IDs: <11> ::= <4> <5> <22> <6> |
| 1112 |
Internal symbols: <expression[1]> ::= <[Lex-3]> <[Lex-4]> <numeric expression> <[Lex-5]> |
| 1113 |
G1 R11 expression ::= expression '+' expression |
| 1114 |
Symbol IDs: <10> ::= <10> <7> <11> |
| 1115 |
Internal symbols: <expression[0]> ::= <expression[0]> <[Lex-6]> <expression[1]> |
| 1116 |
G1 R12 <numeric expression> ::= <numeric expression> |
| 1117 |
Internal rule top priority rule for <numeric expression> |
| 1118 |
Symbol IDs: <22> ::= <13> |
| 1119 |
Internal symbols: <numeric expression> ::= <numeric expression[0]> |
| 1120 |
G1 R13 <numeric expression> ::= <numeric expression> |
| 1121 |
Internal rule for symbol <numeric expression> priority transition from 0 to 1 |
| 1122 |
Symbol IDs: <13> ::= <14> |
| 1123 |
Internal symbols: <numeric expression[0]> ::= <numeric expression[1]> |
| 1124 |
G1 R14 <numeric expression> ::= <numeric expression> |
| 1125 |
Internal rule for symbol <numeric expression> priority transition from 1 to 2 |
| 1126 |
Symbol IDs: <14> ::= <15> |
| 1127 |
Internal symbols: <numeric expression[1]> ::= <numeric expression[2]> |
| 1128 |
G1 R15 <numeric expression> ::= variable |
| 1129 |
Symbol IDs: <15> ::= <20> |
| 1130 |
Internal symbols: <numeric expression[2]> ::= <variable> |
| 1131 |
G1 R16 <numeric expression> ::= number |
| 1132 |
Symbol IDs: <15> ::= <24> |
| 1133 |
Internal symbols: <numeric expression[2]> ::= <number> |
| 1134 |
G1 R17 <numeric expression> ::= <numeric expression> '*' <numeric expression> |
| 1135 |
Symbol IDs: <14> ::= <14> <8> <15> |
| 1136 |
Internal symbols: <numeric expression[1]> ::= <numeric expression[1]> <[Lex-7]> <numeric expression[2]> |
| 1137 |
G1 R18 <numeric expression> ::= <numeric expression> '+' <numeric expression> |
| 1138 |
Symbol IDs: <13> ::= <13> <9> <14> |
| 1139 |
Internal symbols: <numeric expression[0]> ::= <numeric expression[0]> <[Lex-8]> <numeric expression[1]> |
| 1140 |
G1 R19 :start ::= statements |
| 1141 |
Symbol IDs: <0> ::= <16> |
| 1142 |
Internal symbols: <[:start]> ::= <statements> |
| 1143 |
Lex (L0) Rules: |
| 1144 |
L0 R0 'set' ::= [s] [e] [t] |
| 1145 |
Internal rule for single-quoted string 'set' |
| 1146 |
Symbol IDs: <2> ::= <27> <21> <28> |
| 1147 |
Internal symbols: <[Lex-0]> ::= <[[s]]> <[[e]]> <[[t]]> |
| 1148 |
L0 R1 'to' ::= [t] [o] |
| 1149 |
Internal rule for single-quoted string 'to' |
| 1150 |
Symbol IDs: <3> ::= <28> <25> |
| 1151 |
Internal symbols: <[Lex-1]> ::= <[[t]]> <[[o]]> |
| 1152 |
L0 R2 '=' ::= [\=] |
| 1153 |
Internal rule for single-quoted string '=' |
| 1154 |
Symbol IDs: <4> ::= <16> |
| 1155 |
Internal symbols: <[Lex-2]> ::= <[[\=]]> |
| 1156 |
L0 R3 'string' ::= [s] [t] [r] [i] [n] [g] |
| 1157 |
Internal rule for single-quoted string 'string' |
| 1158 |
Symbol IDs: <5> ::= <27> <28> <26> <23> <24> <22> |
| 1159 |
Internal symbols: <[Lex-3]> ::= <[[s]]> <[[t]]> <[[r]]> <[[i]]> <[[n]]> <[[g]]> |
| 1160 |
L0 R4 '(' ::= [\(] |
| 1161 |
Internal rule for single-quoted string '(' |
| 1162 |
Symbol IDs: <6> ::= <12> |
| 1163 |
Internal symbols: <[Lex-4]> ::= <[[\(]]> |
| 1164 |
L0 R5 ')' ::= [\)] |
| 1165 |
Internal rule for single-quoted string ')' |
| 1166 |
Symbol IDs: <7> ::= <13> |
| 1167 |
Internal symbols: <[Lex-5]> ::= <[[\)]]> |
| 1168 |
L0 R6 '+' ::= [\+] |
| 1169 |
Internal rule for single-quoted string '+' |
| 1170 |
Symbol IDs: <8> ::= <15> |
| 1171 |
Internal symbols: <[Lex-6]> ::= <[[\+]]> |
| 1172 |
L0 R7 '*' ::= [\*] |
| 1173 |
Internal rule for single-quoted string '*' |
| 1174 |
Symbol IDs: <9> ::= <14> |
| 1175 |
Internal symbols: <[Lex-7]> ::= <[[\*]]> |
| 1176 |
L0 R8 '+' ::= [\+] |
| 1177 |
Internal rule for single-quoted string '+' |
| 1178 |
Symbol IDs: <10> ::= <15> |
| 1179 |
Internal symbols: <[Lex-8]> ::= <[[\+]]> |
| 1180 |
L0 R9 variable ::= [\w] + |
| 1181 |
Symbol IDs: <29> ::= <19> |
| 1182 |
Internal symbols: <variable> ::= <[[\w]]> |
| 1183 |
L0 R10 number ::= [\d] + |
| 1184 |
Symbol IDs: <30> ::= <17> |
| 1185 |
Internal symbols: <number> ::= <[[\d]]> |
| 1186 |
L0 R11 string ::= ['] <string contents> ['] |
| 1187 |
Symbol IDs: <31> ::= <11> <32> <11> |
| 1188 |
Internal symbols: <string> ::= <[[']]> <string contents> <[[']]> |
| 1189 |
L0 R12 <string contents> ::= [^'\x{0A}\x{0B}\x{0C}\x{0D}\x{0085}\x{2028}\x{2029}] + |
| 1190 |
Symbol IDs: <32> ::= <20> |
| 1191 |
Internal symbols: <string contents> ::= <[[^'\x{0A}\x{0B}\x{0C}\x{0D}\x{0085}\x{2028}\x{2029}]]> |
| 1192 |
L0 R13 :discard ::= whitespace |
| 1193 |
Discard rule for <whitespace> |
| 1194 |
Symbol IDs: <0> ::= <33> |
| 1195 |
Internal symbols: <[:discard]> ::= <whitespace> |
| 1196 |
L0 R14 whitespace ::= [\s] + |
| 1197 |
Symbol IDs: <33> ::= <18> |
| 1198 |
Internal symbols: <whitespace> ::= <[[\s]]> |
| 1199 |
L0 R15 :start_lex ::= :discard |
| 1200 |
Internal lexical start rule for <[:discard]> |
| 1201 |
Symbol IDs: <1> ::= <0> |
| 1202 |
Internal symbols: <[:start_lex]> ::= <[:discard]> |
| 1203 |
L0 R16 :start_lex ::= 'set' |
| 1204 |
Internal lexical start rule for <[Lex-0]> |
| 1205 |
Symbol IDs: <1> ::= <2> |
| 1206 |
Internal symbols: <[:start_lex]> ::= <[Lex-0]> |
| 1207 |
L0 R17 :start_lex ::= 'to' |
| 1208 |
Internal lexical start rule for <[Lex-1]> |
| 1209 |
Symbol IDs: <1> ::= <3> |
| 1210 |
Internal symbols: <[:start_lex]> ::= <[Lex-1]> |
| 1211 |
L0 R18 :start_lex ::= '=' |
| 1212 |
Internal lexical start rule for <[Lex-2]> |
| 1213 |
Symbol IDs: <1> ::= <4> |
| 1214 |
Internal symbols: <[:start_lex]> ::= <[Lex-2]> |
| 1215 |
L0 R19 :start_lex ::= 'string' |
| 1216 |
Internal lexical start rule for <[Lex-3]> |
| 1217 |
Symbol IDs: <1> ::= <5> |
| 1218 |
Internal symbols: <[:start_lex]> ::= <[Lex-3]> |
| 1219 |
L0 R20 :start_lex ::= '(' |
| 1220 |
Internal lexical start rule for <[Lex-4]> |
| 1221 |
Symbol IDs: <1> ::= <6> |
| 1222 |
Internal symbols: <[:start_lex]> ::= <[Lex-4]> |
| 1223 |
L0 R21 :start_lex ::= ')' |
| 1224 |
Internal lexical start rule for <[Lex-5]> |
| 1225 |
Symbol IDs: <1> ::= <7> |
| 1226 |
Internal symbols: <[:start_lex]> ::= <[Lex-5]> |
| 1227 |
L0 R22 :start_lex ::= '+' |
| 1228 |
Internal lexical start rule for <[Lex-6]> |
| 1229 |
Symbol IDs: <1> ::= <8> |
| 1230 |
Internal symbols: <[:start_lex]> ::= <[Lex-6]> |
| 1231 |
L0 R23 :start_lex ::= '*' |
| 1232 |
Internal lexical start rule for <[Lex-7]> |
| 1233 |
Symbol IDs: <1> ::= <9> |
| 1234 |
Internal symbols: <[:start_lex]> ::= <[Lex-7]> |
| 1235 |
L0 R24 :start_lex ::= '+' |
| 1236 |
Internal lexical start rule for <[Lex-8]> |
| 1237 |
Symbol IDs: <1> ::= <10> |
| 1238 |
Internal symbols: <[:start_lex]> ::= <[Lex-8]> |
| 1239 |
L0 R25 :start_lex ::= number |
| 1240 |
Internal lexical start rule for <number> |
| 1241 |
Symbol IDs: <1> ::= <30> |
| 1242 |
Internal symbols: <[:start_lex]> ::= <number> |
| 1243 |
L0 R26 :start_lex ::= string |
| 1244 |
Internal lexical start rule for <string> |
| 1245 |
Symbol IDs: <1> ::= <31> |
| 1246 |
Internal symbols: <[:start_lex]> ::= <string> |
| 1247 |
L0 R27 :start_lex ::= variable |
| 1248 |
Internal lexical start rule for <variable> |
| 1249 |
Symbol IDs: <1> ::= <29> |
| 1250 |
Internal symbols: <[:start_lex]> ::= <variable> |
| 1251 |
|
| 1252 |
=for Marpa::R2::Display::End |
| 1253 |
|
| 1254 |
=head2 show_symbols() output |
| 1255 |
|
| 1256 |
=for Marpa::R2::Display |
| 1257 |
name: SLIF debug example show_symbols() output |
| 1258 |
remove-display-indent: 1 |
| 1259 |
remove-blank-last-line: 1 |
| 1260 |
partial: 1 |
| 1261 |
|
| 1262 |
G1 Symbols: |
| 1263 |
G1 S0 :start -- Internal G1 start symbol |
| 1264 |
Internal name: <[:start]> |
| 1265 |
G1 S1 'set' -- Internal lexical symbol for "'set'" |
| 1266 |
/* terminal */ |
| 1267 |
Internal name: <[Lex-0]> |
| 1268 |
SLIF name: 'set' |
| 1269 |
G1 S2 'to' -- Internal lexical symbol for "'to'" |
| 1270 |
/* terminal */ |
| 1271 |
Internal name: <[Lex-1]> |
| 1272 |
SLIF name: 'to' |
| 1273 |
G1 S3 '=' -- Internal lexical symbol for "'='" |
| 1274 |
/* terminal */ |
| 1275 |
Internal name: <[Lex-2]> |
| 1276 |
SLIF name: '=' |
| 1277 |
G1 S4 'string' -- Internal lexical symbol for "'string'" |
| 1278 |
/* terminal */ |
| 1279 |
Internal name: <[Lex-3]> |
| 1280 |
SLIF name: 'string' |
| 1281 |
G1 S5 '(' -- Internal lexical symbol for "'('" |
| 1282 |
/* terminal */ |
| 1283 |
Internal name: <[Lex-4]> |
| 1284 |
SLIF name: '(' |
| 1285 |
G1 S6 ')' -- Internal lexical symbol for "')'" |
| 1286 |
/* terminal */ |
| 1287 |
Internal name: <[Lex-5]> |
| 1288 |
SLIF name: ')' |
| 1289 |
G1 S7 '+' -- Internal lexical symbol for "'+'" |
| 1290 |
/* terminal */ |
| 1291 |
Internal name: <[Lex-6]> |
| 1292 |
SLIF name: '+' |
| 1293 |
G1 S8 '*' -- Internal lexical symbol for "'*'" |
| 1294 |
/* terminal */ |
| 1295 |
Internal name: <[Lex-7]> |
| 1296 |
SLIF name: '*' |
| 1297 |
G1 S9 '+' -- Internal lexical symbol for "'+'" |
| 1298 |
/* terminal */ |
| 1299 |
Internal name: <[Lex-8]> |
| 1300 |
SLIF name: '+' |
| 1301 |
G1 S10 expression -- <expression> at priority 0 |
| 1302 |
Internal name: <expression[0]> |
| 1303 |
SLIF name: expression |
| 1304 |
G1 S11 expression -- <expression> at priority 1 |
| 1305 |
Internal name: <expression[1]> |
| 1306 |
SLIF name: expression |
| 1307 |
G1 S12 expression -- <expression> at priority 2 |
| 1308 |
Internal name: <expression[2]> |
| 1309 |
SLIF name: expression |
| 1310 |
G1 S13 <numeric expression> -- <numeric expression> at priority 0 |
| 1311 |
Internal name: <numeric expression[0]> |
| 1312 |
SLIF name: numeric expression |
| 1313 |
G1 S14 <numeric expression> -- <numeric expression> at priority 1 |
| 1314 |
Internal name: <numeric expression[1]> |
| 1315 |
SLIF name: numeric expression |
| 1316 |
G1 S15 <numeric expression> -- <numeric expression> at priority 2 |
| 1317 |
Internal name: <numeric expression[2]> |
| 1318 |
SLIF name: numeric expression |
| 1319 |
G1 S16 statements |
| 1320 |
Internal name: <statements> |
| 1321 |
G1 S17 statement |
| 1322 |
Internal name: <statement> |
| 1323 |
G1 S18 assignment |
| 1324 |
Internal name: <assignment> |
| 1325 |
G1 S19 <numeric assignment> |
| 1326 |
Internal name: <numeric assignment> |
| 1327 |
G1 S20 variable |
| 1328 |
/* terminal */ |
| 1329 |
Internal name: <variable> |
| 1330 |
G1 S21 expression |
| 1331 |
Internal name: <expression> |
| 1332 |
G1 S22 <numeric expression> |
| 1333 |
Internal name: <numeric expression> |
| 1334 |
G1 S23 string |
| 1335 |
/* terminal */ |
| 1336 |
Internal name: <string> |
| 1337 |
G1 S24 number |
| 1338 |
/* terminal */ |
| 1339 |
Internal name: <number> |
| 1340 |
|
| 1341 |
=for Marpa::R2::Display::End |
| 1342 |
|
| 1343 |
=head2 progress() outputs |
| 1344 |
|
| 1345 |
These section contains samples of |
| 1346 |
the output of the C<progress()> |
| 1347 |
method -- the progress reports in their "raw" format. |
| 1348 |
The output is shown in L<Data::Dumper> format, |
| 1349 |
with C<Data::Dumper::Indent> set to 0 |
| 1350 |
and C<Data::Dumper::Terse> set to 1. |
| 1351 |
|
| 1352 |
The C<Data::Dumper> output from L<C<progress()>|/"progress()"> at G1 location 0: |
| 1353 |
|
| 1354 |
=for Marpa::R2::Display |
| 1355 |
name: SLIF progress() output at location 0 |
| 1356 |
remove-display-indent: 1 |
| 1357 |
remove-blank-last-line: 1 |
| 1358 |
|
| 1359 |
[[0,0,0],[1,0,0],[2,0,0],[3,0,0],[4,0,0],[19,0,0]] |
| 1360 |
|
| 1361 |
=for Marpa::R2::Display::End |
| 1362 |
|
| 1363 |
The C<Data::Dumper> output from L<C<progress()>|/"progress()"> at G1 location 1: |
| 1364 |
|
| 1365 |
=for Marpa::R2::Display |
| 1366 |
name: SLIF progress() output at location 1 |
| 1367 |
remove-display-indent: 1 |
| 1368 |
remove-blank-last-line: 1 |
| 1369 |
|
| 1370 |
[[4,1,0]] |
| 1371 |
|
| 1372 |
=for Marpa::R2::Display::End |
| 1373 |
|
| 1374 |
The C<Data::Dumper> output from L<C<progress()>|/"progress()"> at location 2: |
| 1375 |
|
| 1376 |
=for Marpa::R2::Display |
| 1377 |
name: SLIF progress() output at location 2 |
| 1378 |
remove-display-indent: 1 |
| 1379 |
remove-blank-last-line: 1 |
| 1380 |
|
| 1381 |
[[5,0,2],[6,0,2],[7,0,2],[8,0,2],[9,0,2],[10,0,2],[11,0,2],[4,2,0]] |
| 1382 |
|
| 1383 |
=for Marpa::R2::Display::End |
| 1384 |
|
| 1385 |
The default L<C<progress()>|/"progress()"> output is for the latest Earley set. |
| 1386 |
Here is the L<C<progress()>|/"progress()"> output for the latest Earley set. |
| 1387 |
|
| 1388 |
=for Marpa::R2::Display |
| 1389 |
name: SLIF progress() output at default location |
| 1390 |
remove-display-indent: 1 |
| 1391 |
remove-blank-last-line: 1 |
| 1392 |
|
| 1393 |
[[0,-1,0],[2,-1,0],[4,-1,0],[5,-1,2],[7,-1,4],[8,-1,4],[11,-1,2],[19,-1,0],[1,0,5],[2,0,5],[3,0,5],[4,0,5],[11,1,2]] |
| 1394 |
|
| 1395 |
=for Marpa::R2::Display::End |
| 1396 |
|
| 1397 |
=head1 Copyright and License |
| 1398 |
|
| 1399 |
=for Marpa::R2::Display |
| 1400 |
ignore: 1 |
| 1401 |
|
| 1402 |
Copyright 2022 Jeffrey Kegler |
| 1403 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 1404 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 1405 |
General Public License as published by the Free Software Foundation, |
| 1406 |
either version 3 of the License, or (at your option) any later version. |
| 1407 |
|
| 1408 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 1409 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1410 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 1411 |
Lesser General Public License for more details. |
| 1412 |
|
| 1413 |
You should have received a copy of the GNU Lesser |
| 1414 |
General Public License along with Marpa::R2. If not, see |
| 1415 |
http://www.gnu.org/licenses/. |
| 1416 |
|
| 1417 |
=for Marpa::R2::Display::End |
| 1418 |
|
| 1419 |
=cut |
| 1420 |
|
| 1421 |
# Local Variables: |
| 1422 |
# mode: cperl |
| 1423 |
# cperl-indent-level: 4 |
| 1424 |
# fill-column: 100 |
| 1425 |
# End: |
| 1426 |
# vim: expandtab shiftwidth=4: |