| 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::Exhaustion - Parse exhaustion in the SLIF |
| 19 |
|
| 20 |
=head1 About this document |
| 21 |
|
| 22 |
This page is part of the reference documents for the recognizer objects |
| 23 |
of Marpa's SLIF (Scanless interface). |
| 24 |
It contains a detailed discussion of parse exhaustion. |
| 25 |
|
| 26 |
|
| 27 |
=head1 Exhaustion |
| 28 |
|
| 29 |
At bottom, B<parse exhaustion> is a simple concept. |
| 30 |
The recognizer may reach a point where there is |
| 31 |
simply no way to continue successfully. |
| 32 |
Regardless of what it reads next, |
| 33 |
the parse will fail. |
| 34 |
When this happens, the parse is said to be B<exhausted>. |
| 35 |
|
| 36 |
Some users have confused parse exhaustion with parse failure. |
| 37 |
But other users have confused parse exhaustion with parse success. |
| 38 |
That is because, |
| 39 |
for a particular grammar, |
| 40 |
there can be a strong association between parse exhaustion |
| 41 |
and parse success, |
| 42 |
but the strong association can go either way. |
| 43 |
Grammars can be either exhaustion-loving or exhaustion-hating. |
| 44 |
B<Both> kinds of grammar are very common in |
| 45 |
practical application. |
| 46 |
|
| 47 |
=head1 Hate and love |
| 48 |
|
| 49 |
In an B<exhaustion-hating> application, |
| 50 |
parse exhaustion is typically parse failure. |
| 51 |
C programs, Perl scripts and most programming languages |
| 52 |
are exhaustion-hating applications. |
| 53 |
If a C program is well-formed, |
| 54 |
it is always possible to read more input. |
| 55 |
The same is true of a Perl program that does not have a C<__DATA__> section. |
| 56 |
|
| 57 |
In an B<exhaustion-loving> applications |
| 58 |
parse exhaustion means parse success. |
| 59 |
A toy example of an exhaustion-loving |
| 60 |
application is the language consisting of balanced parentheses. |
| 61 |
When the parentheses come into perfect balance the parse is exhausted, |
| 62 |
because any further input would unbalance the brackets. |
| 63 |
And the parse succeeds when the parentheses come into perfect balance. |
| 64 |
Exhaustion means success. |
| 65 |
|
| 66 |
Any language which balances start and end indicators will tend to |
| 67 |
be exhaustion-loving. |
| 68 |
HTML and XML, with their start and end tags, can be seen |
| 69 |
as exhaustion-loving languages. |
| 70 |
|
| 71 |
For many languages, it's not strictly love or hate. |
| 72 |
I mentioned Perl's C<__DATA__> as a complication in a |
| 73 |
basically exhaustion-hating language. |
| 74 |
It is possible for a language to be |
| 75 |
exhaustion-loving at some points |
| 76 |
and exhaustion-hating at others. |
| 77 |
We can call those languages B<exhaustion-conflicted>. |
| 78 |
|
| 79 |
=head1 Reading methods |
| 80 |
|
| 81 |
The methods that may encounter parse exhaustion are those |
| 82 |
that read input: |
| 83 |
L<read()|Marpa::R2::Scanless::R/"read()">, |
| 84 |
L<resume()|Marpa::R2::Scanless::R/"resume()">, |
| 85 |
L<lexeme_complete()|Marpa::R2::Scanless::R/"lexeme_complete()">, |
| 86 |
and L<lexeme_read()|Marpa::R2::Scanless::R/"lexeme_read()">. |
| 87 |
These are also, and not by coincidence, the event-triggering methods. |
| 88 |
In this document, we will call them the B<reading methods>. |
| 89 |
|
| 90 |
=head1 Synchronous and asynchronous parse exhaustion |
| 91 |
|
| 92 |
Intuitively, parse exhaustion is B<synchonous> if it |
| 93 |
occurs at a location where control would return to |
| 94 |
the user anyway, and asynchronous otherwise. |
| 95 |
More precisely, |
| 96 |
we say that parse exhaustion is B<synchronous> |
| 97 |
if it occurs at a point when control |
| 98 |
would normally return from SLIF to the application, |
| 99 |
due to either |
| 100 |
|
| 101 |
=over 4 |
| 102 |
|
| 103 |
=item * |
| 104 |
|
| 105 |
the parse reaching end of string (EOS), or |
| 106 |
|
| 107 |
=item * |
| 108 |
|
| 109 |
the occurrence of |
| 110 |
a parse event other than an exhaustion event. |
| 111 |
|
| 112 |
=back |
| 113 |
|
| 114 |
Marpa can be set up so that an event occurs on |
| 115 |
asynchronous exhaustion. |
| 116 |
Note that in The definition above |
| 117 |
the occurrence of an exhaustion event |
| 118 |
does not makes the parse exhaustion synchronous. |
| 119 |
This wording is chosen to avoid a paradox. |
| 120 |
If a parse exhaustion event could made exhaustion synchronous, |
| 121 |
then it B<should not> occur, because parse exhaustion events |
| 122 |
only occur on asynchronous parse exhaustion. |
| 123 |
But if the parse exhaustion event did not occur, |
| 124 |
then the parse exhaustion would be asynchronous, so that |
| 125 |
then a parse exhaustion event B<should> occur. |
| 126 |
|
| 127 |
In this document, an B<exhaustion location> is |
| 128 |
a location at which parse exhaustion occurs. |
| 129 |
|
| 130 |
=head1 Handling parse exhaustion |
| 131 |
|
| 132 |
How parse exhaustion is handled depends on the setting |
| 133 |
of the |
| 134 |
L<SLIF's C<exhaustion> recognizer |
| 135 |
setting|Marpa::R2::Scanless::R/"exhaustion">. |
| 136 |
The value of this may be "C<fatal>" or |
| 137 |
"C<event>". |
| 138 |
("C<fatal>" is the default.) |
| 139 |
|
| 140 |
Synchronous parse exhaustion is always ignored, |
| 141 |
regardless of the recognizer setting. |
| 142 |
No exhaustion event is triggered by synchronous |
| 143 |
parse exhaustion. |
| 144 |
|
| 145 |
If the C<exhaustion> setting is "C<fatal>", |
| 146 |
asynchronous parse exhaustion is thrown |
| 147 |
as a fatal error. |
| 148 |
If the C<exhaustion> setting is "C<event>", |
| 149 |
then an |
| 150 |
L<exhaustion event|Marpa::R2::Event/"Exhaustion events"> |
| 151 |
is triggered, returning control to the application. |
| 152 |
This is treated by the |
| 153 |
triggering method as a successful return. |
| 154 |
|
| 155 |
Note that the |
| 156 |
L<lexeme_complete()|Marpa::R2::Scanless::R/"lexeme_complete()">, |
| 157 |
and L<lexeme_read()|Marpa::R2::Scanless::R/"lexeme_read()">, |
| 158 |
always ignore |
| 159 |
parse exhaustion, |
| 160 |
regardless of the C<exhaustion> recognizer setting. |
| 161 |
This is because these methods |
| 162 |
read input only at a single location, |
| 163 |
so that every parse exhaustion is synchronous. |
| 164 |
|
| 165 |
=head1 Detecting parse exhaustion |
| 166 |
|
| 167 |
The return value of |
| 168 |
a reading method |
| 169 |
does not indicate whether exhaustion occurred or not. |
| 170 |
In most cases, you will either know |
| 171 |
from the context whether the parse is exhausted, |
| 172 |
or you will not care. |
| 173 |
But what if you do not know and do care? |
| 174 |
|
| 175 |
Those applications that want to know whether |
| 176 |
a parse is exhausted or not |
| 177 |
can directly query |
| 178 |
parse exhaustion status |
| 179 |
with |
| 180 |
the L<C<exhausted()> method|Marpa::R2::Scanless::R/"exhausted()">. |
| 181 |
Even when L<parse exhaustion events|Marpa::R2::Event/"Exhaustion events"> |
| 182 |
are enabled, |
| 183 |
using the C<exhausted()> method is the preferred method for detecting exhaustion, |
| 184 |
because it reports both asynchronous and synchronous parse exhaustion. |
| 185 |
Exhaustion events only trigger in cases of asynchronous parse exhaustion. |
| 186 |
|
| 187 |
=head1 Exhaustion-conflicted |
| 188 |
|
| 189 |
Exhaustion-conflicted applications are those which cannot be |
| 190 |
called exhaustion-loving or exhaustion-hating. |
| 191 |
This may be because their behavior is a combination of the two. |
| 192 |
But it may also be because the application's behavior is not known -- |
| 193 |
for example, while developing an application, it's convenient |
| 194 |
to assume that it is exhaustion-conflicted. |
| 195 |
|
| 196 |
The SLIF's behavior for exhaustion-conflicted applications has to be aimed |
| 197 |
at a "lowest common denominator". |
| 198 |
It is also a good idea for a default to be a lowest common |
| 199 |
denominator and, |
| 200 |
by default, |
| 201 |
the SLIF assumes that an application |
| 202 |
is exhausted-conflicted. |
| 203 |
In fact, |
| 204 |
the default behavior on parse exhaustion |
| 205 |
usually works well enough |
| 206 |
that it does not need customizing. |
| 207 |
|
| 208 |
For a typical application without events, end of parse (EOP) |
| 209 |
is end of string (EOS). |
| 210 |
In this case exhaustion before EOS is a fatal error, |
| 211 |
which is usually what is desired. |
| 212 |
On return due to EOS, |
| 213 |
unless the application checks, |
| 214 |
it will not know whether exhaustion occurred, but usually |
| 215 |
it does not care. |
| 216 |
If the application does care, it can |
| 217 |
L<check for exhaustion explicitly|/"Detecting parse exhaustion">. |
| 218 |
|
| 219 |
If the application uses events to signal EOP, |
| 220 |
the case is much the same. |
| 221 |
On return due to an event, |
| 222 |
the application will not know if exhaustion occurred, |
| 223 |
but usually |
| 224 |
it will not care. |
| 225 |
If the application does care, it can |
| 226 |
L<check for exhaustion explicitly|/"Detecting parse exhaustion">. |
| 227 |
|
| 228 |
If the application uses events for other purposes, |
| 229 |
an event may "hide" exhaustion, so that it is not |
| 230 |
thrown as an error. |
| 231 |
Typically, an application will soon attempt to continue |
| 232 |
the reading of input, |
| 233 |
and when it does there will be a fatal |
| 234 |
error. |
| 235 |
An application which wants to know about exhaustion immediately, |
| 236 |
either to "fast fail" or for other reasons, |
| 237 |
can |
| 238 |
L<check for exhaustion explicitly|/"Detecting parse exhaustion"> |
| 239 |
every time an event triggers. |
| 240 |
|
| 241 |
=head2 Exhaustion-loving |
| 242 |
|
| 243 |
For an exhaustion-loving application, |
| 244 |
what was said for exhaustion-conflicted applications |
| 245 |
applies without change. |
| 246 |
Applications that consider it important to confirm |
| 247 |
that exhaustion did occur at EOP can |
| 248 |
L<check for exhaustion explicitly|/"Detecting parse exhaustion"> |
| 249 |
|
| 250 |
Some applications go beyond being exhaustion-loving, |
| 251 |
and want to use exhaustion to signal the EOP. |
| 252 |
These exhaustion-sensing applications are |
| 253 |
L<discussed below|/"Exhaustion-sensing">. |
| 254 |
|
| 255 |
=head2 Exhaustion-hating |
| 256 |
|
| 257 |
Exhaustion-hating applications are handled reasonably |
| 258 |
by the default behavior. |
| 259 |
Asynchronous exhaustion will be a fatal error. |
| 260 |
Synchronous exhaustion |
| 261 |
will cause failure at the next read, |
| 262 |
unless it happens at EOP. |
| 263 |
By default, exhaustion at EOP will go unreported |
| 264 |
but if an application really is exhaustion-hating, |
| 265 |
the parse will fail, |
| 266 |
and parse failure will certainly show up |
| 267 |
when the application tries to evaluate the parse. |
| 268 |
|
| 269 |
Exhaustion-hating applications, if they want to be stricter |
| 270 |
than this, |
| 271 |
can check for exhaustion explicitly whenever |
| 272 |
a reading method returns. |
| 273 |
A possible annoyance is that, depending where it happens, |
| 274 |
exhaustion may also cause the reading method to throw an exception. |
| 275 |
Applications which want more orthogonality in their exhaustion handling |
| 276 |
can enable exhaustion events, |
| 277 |
which will prevent exceptions being thrown due to parse exhaustion. |
| 278 |
|
| 279 |
=head1 Exhaustion-sensing |
| 280 |
|
| 281 |
Sometimes an application, rather than read |
| 282 |
an entire input, |
| 283 |
wants to find the longest occurrence starting at some location. |
| 284 |
(Lexers are typically applications of this kind.) |
| 285 |
Looking for exhaustion is one way to try to implement this |
| 286 |
kind of "longest acceptable input stream" search. |
| 287 |
But exhaustion-sensing is not necessarily the best way, |
| 288 |
or even a good way, |
| 289 |
to find the "longest parse". |
| 290 |
Exhaustion may not happen until after last successful parse -- |
| 291 |
sometimes not until long after it. |
| 292 |
Completion parse events may be a cleaner way to deal with this. |
| 293 |
|
| 294 |
Applications which do want to use parse exhaustion as part of |
| 295 |
a strategy for finding the EOP |
| 296 |
can set the |
| 297 |
L<SLIF's C<exhaustion> recognizer setting|Marpa::R2::Scanless::R/"exhaustion"> |
| 298 |
to "C<event>", so that a parse event occurs at parse exhaustion. |
| 299 |
When the event-triggering method returns, the application |
| 300 |
can then |
| 301 |
L<check for exhaustion explicitly|/"Detecting parse exhaustion">. |
| 302 |
|
| 303 |
=head1 Copyright and License |
| 304 |
|
| 305 |
=for Marpa::R2::Display |
| 306 |
ignore: 1 |
| 307 |
|
| 308 |
Copyright 2022 Jeffrey Kegler |
| 309 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 310 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 311 |
General Public License as published by the Free Software Foundation, |
| 312 |
either version 3 of the License, or (at your option) any later version. |
| 313 |
|
| 314 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 315 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 316 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 317 |
Lesser General Public License for more details. |
| 318 |
|
| 319 |
You should have received a copy of the GNU Lesser |
| 320 |
General Public License along with Marpa::R2. If not, see |
| 321 |
http://www.gnu.org/licenses/. |
| 322 |
|
| 323 |
=for Marpa::R2::Display::End |
| 324 |
|
| 325 |
=cut |
| 326 |
|
| 327 |
# vim: expandtab shiftwidth=4: |
| 328 |
|