| 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::ASF - Marpa's abstract syntax forests (ASF's) |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
We want to "diagram" the following sentence. |
| 23 |
|
| 24 |
=for Marpa::R2::Display |
| 25 |
name: ASF synopsis input |
| 26 |
normalize-whitespace: 1 |
| 27 |
|
| 28 |
my $sentence = 'a panda eats shoots and leaves.'; |
| 29 |
|
| 30 |
=for Marpa::R2::Display::End |
| 31 |
|
| 32 |
Here's the result we are looking for. It is in Penntag form: |
| 33 |
|
| 34 |
=for Marpa::R2::Display |
| 35 |
name: ASF synopsis output |
| 36 |
partial: 1 |
| 37 |
remove-display-indent: 1 |
| 38 |
remove-blank-last-line: 1 |
| 39 |
|
| 40 |
(S (NP (DT a) (NN panda)) |
| 41 |
(VP (VBZ eats) (NP (NNS shoots) (CC and) (NNS leaves))) |
| 42 |
(. .)) |
| 43 |
(S (NP (DT a) (NN panda)) |
| 44 |
(VP (VP (VBZ eats) (NP (NNS shoots))) (CC and) (VP (VBZ leaves))) |
| 45 |
(. .)) |
| 46 |
(S (NP (DT a) (NN panda)) |
| 47 |
(VP (VP (VBZ eats)) (VP (VBZ shoots)) (CC and) (VP (VBZ leaves))) |
| 48 |
(. .)) |
| 49 |
|
| 50 |
=for Marpa::R2::Display::End |
| 51 |
|
| 52 |
Here is the grammar. |
| 53 |
|
| 54 |
=for Marpa::R2::Display |
| 55 |
name: ASF synopsis grammar |
| 56 |
normalize-whitespace: 1 |
| 57 |
remove-display-indent: 1 |
| 58 |
remove-blank-last-line: 1 |
| 59 |
|
| 60 |
:default ::= action => [ values ] bless => ::lhs |
| 61 |
lexeme default = action => [ value ] bless => ::name |
| 62 |
|
| 63 |
S ::= NP VP period bless => S |
| 64 |
|
| 65 |
NP ::= NN bless => NP |
| 66 |
| NNS bless => NP |
| 67 |
| DT NN bless => NP |
| 68 |
| NN NNS bless => NP |
| 69 |
| NNS CC NNS bless => NP |
| 70 |
|
| 71 |
VP ::= VBZ NP bless => VP |
| 72 |
| VP VBZ NNS bless => VP |
| 73 |
| VP CC VP bless => VP |
| 74 |
| VP VP CC VP bless => VP |
| 75 |
| VBZ bless => VP |
| 76 |
|
| 77 |
period ~ '.' |
| 78 |
|
| 79 |
:discard ~ whitespace |
| 80 |
whitespace ~ [\s]+ |
| 81 |
|
| 82 |
CC ~ 'and' |
| 83 |
DT ~ 'a' | 'an' |
| 84 |
NN ~ 'panda' |
| 85 |
NNS ~ 'shoots' | 'leaves' |
| 86 |
VBZ ~ 'eats' | 'shoots' | 'leaves' |
| 87 |
|
| 88 |
=for Marpa::R2::Display::End |
| 89 |
|
| 90 |
Here's the code. It actually does two traversals, one that produces the full result as |
| 91 |
shown above, and another which "prunes" the forest down to a single tree. |
| 92 |
|
| 93 |
=for Marpa::R2::Display |
| 94 |
name: ASF synopsis code |
| 95 |
normalize-whitespace: 1 |
| 96 |
|
| 97 |
my $panda_grammar = Marpa::R2::Scanless::G->new( |
| 98 |
{ source => \$dsl, bless_package => 'PennTags', } ); |
| 99 |
my $panda_recce = Marpa::R2::Scanless::R->new( { grammar => $panda_grammar } ); |
| 100 |
$panda_recce->read( \$sentence ); |
| 101 |
my $asf = Marpa::R2::ASF->new( { slr=>$panda_recce } ); |
| 102 |
my $full_result = $asf->traverse( {}, \&full_traverser ); |
| 103 |
my $pruned_result = $asf->traverse( {}, \&pruning_traverser ); |
| 104 |
|
| 105 |
=for Marpa::R2::Display::End |
| 106 |
|
| 107 |
The code for the full traverser is in an appendix. |
| 108 |
The pruning code is simpler. Here it is: |
| 109 |
|
| 110 |
=for Marpa::R2::Display |
| 111 |
name: ASF synopsis pruning traverser code |
| 112 |
normalize-whitespace: 1 |
| 113 |
|
| 114 |
sub penn_tag { |
| 115 |
my ($symbol_name) = @_; |
| 116 |
return q{.} if $symbol_name eq 'period'; |
| 117 |
return $symbol_name; |
| 118 |
} |
| 119 |
|
| 120 |
sub pruning_traverser { |
| 121 |
|
| 122 |
# This routine converts the glade into a list of Penn-tagged elements. It is called recursively. |
| 123 |
my ( $glade, $scratch ) = @_; |
| 124 |
my $rule_id = $glade->rule_id(); |
| 125 |
my $symbol_id = $glade->symbol_id(); |
| 126 |
my $symbol_name = $panda_grammar->symbol_name($symbol_id); |
| 127 |
|
| 128 |
# A token is a single choice, and we know enough to fully Penn-tag it |
| 129 |
if ( not defined $rule_id ) { |
| 130 |
my $literal = $glade->literal(); |
| 131 |
my $penn_tag = penn_tag($symbol_name); |
| 132 |
return "($penn_tag $literal)"; |
| 133 |
} |
| 134 |
|
| 135 |
my $length = $glade->rh_length(); |
| 136 |
my @return_value = map { $glade->rh_value($_) } 0 .. $length - 1; |
| 137 |
|
| 138 |
# Special case for the start rule |
| 139 |
return ( join q{ }, @return_value ) . "\n" if $symbol_name eq '[:start]'; |
| 140 |
|
| 141 |
my $join_ws = q{ }; |
| 142 |
$join_ws = qq{\n } if $symbol_name eq 'S'; |
| 143 |
my $penn_tag = penn_tag($symbol_name); |
| 144 |
return "($penn_tag " . ( join $join_ws, @return_value ) . ')'; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
=for Marpa::R2::Display::End |
| 149 |
|
| 150 |
Here is the "pruned" output: |
| 151 |
|
| 152 |
=for Marpa::R2::Display |
| 153 |
name: ASF pruned synopsis output |
| 154 |
remove-display-indent: 1 |
| 155 |
remove-blank-last-line: 1 |
| 156 |
|
| 157 |
(S (NP (DT a) (NN panda)) |
| 158 |
(VP (VBZ eats) (NP (NNS shoots) (CC and) (NNS leaves))) |
| 159 |
(. .)) |
| 160 |
|
| 161 |
=for Marpa::R2::Display::End |
| 162 |
|
| 163 |
=head1 About this document |
| 164 |
|
| 165 |
This document describes the abstract syntax forests (ASF's) of |
| 166 |
Marpa's SLIF interface. |
| 167 |
An ASF is an efficient and practical way to represent multiple abstract syntax trees (AST's). |
| 168 |
|
| 169 |
=head1 Constructor |
| 170 |
|
| 171 |
=head2 new() |
| 172 |
|
| 173 |
=for Marpa::R2::Display |
| 174 |
name: ASF low-level calls synopsis, code part 1 |
| 175 |
normalize-whitespace: 1 |
| 176 |
partial: 1 |
| 177 |
|
| 178 |
my $asf = Marpa::R2::ASF->new( { slr => $slr } ); |
| 179 |
die 'No ASF' if not defined $asf; |
| 180 |
|
| 181 |
=for Marpa::R2::Display::End |
| 182 |
|
| 183 |
Creates a new ASF object. |
| 184 |
Must be called with a list of one or more hashes of named arguments. |
| 185 |
Currently only one named argument is allowed, the C<slr> argument, and |
| 186 |
that argument is required. |
| 187 |
The value of the C<slr> argument must be a SLIF recognizer object. |
| 188 |
|
| 189 |
Returns the new ASF object, or C<undef> if there was a problem. |
| 190 |
|
| 191 |
=head1 Accessor |
| 192 |
|
| 193 |
=head2 grammar() |
| 194 |
|
| 195 |
=for Marpa::R2::Display |
| 196 |
name: ASF low-level calls synopsis, code part 2 |
| 197 |
normalize-whitespace: 1 |
| 198 |
partial: 1 |
| 199 |
|
| 200 |
my $grammar = $asf->grammar(); |
| 201 |
|
| 202 |
=for Marpa::R2::Display::End |
| 203 |
|
| 204 |
Returns the SLIF grammar associated with the ASF. |
| 205 |
This can be convenient when using SLIF grammar methods |
| 206 |
while examining an ASF. |
| 207 |
All failures are thrown as exceptions. |
| 208 |
|
| 209 |
=head1 The traverser method |
| 210 |
|
| 211 |
=head2 traverse() |
| 212 |
|
| 213 |
=for Marpa::R2::Display |
| 214 |
name: ASF synopsis code |
| 215 |
normalize-whitespace: 1 |
| 216 |
partial: 1 |
| 217 |
|
| 218 |
my $full_result = $asf->traverse( {}, \&full_traverser ); |
| 219 |
|
| 220 |
=for Marpa::R2::Display::End |
| 221 |
|
| 222 |
Performs a traversal of the ASF. |
| 223 |
Returns the value of the traversal, |
| 224 |
which is computed as described below. |
| 225 |
It requires two arguments. |
| 226 |
The first is a per-traversal object, which must be a Perl reference. |
| 227 |
The second argument must be a reference to a traverser function, |
| 228 |
Discussion of how to write a traverser follows. |
| 229 |
The C<traverse()> method may be called repeatedly for an ASF, |
| 230 |
with the same traverser, or with different ones. |
| 231 |
|
| 232 |
=head1 How to write a traverser |
| 233 |
|
| 234 |
The process of |
| 235 |
writing a traverser will be familiar if you have experience with |
| 236 |
traversing trees. |
| 237 |
The traverser may be called at every node of the forest. |
| 238 |
(These nodes are called B<glades>.) |
| 239 |
The traverser must return a value, |
| 240 |
which may not be an C<undef>. |
| 241 |
The value returned by the traverser becomes the value of the glade. |
| 242 |
The value of the topmost glade (called the B<peak>) becomes the value |
| 243 |
of the traversal, and will be the value returned by |
| 244 |
L<the C<traverse()> method|/"traverse()">. |
| 245 |
|
| 246 |
The traverser is always invoked once for the peak. |
| 247 |
The traverser is also invoked once for any glade whose |
| 248 |
value is required. |
| 249 |
It may or may not be invoked for other glades. |
| 250 |
The traverser is never invoked twice for the same glade. |
| 251 |
If more than one attempt to made |
| 252 |
to retrieve the value of a glade, |
| 253 |
the traverser will only be invoked for the first one -- |
| 254 |
all subsequent attempts will return a memoized value. |
| 255 |
|
| 256 |
The traverser is always invoked with two arguments. |
| 257 |
The first argument will be a B<glade object>. |
| 258 |
Methods of the glade object are used to find information about |
| 259 |
the glade, and to move around in it. |
| 260 |
|
| 261 |
The second of the two arguments to a traverser is |
| 262 |
the per-traversal object, which will be shared by all calls |
| 263 |
in the traversal. |
| 264 |
The per-traversal object |
| 265 |
may be used as a "scratch pad" for information |
| 266 |
that it is not convenient to pass via return values. |
| 267 |
Prefer the per-traversal object to the use of globals. |
| 268 |
|
| 269 |
"Moving around" in a glade means visiting its B<parse alternatives>. |
| 270 |
(Parse alternatives are usually called B<alternatives>, |
| 271 |
when the meaning is clear.) |
| 272 |
If a glade has exactly one alternative, it is called a B<trivial glade>. |
| 273 |
When invoked, the traverser points at the first alternative. |
| 274 |
Alternatives after the first may be visited using |
| 275 |
the L<C<next()>|/"next()"> glade method. |
| 276 |
|
| 277 |
Parse alternatives may be either token |
| 278 |
alternatives or rule alternatives. |
| 279 |
Rule and token alternatives may be distnguished |
| 280 |
with |
| 281 |
the L<C<rule_id()>|/"rule_id()"> glade method, |
| 282 |
which returns C<undef> if and only if the glade is positioned at a token |
| 283 |
alternative. |
| 284 |
|
| 285 |
As a special case, |
| 286 |
a glade representing a nulled symbol is always a trivial glade, |
| 287 |
containing only one token alternative. |
| 288 |
This means that a nulled symbol is always treated as a token |
| 289 |
in this context, |
| 290 |
even when it actually is the LHS symbol of a nulled rule. |
| 291 |
|
| 292 |
At all alternatives, |
| 293 |
the L<C<span()>|/"span()"> and |
| 294 |
the L<C<literal()>|/"literal()"> glade methods |
| 295 |
are of use. |
| 296 |
The L<C<symbol_id()>|/"symbol_id()"> glade method is |
| 297 |
also always of use, |
| 298 |
although its meaning varies. |
| 299 |
At token alternatives, the L<C<symbol_id()>|/"symbol_id()"> method returns the |
| 300 |
ID of the token symbol. |
| 301 |
At rule alternatives, the L<C<symbol_id()>|/"symbol_id()"> method returns the |
| 302 |
ID of the LHS symbol of the rule. |
| 303 |
|
| 304 |
At rule alternatives, |
| 305 |
the L<C<rh_length()>|/"rh_length()"> and |
| 306 |
the L<C<rh_value()>|/"rh_value()"> glade methods |
| 307 |
are of use. |
| 308 |
The C<rh_length()> method returns the length of the RHS, |
| 309 |
and the C<rh_value()> method returns the value of one of the |
| 310 |
RHS children, as determined using the traverser. |
| 311 |
|
| 312 |
At the peak of the ASF, the symbol will be named 'C<[:start]>'. |
| 313 |
This case often requires special treatment. |
| 314 |
Note that it is entirely possible for the peak glade to be non-trivial. |
| 315 |
|
| 316 |
=head1 Glade methods |
| 317 |
|
| 318 |
These are methods of the glade object. |
| 319 |
Glade objects are passed as arguments to the traversal routine, |
| 320 |
and are only valid within its scope. |
| 321 |
|
| 322 |
=head2 literal() |
| 323 |
|
| 324 |
=for Marpa::R2::Display |
| 325 |
name: ASF synopsis full traverser code |
| 326 |
normalize-whitespace: 1 |
| 327 |
partial: 1 |
| 328 |
|
| 329 |
my $literal = $glade->literal(); |
| 330 |
|
| 331 |
=for Marpa::R2::Display::End |
| 332 |
|
| 333 |
Returns the B<glade literal>, a string in the input which corresponds to this glade. |
| 334 |
The glade literal remains constant inside a glade. |
| 335 |
The C<literal()> method accepts no arguments. |
| 336 |
|
| 337 |
=head2 span() |
| 338 |
|
| 339 |
=for Marpa::R2::Display::Start |
| 340 |
name: ASF span() traverser method example |
| 341 |
normalize-whitespace: 1 |
| 342 |
partial: 1 |
| 343 |
|
| 344 |
my ( $start, $length ) = $glade->span(); |
| 345 |
my $end = $start + $length - 1; |
| 346 |
|
| 347 |
=for Marpa::R2::Display::End |
| 348 |
|
| 349 |
Returns the B<glade span>, two numbers which describe |
| 350 |
the location which corresponds to this glade. |
| 351 |
The first number will be the start of the span, as |
| 352 |
an offset in the input stream. |
| 353 |
The second number will be its length. |
| 354 |
The glade span remains constant within a glade. |
| 355 |
The C<span()> method accepts no arguments. |
| 356 |
|
| 357 |
The "end" character of the span, when defined, may be calculated as |
| 358 |
its start plus its length, minus one. |
| 359 |
Applications should note that glades representing nulled symbols |
| 360 |
are special cases. |
| 361 |
They will have a length of zero and, |
| 362 |
properly speaking, |
| 363 |
their literals are zero length and |
| 364 |
do not have defined first (start) and last (end) characters. |
| 365 |
|
| 366 |
=head2 symbol_id() |
| 367 |
|
| 368 |
=for Marpa::R2::Display |
| 369 |
name: ASF synopsis full traverser code |
| 370 |
normalize-whitespace: 1 |
| 371 |
partial: 1 |
| 372 |
|
| 373 |
my $symbol_id = $glade->symbol_id(); |
| 374 |
|
| 375 |
=for Marpa::R2::Display::End |
| 376 |
|
| 377 |
Returns the B<glade symbol>. |
| 378 |
For a token alternative, the glade symbol is the token |
| 379 |
symbol. |
| 380 |
For a rule alternative, the glade symbol is the LHS symbol of |
| 381 |
the rule. |
| 382 |
The symbol ID remains constant within a glade. |
| 383 |
The C<symbol_id()> method accepts no arguments. |
| 384 |
|
| 385 |
=head2 rule_id() |
| 386 |
|
| 387 |
=for Marpa::R2::Display |
| 388 |
name: ASF synopsis full traverser code |
| 389 |
normalize-whitespace: 1 |
| 390 |
partial: 1 |
| 391 |
|
| 392 |
my $rule_id = $glade->rule_id(); |
| 393 |
|
| 394 |
=for Marpa::R2::Display::End |
| 395 |
|
| 396 |
Returns the ID of the rule for the current |
| 397 |
alternative. |
| 398 |
The ID will be a non-negative number, |
| 399 |
or C<undef>. |
| 400 |
(Note that, |
| 401 |
for alternatives in this interface, both zero and |
| 402 |
C<undef> are considered valid rule IDs.) |
| 403 |
Returns C<undef> if and only if the current |
| 404 |
alternative is a token alternative. |
| 405 |
The C<rule_id()> method accepts no arguments. |
| 406 |
|
| 407 |
=head2 rh_length() |
| 408 |
|
| 409 |
=for Marpa::R2::Display |
| 410 |
name: ASF synopsis full traverser code |
| 411 |
normalize-whitespace: 1 |
| 412 |
partial: 1 |
| 413 |
|
| 414 |
my $length = $glade->rh_length(); |
| 415 |
|
| 416 |
=for Marpa::R2::Display::End |
| 417 |
|
| 418 |
Returns the number of RHS children of the current rule. |
| 419 |
On success, this will always be an integer greater than zero. |
| 420 |
The C<rh_length()> method accepts no arguments. |
| 421 |
It is a fatal error to call C<rh_length()> for a glade |
| 422 |
that currently points to a token alternative. |
| 423 |
|
| 424 |
=head2 rh_value() |
| 425 |
|
| 426 |
=for Marpa::R2::Display |
| 427 |
name: ASF synopsis full traverser code |
| 428 |
normalize-whitespace: 1 |
| 429 |
partial: 1 |
| 430 |
|
| 431 |
my $child_value = $glade->rh_value($rh_ix); |
| 432 |
|
| 433 |
=for Marpa::R2::Display::End |
| 434 |
|
| 435 |
Requires exactly one argument, C<$rh_ix>, which must be the zero-based |
| 436 |
index of a RHS child of the current rule instance. |
| 437 |
Returns the value of the child |
| 438 |
at index C<$rh_ix> |
| 439 |
of the current rule instance. |
| 440 |
For convenient iteration, |
| 441 |
returns C<undef> if the value of the C<$rh_ix> is greater than or equal to the RHS length. |
| 442 |
It is a fatal error to call C<rh_value()> for a glade |
| 443 |
that currently points to a token alternative. |
| 444 |
|
| 445 |
=head2 rh_values() |
| 446 |
|
| 447 |
=for Marpa::R2::Display::Start |
| 448 |
name: ASF rh_values() traverser method example |
| 449 |
normalize-whitespace: 1 |
| 450 |
partial: 1 |
| 451 |
|
| 452 |
my @return_value = $glade->rh_values(); |
| 453 |
|
| 454 |
=for Marpa::R2::Display::End |
| 455 |
|
| 456 |
Returns the RHS children of the current rule. |
| 457 |
The C<rh_values()> method accepts no arguments. |
| 458 |
It is a fatal error to call C<rh_values()> for a glade |
| 459 |
that currently points to a token alternative. |
| 460 |
|
| 461 |
=head2 all_choices() |
| 462 |
|
| 463 |
=for Marpa::R2::Display::Start |
| 464 |
name: ASF all_choices() traverser method example |
| 465 |
normalize-whitespace: 1 |
| 466 |
partial: 1 |
| 467 |
|
| 468 |
my @results = $glade->all_choices(); |
| 469 |
|
| 470 |
=for Marpa::R2::Display::End |
| 471 |
|
| 472 |
C<all_choices()> expects to be called by a traverser |
| 473 |
which always return its parse results as a reference |
| 474 |
to an array of choices. |
| 475 |
C<all_choices()> |
| 476 |
returns the Cartesian product of the lists of choices |
| 477 |
at the current position. |
| 478 |
|
| 479 |
For example, suppose there were 3 child values, |
| 480 |
and |
| 481 |
|
| 482 |
=over |
| 483 |
|
| 484 |
=item * C<A> was the only choice for the first child value; |
| 485 |
|
| 486 |
=item * C<B>, C<C> and C<D> were the 3 choices for the value |
| 487 |
of the second child; and |
| 488 |
|
| 489 |
=item * C<E> was the only choice for the third child value. |
| 490 |
|
| 491 |
=back |
| 492 |
|
| 493 |
We would then expect the traverser to return parse results |
| 494 |
that were references to arrays of the choices: |
| 495 |
|
| 496 |
=for Marpa::R2::Display |
| 497 |
ignore: 1 |
| 498 |
|
| 499 |
[A] |
| 500 |
[B,C,D] |
| 501 |
[E] |
| 502 |
|
| 503 |
=for Marpa::R2::Display::End |
| 504 |
|
| 505 |
and the result of C<all_choices()> would be their |
| 506 |
Cartesian product: |
| 507 |
|
| 508 |
=for Marpa::R2::Display |
| 509 |
ignore: 1 |
| 510 |
|
| 511 |
[A,B,E] |
| 512 |
[A,C,E] |
| 513 |
[A,D,E] |
| 514 |
|
| 515 |
=for Marpa::R2::Display::End |
| 516 |
|
| 517 |
If a parse result is not an unblessed array, |
| 518 |
C<all_choices()> will treat it as if it |
| 519 |
was the only choice for that child value. |
| 520 |
That is, if the parse result is C<X>, |
| 521 |
where C<X> is not an unblessed array, |
| 522 |
then effect would be the same as if C<[ X ]>, |
| 523 |
a reference to an array whose only |
| 524 |
element is C<X>, |
| 525 |
had been the parse result. |
| 526 |
|
| 527 |
=head2 next() |
| 528 |
|
| 529 |
=for Marpa::R2::Display |
| 530 |
name: ASF synopsis full traverser code |
| 531 |
normalize-whitespace: 1 |
| 532 |
partial: 1 |
| 533 |
|
| 534 |
last CHOICE if not defined $glade->next(); |
| 535 |
|
| 536 |
=for Marpa::R2::Display::End |
| 537 |
|
| 538 |
Points the glade at the next alternative. |
| 539 |
If there is no next alternative, returns C<undef>. |
| 540 |
On success, returns a defined value. |
| 541 |
One of the values returned on success may be |
| 542 |
the integer zero, so applications using |
| 543 |
this method to control an interation |
| 544 |
should be careful to check for a Perl defined value, |
| 545 |
and not for a Perl true value. |
| 546 |
|
| 547 |
In addition, because |
| 548 |
the L<C<rule_id()> method|/"rule_id()"> |
| 549 |
remains constant only within a symch, and |
| 550 |
the L<C<next()> method|/"next()"> may |
| 551 |
change the current symch, |
| 552 |
L<C<rule_id()> method|/"rule_id()"> |
| 553 |
must always be called to obtain the current rule ID |
| 554 |
in a C<while> loop that is controlled by |
| 555 |
the L<C<next()> method|/"next()">. |
| 556 |
|
| 557 |
=head1 Details |
| 558 |
|
| 559 |
This section contains additional explanations, not essential to understanding |
| 560 |
the rest of this document. |
| 561 |
Often they are formal or mathematical. |
| 562 |
While some people find these helpful, others find them distracting, |
| 563 |
which is why |
| 564 |
they are segregated here. |
| 565 |
|
| 566 |
=head2 Symches and factorings |
| 567 |
|
| 568 |
B<Symch> and B<factoring> are terms which are |
| 569 |
useful for some advanced applications. |
| 570 |
For the purposes of this document, |
| 571 |
the reader can consider the term "factoring" as a synonym |
| 572 |
for "parse alternative". |
| 573 |
A symch is either a rule symch or a token alternative. |
| 574 |
A rule symch is a series of rule alternatives (factorings) which share the same rule ID and the same glade. |
| 575 |
A glade's token alternative is a symch all by itself. |
| 576 |
The term B<symch> is shorthand for "symbolic choice". |
| 577 |
|
| 578 |
The value that each glade accessor |
| 579 |
returns can be classified as |
| 580 |
|
| 581 |
=over |
| 582 |
|
| 583 |
=item * remaining constant inside a glade; |
| 584 |
|
| 585 |
=item * remaining constant within a symch; or |
| 586 |
|
| 587 |
=item * potentially varying with each factoring. |
| 588 |
|
| 589 |
=back |
| 590 |
|
| 591 |
The values of the |
| 592 |
L<C<literal()>|/"literal()">, |
| 593 |
L<C<span()>|/"span()">, and |
| 594 |
L<C<symbol_id()>|/"symbol_id()"> methods |
| 595 |
remain constant inside each glade. |
| 596 |
The L<C<rule_id()> method|/"rule_id()"> remains constant within a symch -- |
| 597 |
in fact, the rule ID and the glade define a symch. |
| 598 |
(Recall that for alternatives in the ASF interface, |
| 599 |
C<undef> is considered a rule ID.) |
| 600 |
The values of the |
| 601 |
L<C<rh_length()> method|/"rh_length()"> |
| 602 |
and the values of the |
| 603 |
L<C<rh_value()> method|/"rh_value()"> method |
| 604 |
may vary with each alternative (factoring). |
| 605 |
|
| 606 |
When moving through a glade using |
| 607 |
the L<C<next()> method|/"next()">, |
| 608 |
alternatives within the same symch are visited |
| 609 |
as a group. |
| 610 |
More precisely, let |
| 611 |
the "current rule ID" be defined as |
| 612 |
the rule ID of the alternative at which the glade is |
| 613 |
currently pointing. |
| 614 |
The |
| 615 |
L<C<next()>|/"next()"> glade method guarantees that, |
| 616 |
before |
| 617 |
any alternative with a rule ID different from the current rule ID |
| 618 |
is visited, |
| 619 |
all of the so-far-unvisited alternatives that share the current rule ID will be |
| 620 |
visited. |
| 621 |
|
| 622 |
=head1 Appendix: full traverser code |
| 623 |
|
| 624 |
This code is not fully |
| 625 |
generalized. |
| 626 |
Applications will often not be able to use it |
| 627 |
exactly as is. |
| 628 |
Instead, this C<full_traverser()>, |
| 629 |
and the C<pruning_traverser()>, |
| 630 |
are intended to be used as templates. |
| 631 |
An application adopting this C<full_traverser()> |
| 632 |
will often want to introduce some pruning, |
| 633 |
as well as some processing of each child value. |
| 634 |
For example, the volumes of data processed is often |
| 635 |
quite large, and an application may wish to compact each |
| 636 |
child value in some way. |
| 637 |
|
| 638 |
=for Marpa::R2::Display |
| 639 |
name: ASF synopsis full traverser code |
| 640 |
normalize-whitespace: 1 |
| 641 |
|
| 642 |
sub full_traverser { |
| 643 |
|
| 644 |
# This routine converts the glade into a list of Penn-tagged elements. |
| 645 |
# It is called recursively. |
| 646 |
my ( $glade, $scratch ) = @_; |
| 647 |
my $rule_id = $glade->rule_id(); |
| 648 |
my $symbol_id = $glade->symbol_id(); |
| 649 |
my $symbol_name = $panda_grammar->symbol_name($symbol_id); |
| 650 |
|
| 651 |
# A token is a single choice, and we know enough to fully Penn-tag it |
| 652 |
if ( not defined $rule_id ) { |
| 653 |
my $literal = $glade->literal(); |
| 654 |
my $penn_tag = penn_tag($symbol_name); |
| 655 |
return ["($penn_tag $literal)"]; |
| 656 |
} ## end if ( not defined $rule_id ) |
| 657 |
|
| 658 |
# Our result will be a list of choices |
| 659 |
my @return_value = (); |
| 660 |
|
| 661 |
CHOICE: while (1) { |
| 662 |
|
| 663 |
# The results at each position are a list of choices, so |
| 664 |
# to produce a new result list, we need to take a Cartesian |
| 665 |
# product of all the choices |
| 666 |
my $length = $glade->rh_length(); |
| 667 |
my @results = ( [] ); |
| 668 |
for my $rh_ix ( 0 .. $length - 1 ) { |
| 669 |
my @new_results = (); |
| 670 |
for my $old_result (@results) { |
| 671 |
my $child_value = $glade->rh_value($rh_ix); |
| 672 |
for my $new_value ( @{$child_value} ) { |
| 673 |
push @new_results, [ @{$old_result}, $new_value ]; |
| 674 |
} |
| 675 |
} |
| 676 |
@results = @new_results; |
| 677 |
} ## end for my $rh_ix ( 0 .. $length - 1 ) |
| 678 |
|
| 679 |
# Special case for the start rule |
| 680 |
if ( $symbol_name eq '[:start]' ) { |
| 681 |
return [ map { join q{}, @{$_} } @results ]; |
| 682 |
} |
| 683 |
|
| 684 |
# Now we have a list of choices, as a list of lists. Each sub list |
| 685 |
# is a list of Penn-tagged elements, which we need to join into |
| 686 |
# a single Penn-tagged element. The result will be to collapse |
| 687 |
# one level of lists, and leave us with a list of Penn-tagged |
| 688 |
# elements |
| 689 |
my $join_ws = q{ }; |
| 690 |
$join_ws = qq{\n } if $symbol_name eq 'S'; |
| 691 |
push @return_value, map { |
| 692 |
'(' |
| 693 |
. penn_tag($symbol_name) . q{ } |
| 694 |
. ( join $join_ws, @{$_} ) . ')' |
| 695 |
} @results; |
| 696 |
|
| 697 |
# Look at the next alternative in this glade, or end the |
| 698 |
# loop if there is none |
| 699 |
last CHOICE if not defined $glade->next(); |
| 700 |
|
| 701 |
} ## end CHOICE: while (1) |
| 702 |
|
| 703 |
# Return the list of Penn-tagged elements for this glade |
| 704 |
return \@return_value; |
| 705 |
} |
| 706 |
|
| 707 |
=for Marpa::R2::Display::End |
| 708 |
|
| 709 |
=head1 Copyright and License |
| 710 |
|
| 711 |
=for Marpa::R2::Display |
| 712 |
ignore: 1 |
| 713 |
|
| 714 |
Copyright 2022 Jeffrey Kegler |
| 715 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 716 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 717 |
General Public License as published by the Free Software Foundation, |
| 718 |
either version 3 of the License, or (at your option) any later version. |
| 719 |
|
| 720 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 721 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 722 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 723 |
Lesser General Public License for more details. |
| 724 |
|
| 725 |
You should have received a copy of the GNU Lesser |
| 726 |
General Public License along with Marpa::R2. If not, see |
| 727 |
http://www.gnu.org/licenses/. |
| 728 |
|
| 729 |
=for Marpa::R2::Display::End |
| 730 |
|
| 731 |
=cut |
| 732 |
|
| 733 |
# Local Variables: |
| 734 |
# mode: cperl |
| 735 |
# cperl-indent-level: 4 |
| 736 |
# fill-column: 100 |
| 737 |
# End: |
| 738 |
# vim: expandtab shiftwidth=4: |