| 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::Semantics::Rank - How ranks are computed |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
=for Marpa::R2::Display |
| 23 |
name: Rank document synopsis |
| 24 |
normalize-whitespace: 1 |
| 25 |
|
| 26 |
my $source = <<'END_OF_SOURCE'; |
| 27 |
:discard ~ ws; ws ~ [\s]+ |
| 28 |
:default ::= action => ::array |
| 29 |
|
| 30 |
Top ::= List action => main::group |
| 31 |
List ::= Item3 rank => 3 |
| 32 |
List ::= Item2 rank => 2 |
| 33 |
List ::= Item1 rank => 1 |
| 34 |
List ::= List Item3 rank => 3 |
| 35 |
List ::= List Item2 rank => 2 |
| 36 |
List ::= List Item1 rank => 1 |
| 37 |
Item3 ::= VAR '=' VAR action => main::concat |
| 38 |
Item2 ::= VAR '=' action => main::concat |
| 39 |
Item1 ::= VAR action => main::concat |
| 40 |
VAR ~ [\w]+ |
| 41 |
|
| 42 |
END_OF_SOURCE |
| 43 |
|
| 44 |
my @tests = ( |
| 45 |
[ 'a', '(a)', ], |
| 46 |
[ 'a = b', '(a=b)', ], |
| 47 |
[ 'a = b = c', '(a=)(b=c)', ], |
| 48 |
[ 'a = b = c = d', '(a=)(b=)(c=d)', ], |
| 49 |
[ 'a = b c = d', '(a=b)(c=d)' ], |
| 50 |
[ 'a = b c = d e =', '(a=b)(c=d)(e=)' ], |
| 51 |
[ 'a = b c = d e', '(a=b)(c=d)(e)' ], |
| 52 |
[ 'a = b c = d e = f', '(a=b)(c=d)(e=f)' ], |
| 53 |
); |
| 54 |
|
| 55 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$source } ); |
| 56 |
for my $test (@tests) { |
| 57 |
my ( $input, $output ) = @{$test}; |
| 58 |
my $recce = Marpa::R2::Scanless::R->new( |
| 59 |
{ |
| 60 |
grammar => $grammar, |
| 61 |
ranking_method => 'high_rule_only' |
| 62 |
} |
| 63 |
); |
| 64 |
$recce->read( \$input ); |
| 65 |
my $value_ref = $recce->value(); |
| 66 |
if ( not defined $value_ref ) { |
| 67 |
die 'No parse'; |
| 68 |
} |
| 69 |
push @results, ${$value_ref}; |
| 70 |
} |
| 71 |
|
| 72 |
=for Marpa::R2::Display::End |
| 73 |
|
| 74 |
=for Marpa::R2::Display |
| 75 |
name: rank example semantics |
| 76 |
normalize-whitespace: 1 |
| 77 |
|
| 78 |
sub flatten { |
| 79 |
my ($array) = @_; |
| 80 |
|
| 81 |
# say STDERR 'flatten arg: ', Data::Dumper::Dumper($array); |
| 82 |
my $ref = ref $array; |
| 83 |
return [$array] if $ref ne 'ARRAY'; |
| 84 |
my @flat = (); |
| 85 |
ELEMENT: for my $element ( @{$array} ) { |
| 86 |
my $ref = ref $element; |
| 87 |
if ( $ref ne 'ARRAY' ) { |
| 88 |
push @flat, $element; |
| 89 |
next ELEMENT; |
| 90 |
} |
| 91 |
my $flat_piece = flatten($element); |
| 92 |
push @flat, @{$flat_piece}; |
| 93 |
} |
| 94 |
return \@flat; |
| 95 |
} |
| 96 |
|
| 97 |
sub concat { |
| 98 |
my ( $pp, @args ) = @_; |
| 99 |
|
| 100 |
# say STDERR 'concat: ', Data::Dumper::Dumper(\@args); |
| 101 |
my $flat = flatten( \@args ); |
| 102 |
return join '', @{$flat}; |
| 103 |
} |
| 104 |
|
| 105 |
sub group { |
| 106 |
my ( $pp, @args ) = @_; |
| 107 |
|
| 108 |
# say STDERR 'comma_sep args: ', Data::Dumper::Dumper(\@args); |
| 109 |
my $flat = flatten( \@args ); |
| 110 |
return join '', map { +'(' . $_ . ')'; } @{$flat}; |
| 111 |
} |
| 112 |
|
| 113 |
=for Marpa::R2::Display::End |
| 114 |
|
| 115 |
=head1 Description |
| 116 |
|
| 117 |
This document describes rule ranking. |
| 118 |
Rule ranking plays a role in parse ordering, |
| 119 |
which is described in L<a separate |
| 120 |
document|Marpa::R2::Semantics::Order>. |
| 121 |
|
| 122 |
=head1 Overview |
| 123 |
|
| 124 |
Rule ranking takes place at the nodes of the parse forest. |
| 125 |
In the context of rule ranking, |
| 126 |
the parse forest nodes are called B<choicepoints>. |
| 127 |
Every choicepoint has one or more B<choices>. |
| 128 |
Choices that are from the same choicepoint |
| 129 |
are called B<siblings>. |
| 130 |
|
| 131 |
Choices are ordered by their B<choice key>. |
| 132 |
The choice key consists of two subkeys: |
| 133 |
the B<symbolic subkey> and the B<null-variant subkey>. |
| 134 |
The symbolic subkey is the major subkey, |
| 135 |
and the null-variant subkey is the minor subkey, |
| 136 |
so that the order is by null-variant within symbolic rank. |
| 137 |
The symbolic subkey is so called because it is based |
| 138 |
on the symbols of the choice. |
| 139 |
The null-variant subkey is based on the location of the |
| 140 |
choice's nulled symbol instances. |
| 141 |
Choicepoints, choices and subkeys are described in much |
| 142 |
more detail below. |
| 143 |
|
| 144 |
=head1 Ranking methods |
| 145 |
|
| 146 |
SLIF recognizer objects have a L<C<ranking_method> named |
| 147 |
argument|Marpa::R2::Scanless::R/"ranking_method">, |
| 148 |
whose value can be the name of a ranking method, |
| 149 |
or "C<none>", indicating that the default ranking method is to |
| 150 |
be used. |
| 151 |
|
| 152 |
=head2 The C<rule> ranking method |
| 153 |
|
| 154 |
The C<rule> ranking method ranks choices according to their choice keys. |
| 155 |
All choices are included in the ordering. |
| 156 |
|
| 157 |
=head2 The C<high_rule_only> ranking method |
| 158 |
|
| 159 |
The C<high_rule_only> ranking method is similar to the |
| 160 |
C<rule> ranking method, except that, at every choicepoint, |
| 161 |
it discards all of the choices which |
| 162 |
have a rank lower than that of the highest ranked choice. |
| 163 |
The C<high_rule_only> ranking method |
| 164 |
can reduce the ambiguity of a parse, |
| 165 |
but it does not necessarily do so. |
| 166 |
This is because a choice with the highest choice key |
| 167 |
can have one or more siblings |
| 168 |
with the same choice key. |
| 169 |
|
| 170 |
=head1 The symbolic subkey |
| 171 |
|
| 172 |
Every rule alternative has a numeric B<symbolic rank>. |
| 173 |
A rule's rank can be specified using the |
| 174 |
the C<rank> adverb |
| 175 |
argument for that RHS alternative. |
| 176 |
Rule ranks must be integers. |
| 177 |
They may be negative. |
| 178 |
If no numeric rank is specified, the numeric rank is 0. |
| 179 |
|
| 180 |
Rule alternatives may be part of a single rule in the DSL -- |
| 181 |
for example, a |
| 182 |
L<prioritized rule|Marpa::R2::Scanless::DSL/"Prioritized rule">. |
| 183 |
Lexical order within a DSL rule |
| 184 |
makes no difference when ranking rule alternatives. |
| 185 |
For example, it makes no difference if two rule alternatives |
| 186 |
come from the same prioritized rule; |
| 187 |
or from two different prioritized rules. |
| 188 |
|
| 189 |
=head1 The null-variant subkey |
| 190 |
|
| 191 |
Some rules have a RHS which contains |
| 192 |
B<proper nullables>: |
| 193 |
symbols |
| 194 |
which may be nulled, but which are not nulling |
| 195 |
symbols. |
| 196 |
(Nulling symbols are symbols which are B<always> nulled.) |
| 197 |
|
| 198 |
When a rule alternative contains proper nullables, |
| 199 |
each instance |
| 200 |
of that rule creates a B<nulling variant>. |
| 201 |
A B<nulling variant> is |
| 202 |
a specific pattern of |
| 203 |
null and non-null symbols in a rule instance's RHS. |
| 204 |
In many cases, this creates an ambiguity -- different |
| 205 |
nulling variants can match the same substring in the input. |
| 206 |
In ambiguous parsings of this kind, |
| 207 |
some applications may want to rank nulling variants that start |
| 208 |
with non-null symbols higher. |
| 209 |
Other applications may want to do the opposite -- |
| 210 |
to rank nulling variants that start |
| 211 |
with null symbols higher. |
| 212 |
|
| 213 |
The |
| 214 |
L<C<null-ranking> adverb |
| 215 |
for RHS alternatives|Marpa::R2::Scanless::DSL/"null-ranking"> |
| 216 |
specifies which nulling variants are ranked high or low. |
| 217 |
If the C<null-ranking> is "C<low>", |
| 218 |
then the closer a nulling variant |
| 219 |
places its B<visible> (non-null) symbols to the start of the rule instance, |
| 220 |
the higher it ranks. |
| 221 |
A null ranking of C<low> is the default. |
| 222 |
If the C<null-ranking> is "C<high>", |
| 223 |
then the closer a nulling variant |
| 224 |
places its B<null> symbols to the start of the rule instance, |
| 225 |
the higher it ranks. |
| 226 |
In ranking nulling variants with more than one proper nullable, |
| 227 |
major-to-minor is left-to-right. |
| 228 |
|
| 229 |
=head1 Choicepoints |
| 230 |
|
| 231 |
Ranking is done at the or-nodes of our parse forest, |
| 232 |
We also call our parse forest a B<bocage>. |
| 233 |
In the context of ranking, we usually refer to the |
| 234 |
or-nodes as B<choicepoints>. |
| 235 |
|
| 236 |
An or-node is similar to |
| 237 |
an Earley item: |
| 238 |
it has a dotted rule, an origin, |
| 239 |
a current location, |
| 240 |
and a set of confluences. |
| 241 |
The confluences, in turn, each can have |
| 242 |
a mainstem and a tributary. |
| 243 |
To refresh our knowledge about Earley items and confluences, |
| 244 |
we can refer to L<our document on the Marpa |
| 245 |
algorithm|Marpa::R2::Algorithm>. |
| 246 |
|
| 247 |
A confluence is a reason for the choicepoint to be in the parse |
| 248 |
forest. |
| 249 |
Every choicepoint has at least one reason to be |
| 250 |
in the parse forest, or it would not be there. |
| 251 |
Therefore every choicepoint has at least one confluence. |
| 252 |
In an ambiguous parse, one or more choicepoints will |
| 253 |
have more than one confluence. |
| 254 |
A parse tree is created by selecting a single |
| 255 |
confluence (or reason to exist) at each choicepoint. |
| 256 |
|
| 257 |
In the context of ranking and choicepoints, |
| 258 |
confluences are more often called B<choices>. |
| 259 |
If there is only one choice at a choicepoint, |
| 260 |
we say that the choicepoint is a B<singleton choicepoint>. |
| 261 |
If a choicepoint is not a singleton, |
| 262 |
we say that it is an B<ambiguous choicepoint>. |
| 263 |
|
| 264 |
Choices with the same choice key are ranked arbitrarily. |
| 265 |
If all the choices of a choicepoint have the same |
| 266 |
choice key, |
| 267 |
we say that that the choicepoint is B<trivial>. |
| 268 |
A choicepoint is B<non-trivial> if and only if it is not |
| 269 |
trivial. |
| 270 |
|
| 271 |
Every singleton choicepoint is trivial, |
| 272 |
but some trivial choicepoints are not singletons. |
| 273 |
This is because sibling choices can share the same choice key. |
| 274 |
|
| 275 |
Within a non-trivial choicepoint, |
| 276 |
the mainstems and the LHS of the rules in tributaries |
| 277 |
must always be the same. |
| 278 |
(For the explanation of why this is the case, |
| 279 |
see L</"Explanations">.) |
| 280 |
Therefore, when the symbolic subkeys of two |
| 281 |
choices within the same non-trivial choicepoint differ, |
| 282 |
they will differ only in the RHS of the rules |
| 283 |
of their tributaries. |
| 284 |
|
| 285 |
The B<predot symbol> of a choicepoint is the |
| 286 |
predot symbol of its dotted rule. |
| 287 |
The LHS of the tributary rules of a choicepoint's |
| 288 |
choices is always the same as |
| 289 |
the predot symbol of that choicepoint. |
| 290 |
|
| 291 |
Iteration of the parse forest is depth-first, left-to-right. |
| 292 |
An detailed explanation of the parse forest interation |
| 293 |
is in L</"Explanations">. |
| 294 |
|
| 295 |
=head1 Examples |
| 296 |
|
| 297 |
Our examples in this document will look at |
| 298 |
the ranked grammar in the synopsis, |
| 299 |
and at variations of it. |
| 300 |
|
| 301 |
=head2 Longest highest, version 1 |
| 302 |
|
| 303 |
The DSL in the synopsis ranks its items |
| 304 |
"longest highest". |
| 305 |
Here "items" are represented by the symbols, |
| 306 |
C<< <Item3> >>, |
| 307 |
C<< <Item2> >> and |
| 308 |
C<< <Item1> >>. |
| 309 |
The "longest" choice is considered to be the one |
| 310 |
with the most lexemes. |
| 311 |
Working this idea out for this grammar, |
| 312 |
we see that the items |
| 313 |
should rank, |
| 314 |
from highest to lowest: |
| 315 |
C<< <Item3> >>, |
| 316 |
C<< <Item2> >> and |
| 317 |
C<< <Item1> >>. |
| 318 |
|
| 319 |
=for Marpa::R2::Display |
| 320 |
name: Rank document synopsis |
| 321 |
normalize-whitespace: 1 |
| 322 |
partial: 1 |
| 323 |
|
| 324 |
:discard ~ ws; ws ~ [\s]+ |
| 325 |
:default ::= action => ::array |
| 326 |
|
| 327 |
Top ::= List action => main::group |
| 328 |
List ::= Item3 rank => 3 |
| 329 |
List ::= Item2 rank => 2 |
| 330 |
List ::= Item1 rank => 1 |
| 331 |
List ::= List Item3 rank => 3 |
| 332 |
List ::= List Item2 rank => 2 |
| 333 |
List ::= List Item1 rank => 1 |
| 334 |
Item3 ::= VAR '=' VAR action => main::concat |
| 335 |
Item2 ::= VAR '=' action => main::concat |
| 336 |
Item1 ::= VAR action => main::concat |
| 337 |
VAR ~ [\w]+ |
| 338 |
|
| 339 |
=for Marpa::R2::Display::End |
| 340 |
|
| 341 |
To see how ranking works in this grammar, |
| 342 |
we note that, for a choicepoint to be non-trivial, |
| 343 |
it must have a predot symbol that is the LHS of more |
| 344 |
than one rule. |
| 345 |
Therefore, in the above grammar, |
| 346 |
a choicepoint must |
| 347 |
have one of these dotted rules: |
| 348 |
|
| 349 |
=for Marpa::R2::Display |
| 350 |
ignore: 1 |
| 351 |
|
| 352 |
Top ::= List . |
| 353 |
List ::= List . Item3 |
| 354 |
List ::= List . Item2 |
| 355 |
List ::= List . Item1 |
| 356 |
|
| 357 |
=for Marpa::R2::Display::End |
| 358 |
|
| 359 |
The tributary of these choicepoints |
| 360 |
may be any one of |
| 361 |
|
| 362 |
=for Marpa::R2::Display |
| 363 |
ignore: 1 |
| 364 |
|
| 365 |
List ::= Item3 . |
| 366 |
List ::= Item2 . |
| 367 |
List ::= Item1 . |
| 368 |
List ::= List Item3 . |
| 369 |
List ::= List Item2 . |
| 370 |
List ::= List Item1 . |
| 371 |
|
| 372 |
=for Marpa::R2::Display::End |
| 373 |
|
| 374 |
A little effort shows that C<< <Item2> >> never |
| 375 |
participates in an ambiguity, |
| 376 |
so that these are the possible tributaries |
| 377 |
in non-trivial choicepoints: |
| 378 |
|
| 379 |
=for Marpa::R2::Display |
| 380 |
ignore: 1 |
| 381 |
|
| 382 |
List ::= Item1 . |
| 383 |
List ::= List Item1 . |
| 384 |
List ::= Item3 . |
| 385 |
List ::= List Item3 . |
| 386 |
|
| 387 |
=for Marpa::R2::Display::End |
| 388 |
|
| 389 |
=head2 Shortest highest, version 1 |
| 390 |
|
| 391 |
Here we see the grammar of the synopsis, |
| 392 |
reworked for a "shortest highest" |
| 393 |
ranking. |
| 394 |
"Shortest highest" is the reverse of |
| 395 |
"longest highest". |
| 396 |
|
| 397 |
=for Marpa::R2::Display |
| 398 |
name: Ranking, shortest highest, version 1 |
| 399 |
|
| 400 |
:discard ~ ws; ws ~ [\s]+ |
| 401 |
:default ::= action => ::array |
| 402 |
|
| 403 |
Top ::= List action => main::group |
| 404 |
List ::= Item3 rank => 1 |
| 405 |
List ::= Item2 rank => 2 |
| 406 |
List ::= Item1 rank => 3 |
| 407 |
List ::= List Item3 rank => 1 |
| 408 |
List ::= List Item2 rank => 2 |
| 409 |
List ::= List Item1 rank => 3 |
| 410 |
Item3 ::= VAR '=' VAR action => main::concat |
| 411 |
Item2 ::= VAR '=' action => main::concat |
| 412 |
Item1 ::= VAR action => main::concat |
| 413 |
VAR ~ [\w]+ |
| 414 |
|
| 415 |
=for Marpa::R2::Display::End |
| 416 |
|
| 417 |
Here are what the results will look like for |
| 418 |
"shortest highest". |
| 419 |
|
| 420 |
=for Marpa::R2::Display |
| 421 |
name: Ranking results, shortest highest, version 1 |
| 422 |
|
| 423 |
my @tests = ( |
| 424 |
[ 'a', '(a)', ], |
| 425 |
[ 'a = b', '(a=)(b)', ], |
| 426 |
[ 'a = b = c', '(a=)(b=)(c)', ], |
| 427 |
[ 'a = b = c = d', '(a=)(b=)(c=)(d)', ], |
| 428 |
[ 'a = b c = d', '(a=)(b)(c=)(d)' ], |
| 429 |
[ 'a = b c = d e =', '(a=)(b)(c=)(d)(e=)' ], |
| 430 |
[ 'a = b c = d e', '(a=)(b)(c=)(d)(e)' ], |
| 431 |
[ 'a = b c = d e = f', '(a=)(b)(c=)(d)(e=)(f)' ], |
| 432 |
); |
| 433 |
|
| 434 |
=for Marpa::R2::Display::End |
| 435 |
|
| 436 |
=head2 Longest highest, version 2 |
| 437 |
|
| 438 |
The previous examples have shown the rule involved |
| 439 |
in parse ranking in "spelled out" form. |
| 440 |
In fact, a more compact form of the grammar can be used, |
| 441 |
as shown below for |
| 442 |
"longest highest" ranking. |
| 443 |
|
| 444 |
=for Marpa::R2::Display |
| 445 |
name: Ranking, longest highest, version 2 |
| 446 |
|
| 447 |
:discard ~ ws; ws ~ [\s]+ |
| 448 |
:default ::= action => ::array |
| 449 |
|
| 450 |
Top ::= List action => main::group |
| 451 |
List ::= Item rank => 1 |
| 452 |
List ::= List Item rank => 0 |
| 453 |
Item ::= VAR '=' VAR rank => 3 action => main::concat |
| 454 |
Item ::= VAR '=' rank => 2 action => main::concat |
| 455 |
Item ::= VAR rank => 1 action => main::concat |
| 456 |
VAR ~ [\w]+ |
| 457 |
|
| 458 |
=for Marpa::R2::Display::End |
| 459 |
|
| 460 |
=head2 Shortest highest, version 2 |
| 461 |
|
| 462 |
This is the grammar for "shortest highest", |
| 463 |
in compact form: |
| 464 |
|
| 465 |
=for Marpa::R2::Display |
| 466 |
name: Ranking, shortest highest, version 2 |
| 467 |
|
| 468 |
:discard ~ ws; ws ~ [\s]+ |
| 469 |
:default ::= action => ::array |
| 470 |
|
| 471 |
Top ::= List action => main::group |
| 472 |
List ::= Item rank => 0 |
| 473 |
List ::= List Item rank => 1 |
| 474 |
Item ::= VAR '=' VAR rank => 1 action => main::concat |
| 475 |
Item ::= VAR '=' rank => 2 action => main::concat |
| 476 |
Item ::= VAR rank => 3 action => main::concat |
| 477 |
VAR ~ [\w]+ |
| 478 |
|
| 479 |
=for Marpa::R2::Display::End |
| 480 |
|
| 481 |
=head1 Reimplementing ranking as pure BNF |
| 482 |
|
| 483 |
It is generally better |
| 484 |
to write a grammar as "pure BNF", instead of using ranking. |
| 485 |
The advantage of using pure BNF is that you can more readily determine |
| 486 |
exactly what language it is that you are parsing: |
| 487 |
Ranked grammars make look easier to analyze at first glance, |
| 488 |
but the more you look at them the more tricky you |
| 489 |
realize they are. |
| 490 |
|
| 491 |
The pure BNF reimplementations below rely on an observation: |
| 492 |
The parse string becomes easier to analyze |
| 493 |
when we think in terms of fenceposts, |
| 494 |
rather than in term of the location of the lexemes. |
| 495 |
Fenceposts are either initial, final or medial. |
| 496 |
The B<initial fencepost> is the position before the first lexeme. |
| 497 |
The B<final fencepost> is the position after the last lexeme. |
| 498 |
A B<medial fencepost> is the position between two lexemes. |
| 499 |
We can call a fencepost a B<VAR-bound> |
| 500 |
if it is a either an initial fencepost, |
| 501 |
a final fencepost, |
| 502 |
or a medial fencepost |
| 503 |
that occurs between two C<< <VAR> >> lexemes. |
| 504 |
We can then visualize the input string |
| 505 |
as a sequence of "VAR-bounded" |
| 506 |
substrings. |
| 507 |
|
| 508 |
=head3 Longest highest as pure BNF |
| 509 |
|
| 510 |
Here is the "longest highest" example, |
| 511 |
reimplemented as BNF: |
| 512 |
|
| 513 |
=for Marpa::R2::Display |
| 514 |
name: Ranking via BNF, longest highest, version 3 |
| 515 |
normalize-whitespace: 1 |
| 516 |
|
| 517 |
:discard ~ ws; ws ~ [\s]+ |
| 518 |
:default ::= action => ::array |
| 519 |
|
| 520 |
Top ::= Max_Boundeds action => main::group |
| 521 |
Top ::= Max_Boundeds Unbounded action => main::group |
| 522 |
Top ::= Unbounded action => main::group |
| 523 |
Max_Boundeds ::= Max_Bounded+ |
| 524 |
Max_Bounded ::= Eq_Finals Var_Final3 |
| 525 |
Max_Bounded ::= Var_Final |
| 526 |
Unbounded ::= Eq_Finals |
| 527 |
Eq_Finals ::= Eq_Final+ |
| 528 |
Var_Final ::= Var_Final3 | Var_Final1 |
| 529 |
Var_Final3 ::= VAR '=' VAR action => main::concat |
| 530 |
Eq_Final ::= VAR '=' action => main::concat |
| 531 |
Var_Final1 ::= VAR action => main::concat |
| 532 |
VAR ~ [\w]+ |
| 533 |
|
| 534 |
=for Marpa::R2::Display::End |
| 535 |
|
| 536 |
=head3 Shortest highest as pure BNF |
| 537 |
|
| 538 |
We can also reimplement the "shortest highest" |
| 539 |
example as BNF. |
| 540 |
One of the advantages of a BNF (re)implementation, |
| 541 |
is that it often clarifies the grammar. |
| 542 |
For example |
| 543 |
in this case, we note that |
| 544 |
the DSL rule |
| 545 |
|
| 546 |
=for Marpa::R2::Display |
| 547 |
name: Ranking via BNF, longest highest, version 3 |
| 548 |
normalize-whitespace: 1 |
| 549 |
partial: 1 |
| 550 |
|
| 551 |
Var_Final3 ::= VAR '=' VAR action => main::concat |
| 552 |
|
| 553 |
=for Marpa::R2::Display::End |
| 554 |
|
| 555 |
is, in fact, never used. |
| 556 |
We therefore omit it: |
| 557 |
|
| 558 |
=for Marpa::R2::Display |
| 559 |
name: Ranking via BNF, shortest highest, version 3 |
| 560 |
normalize-whitespace: 1 |
| 561 |
|
| 562 |
:discard ~ ws; ws ~ [\s]+ |
| 563 |
:default ::= action => ::array |
| 564 |
|
| 565 |
Top ::= Max_Boundeds action => main::group |
| 566 |
Top ::= Max_Boundeds Unbounded action => main::group |
| 567 |
Top ::= Unbounded action => main::group |
| 568 |
Max_Boundeds ::= Max_Bounded+ |
| 569 |
Max_Bounded ::= Eq_Finals Var_Final |
| 570 |
Max_Bounded ::= Var_Final |
| 571 |
Unbounded ::= Eq_Finals |
| 572 |
Eq_Finals ::= Eq_Final+ |
| 573 |
Eq_Final ::= VAR '=' action => main::concat |
| 574 |
Var_Final ::= VAR action => main::concat |
| 575 |
VAR ~ [\w]+ |
| 576 |
|
| 577 |
=for Marpa::R2::Display::End |
| 578 |
|
| 579 |
=head1 Explanations |
| 580 |
|
| 581 |
=head2 Bocages |
| 582 |
|
| 583 |
When ranking, the logic traverses each node |
| 584 |
of a parse forest, which we call a "bocage". |
| 585 |
Our parse forests closely |
| 586 |
resemble |
| 587 |
Elizabeth Scott's SPPF's. |
| 588 |
See L<Marpa::R2::Advanced::Bibliography/"Scott 2008">. |
| 589 |
|
| 590 |
=head2 Only confirmations are choicepoints |
| 591 |
|
| 592 |
An Earley item (and a choicepoint) is a confirmation |
| 593 |
if and only if its dot position is not zero. |
| 594 |
Confirmations are either scanned Earley items or reductions. |
| 595 |
Non-confirmations (predictions and the start Earley item) |
| 596 |
are redundant information |
| 597 |
from the point of of the bocage. |
| 598 |
No or-nodes are created for non-confirmations |
| 599 |
and they can never be choicepoints. |
| 600 |
|
| 601 |
Since choicepoints are always either scanned Earley items or |
| 602 |
reductions, |
| 603 |
all the choices of every choicepoint have a well-defined mainstem |
| 604 |
and a well-defined tributary. |
| 605 |
|
| 606 |
=head2 Choicepoint mainstem is unique |
| 607 |
|
| 608 |
Since every choicepoint has exactly one dotted rule, |
| 609 |
exactly one current location, |
| 610 |
and exactly one origin, |
| 611 |
every choicepoint has exactly one mainstem. |
| 612 |
All the choices for that choicepoint share that mainstem. |
| 613 |
Because every choicepoint |
| 614 |
has exactly one mainstem, |
| 615 |
every choicepoint has exactly one predot symbol. |
| 616 |
|
| 617 |
=head2 Scanned choicepoints are always trivial |
| 618 |
|
| 619 |
The tributary of a scanned choicepoint is a token, |
| 620 |
which has a value and a token symbol. |
| 621 |
The token value plays no role in ranking. |
| 622 |
The token symbol is always the same as the predot symbol. |
| 623 |
|
| 624 |
The token symbol can be assigned a rank, |
| 625 |
but this is pointless: |
| 626 |
Since there is only one predot symbol |
| 627 |
in a choicepoint, |
| 628 |
all token symbols will be equal to the predot symbol, |
| 629 |
and therefore all token symbols of sibling choices will be the same. |
| 630 |
|
| 631 |
Because every symbol in a choice of a scanned choicepoint |
| 632 |
is the same, the symbolic subkeys of all the sibling choices |
| 633 |
of a scanned choicepoint must be identical to each other. |
| 634 |
Since there is no BNF rule for the tributary of a scanned choicepoint, |
| 635 |
the choices of a scanned choicepoint also have no null-variants. |
| 636 |
This means that the choice keys of the sibling choices in |
| 637 |
a scanned choicepoint are always identical to each other. |
| 638 |
Therefore all choices of scanned choicepoints rank the same, |
| 639 |
and all scanned choicepoints are trivial. |
| 640 |
|
| 641 |
While a scanned choicepoint is never non-trivial, |
| 642 |
a scanned choicepoint may be ambiguous if variable length tokens are in use. |
| 643 |
Usually variable length tokens are not in use, |
| 644 |
and all scanned choicepoint are both trivial and singletons. |
| 645 |
|
| 646 |
=head2 All non-trivial choicepoints are reductions |
| 647 |
|
| 648 |
Since every scanned choicepoint must be trivial, |
| 649 |
every non-trivial choicepoint will be a reduction. |
| 650 |
If a choicepoint is a reduction, |
| 651 |
all of its tributaries will be other choicepoints, |
| 652 |
and each tributary will have a dotted rule associated with it. |
| 653 |
The dotted rules of these tributaries |
| 654 |
will always be completions. |
| 655 |
The LHS of the rule of these tributaries |
| 656 |
will always be the predot symbol of the choicepoint. |
| 657 |
This means that the rules of the tributaries of |
| 658 |
the sibling choices in a reduction choicepoint |
| 659 |
must share the same LHS. |
| 660 |
Rules of the tributaries of choices in a reduction choicepoint |
| 661 |
can have different RHS's, however, |
| 662 |
and therefore can differ in symbolic rank. |
| 663 |
|
| 664 |
=head2 Ambiguous choicepoints |
| 665 |
|
| 666 |
All non-trival choicepoints are ambiguous, |
| 667 |
but some trivial choicepoints are also ambiguous. |
| 668 |
This is because choices at choicepoints may differ not just symbolically, |
| 669 |
but in the way in which the symbols divide up the input |
| 670 |
string -- the way in which they "factor" the input. |
| 671 |
Choices which have the same symbols, |
| 672 |
but which are factored differently, |
| 673 |
will have the same rank. |
| 674 |
For more about symbolic choices (also called B<symches>) |
| 675 |
and factorings, see |
| 676 |
L<Marpa::R2::Glade/"Ambiguity: factoring versus symches">. |
| 677 |
|
| 678 |
=head2 Iterating the parses |
| 679 |
|
| 680 |
Every node of a parse tree corresponds to a choice |
| 681 |
from a choicepoint of the parse forest. |
| 682 |
An B<initial parse subtree> is the subtree formed from |
| 683 |
an choicepoint-rooted subforest by taking all the first choices of its choicepoints. |
| 684 |
|
| 685 |
The B<initial parse tree> is the initial parse subtree formed |
| 686 |
from the subforest whose root in the root of the forest. |
| 687 |
In other words, the initial parse tree is the tree formed |
| 688 |
by taking the first choices of the entire forest. |
| 689 |
|
| 690 |
The first parse tree in the iterator of a parse forest |
| 691 |
is the initial parse tree. |
| 692 |
|
| 693 |
For the second and later parse trees, the next parse tree |
| 694 |
is found by traversing the current parse tree from the |
| 695 |
bottom up, left to right. |
| 696 |
Every node of the parse tree will correspond to a choicepoint |
| 697 |
of the parse forest, and a choice within that choicepoint. |
| 698 |
If the current choice of its choicepoint is the last choice |
| 699 |
of that choicepoint, |
| 700 |
we say that that node of the parse tree is B<exhausted>. |
| 701 |
If a parse tree node is not B<exhausted>, |
| 702 |
we say that is B<active>. |
| 703 |
|
| 704 |
As the traversal of the parse tree encounters |
| 705 |
exhausted nodes, it prunes them from the tree. |
| 706 |
The traversal ends when it encounters an active parse |
| 707 |
tree node. |
| 708 |
We call that active parse tree the B<iteration parse tree node>. |
| 709 |
|
| 710 |
Once the iteration parse tree node has been found, |
| 711 |
it is replaced with a new tree node |
| 712 |
which corresponds to the next choice of the choicepoint |
| 713 |
of the iteration parse tree node. |
| 714 |
|
| 715 |
The remaining tree will have missing subtrees, |
| 716 |
due to the replacement |
| 717 |
of the iteration parse tree node, |
| 718 |
and due to the pruning of exhausted nodes. |
| 719 |
These subtrees are replaced with initial parse subtrees. |
| 720 |
|
| 721 |
When the entire parse tree is traversed without finding |
| 722 |
an active parse tree node, |
| 723 |
there are no more parse trees. |
| 724 |
In that case, |
| 725 |
the parse forest iterator is said to be B<exhausted>. |
| 726 |
|
| 727 |
=head1 Motivation |
| 728 |
|
| 729 |
We note that ranking is only by B<direct> tributaries. |
| 730 |
It might reasonably be asked, |
| 731 |
why not, at least in the case of a tie, |
| 732 |
look at tributaries of tributaries? |
| 733 |
Or why not resolve ties by looking at tributaries of |
| 734 |
mainstems? |
| 735 |
|
| 736 |
Marpa's built-in rule ranking |
| 737 |
was chosen as the most powerful system |
| 738 |
that could be implemented with effectively |
| 739 |
zero cost. |
| 740 |
Ranking by direct tributaries uses only information |
| 741 |
that is quickly and directly available, |
| 742 |
so that its runtime cost is probably |
| 743 |
not measurable. |
| 744 |
|
| 745 |
The complexity of the specification was also an issue. |
| 746 |
If indirect tributaries are taken into account, |
| 747 |
we would need to specify which tributaries, |
| 748 |
and under what circumstances they are used. |
| 749 |
Only tributaries of tributaries? |
| 750 |
Or tributaries of mainstems? |
| 751 |
Depth-first, or breadth-first? |
| 752 |
Only in case of ties, or using a more complex metric? |
| 753 |
To arbitrary depth, or using a traversal that is |
| 754 |
cut off at some point? |
| 755 |
|
| 756 |
If we do ranking by direct tributaries only, |
| 757 |
that makes the answers |
| 758 |
to the above questions as simple as it can be. |
| 759 |
When we try to analyze the behavior of grammars that use rankings, |
| 760 |
the importance of having a specification that is |
| 761 |
(relatively) simple becomes clear. |
| 762 |
|
| 763 |
There are apps |
| 764 |
whose requirements justify extra overhead and extra |
| 765 |
complexity. |
| 766 |
For these apps, |
| 767 |
L<Marpa's ASF's|Marpa::R2::ASF> |
| 768 |
allow full generality in ranking. |
| 769 |
|
| 770 |
=head1 Copyright and License |
| 771 |
|
| 772 |
=for Marpa::R2::Display |
| 773 |
ignore: 1 |
| 774 |
|
| 775 |
Copyright 2022 Jeffrey Kegler |
| 776 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 777 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 778 |
General Public License as published by the Free Software Foundation, |
| 779 |
either version 3 of the License, or (at your option) any later version. |
| 780 |
|
| 781 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 782 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 783 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 784 |
Lesser General Public License for more details. |
| 785 |
|
| 786 |
You should have received a copy of the GNU Lesser |
| 787 |
General Public License along with Marpa::R2. If not, see |
| 788 |
http://www.gnu.org/licenses/. |
| 789 |
|
| 790 |
=for Marpa::R2::Display::End |
| 791 |
|
| 792 |
=cut |
| 793 |
|
| 794 |
# vim: expandtab shiftwidth=4: |