| 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::Order - How the SLIF ranks ambiguous parses |
| 19 |
|
| 20 |
=head1 Description |
| 21 |
|
| 22 |
Marpa allows ambiguous parses. |
| 23 |
While an unambiguous parse can produce at most one parse tree |
| 24 |
and one parse result, |
| 25 |
an ambiguous parse will produce a parse series. |
| 26 |
A parse series is a sequence of parse trees, |
| 27 |
each of which will have its own parse result. |
| 28 |
|
| 29 |
This document describes ways of controlling |
| 30 |
the order in which |
| 31 |
the L<SLIF recognizer's C<value()> method|Marpa::R2::Scanless::R/"value()"> |
| 32 |
evaluates the parse |
| 33 |
trees of an ambiguous parse. |
| 34 |
It also describes ways to exclude selected parse trees |
| 35 |
from the parse series. |
| 36 |
|
| 37 |
There are three ways to sort parses. |
| 38 |
|
| 39 |
=over 4 |
| 40 |
|
| 41 |
=item * |
| 42 |
|
| 43 |
Accept the default parse order. |
| 44 |
|
| 45 |
=item * |
| 46 |
|
| 47 |
Use a non-default value of the |
| 48 |
L<the recognizer's C<ranking_method>|Marpa::R2::Scanless::R/"ranking_method"> |
| 49 |
named argument. |
| 50 |
|
| 51 |
=item * |
| 52 |
|
| 53 |
Let the application do it. |
| 54 |
|
| 55 |
=back |
| 56 |
|
| 57 |
All these methods are described in more detail below. |
| 58 |
|
| 59 |
=head2 Semantic duplicates |
| 60 |
|
| 61 |
Regardless of the parse order chosen, |
| 62 |
no two parse trees in a parse series will be |
| 63 |
semantic duplicates. |
| 64 |
Two parse trees are B<semantic duplicates> if |
| 65 |
and only if |
| 66 |
a recursive, top-down evaluation of each |
| 67 |
applies |
| 68 |
the same rules |
| 69 |
in the same order |
| 70 |
at the same G1 locations. |
| 71 |
If the semantics are deterministic, |
| 72 |
parse trees that are semantic duplicates |
| 73 |
will always produce the same parse result. |
| 74 |
In other words, |
| 75 |
from the point of view of a deterministic semantics, |
| 76 |
parse trees that are semantic duplicates |
| 77 |
are indistinguishable. |
| 78 |
|
| 79 |
=head2 The default parse order |
| 80 |
|
| 81 |
By calling |
| 82 |
the recognizer's |
| 83 |
L<C<value()>|Marpa::R2::Scanless::R/"value()"> |
| 84 |
method |
| 85 |
repeatedly, |
| 86 |
Marpa can produce all the parse results |
| 87 |
in the current parse series. |
| 88 |
The default is for the parse results to be returned |
| 89 |
in an B<arbitrary parse order>. |
| 90 |
This corresponds to the "C<none>" value of |
| 91 |
L<the recognizer's C<ranking_method>|Marpa::R2::Scanless::R/"ranking_method"> |
| 92 |
named argument. |
| 93 |
|
| 94 |
Traversal of the parse trees in |
| 95 |
arbitrary parse order |
| 96 |
will be always be well-behaved |
| 97 |
in the sense |
| 98 |
that no two parse trees will be semantic duplicates, |
| 99 |
and no unique (semantic non-duplicate) |
| 100 |
parse tree will be omitted. |
| 101 |
No other property of arbitrary parse order is guaranteed. |
| 102 |
For example, the order may |
| 103 |
change each time |
| 104 |
the parse series is traversed. |
| 105 |
|
| 106 |
=head2 Non-default values of the C<ranking_method> named argument |
| 107 |
|
| 108 |
The default value of |
| 109 |
L<C<Marpa::R2>'s C<ranking_method> named |
| 110 |
argument|Marpa::R2::Scanless::R/"ranking_method"> |
| 111 |
is "C<none>". |
| 112 |
If the "C<none>" value of |
| 113 |
C<Marpa::R2>'s C<ranking_method> named |
| 114 |
argument is specified, |
| 115 |
the default parse order is selected. |
| 116 |
|
| 117 |
Use of non-default values |
| 118 |
of the C<ranking_method> named argument is somewhat like PEG, |
| 119 |
in that it ranks rule alternatives at choice points. |
| 120 |
It has the advantage over PEG of being more powerful, |
| 121 |
because it can rank the parses of any grammar that C<Marpa::R2> |
| 122 |
can parse. |
| 123 |
It is also safer in the sense that in C<Marpa::R2> |
| 124 |
the BNF guides the grammar in the tradiional way. |
| 125 |
The PEG parse description, |
| 126 |
while it looks like BNF, |
| 127 |
is not interpreted as BNF, |
| 128 |
and often omits parses that |
| 129 |
a BNF-driven parser like C<Marpa::R2> recognizes. |
| 130 |
The lack of safety comes from inability to predict |
| 131 |
which parses will be omitted --- |
| 132 |
it is very hard to determine, |
| 133 |
in general, what grammar a PEG parser is actually parsing. |
| 134 |
There is much more about the use of |
| 135 |
L<C<Marpa::R2>'s C<ranking_method> named |
| 136 |
argument|Marpa::R2::Scanless::R/"ranking_method"> |
| 137 |
in |
| 138 |
L<its own document|Marpa::R2::Scanless::Semantics::Rank>. |
| 139 |
|
| 140 |
=head2 Letting the application sort parses |
| 141 |
|
| 142 |
The most general way to sort Marpa parses is for the application |
| 143 |
to take control. |
| 144 |
The application can set up the Marpa semantic actions |
| 145 |
so that the parse result of every parse tree is a |
| 146 |
C<< <rank, true_value> >> duple. |
| 147 |
The duples can then be sorted by C<rank>. |
| 148 |
Once the results are sorted, |
| 149 |
the C<rank> element of the duple can be discarded. |
| 150 |
(Those familiar with the Schwartzian transform |
| 151 |
may note a resemblance. |
| 152 |
In Perl, |
| 153 |
duples can be implemented as references to arrays of 2 elements.) |
| 154 |
|
| 155 |
One way for an application to implement ranking |
| 156 |
is with Marpa's |
| 157 |
L<abstract syntax forests|Marpa::R2::ASF>. |
| 158 |
Ranking using ASF's will probably be less efficient than using the |
| 159 |
recognizer's ranking method, |
| 160 |
but allows a more general and powerful solution. |
| 161 |
|
| 162 |
The user needs to be careful. |
| 163 |
In theory, ambiguity can cause an exponential explosion in the number of results. |
| 164 |
In practice, ambiguity tends to get out of hand very easily. |
| 165 |
Producing and sorting all the parses can take a very |
| 166 |
long time. |
| 167 |
|
| 168 |
=head1 Copyright and License |
| 169 |
|
| 170 |
=for Marpa::R2::Display |
| 171 |
ignore: 1 |
| 172 |
|
| 173 |
Copyright 2022 Jeffrey Kegler |
| 174 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 175 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 176 |
General Public License as published by the Free Software Foundation, |
| 177 |
either version 3 of the License, or (at your option) any later version. |
| 178 |
|
| 179 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 180 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 181 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 182 |
Lesser General Public License for more details. |
| 183 |
|
| 184 |
You should have received a copy of the GNU Lesser |
| 185 |
General Public License along with Marpa::R2. If not, see |
| 186 |
http://www.gnu.org/licenses/. |
| 187 |
|
| 188 |
=for Marpa::R2::Display::End |
| 189 |
|
| 190 |
=cut |
| 191 |
|
| 192 |
# vim: expandtab shiftwidth=4: |