| 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::Phases - SLIF recognizer phases |
| 19 |
|
| 20 |
=head1 About this document |
| 21 |
|
| 22 |
This document describes in detail the sequence of events |
| 23 |
in the SLIF recognizer. |
| 24 |
It serves two purposes. |
| 25 |
|
| 26 |
=over 4 |
| 27 |
|
| 28 |
=item * |
| 29 |
|
| 30 |
It describes the sequence of events |
| 31 |
in the SLIF recognizer |
| 32 |
in fuller detail |
| 33 |
than the other documents do. |
| 34 |
|
| 35 |
=item * |
| 36 |
|
| 37 |
It describes the use of more than one parse series. |
| 38 |
|
| 39 |
=back |
| 40 |
|
| 41 |
Full detail of the sequence of events |
| 42 |
inside the SLIF recognizer |
| 43 |
is not usually necessary. |
| 44 |
Applications usually |
| 45 |
find that things "just work". |
| 46 |
But some application B<do> depend on the details. |
| 47 |
These include |
| 48 |
|
| 49 |
=over 4 |
| 50 |
|
| 51 |
=item * |
| 52 |
|
| 53 |
applications which make unusual use of side effects in the semantics; and |
| 54 |
|
| 55 |
=item * |
| 56 |
|
| 57 |
applications which alter their symbol tables at runtime. |
| 58 |
|
| 59 |
=back |
| 60 |
|
| 61 |
The semantics has two different modes, |
| 62 |
one of which is deprecated. |
| 63 |
If the SLIF recognizer is created from a grammar |
| 64 |
which uses the SLIF grammar's |
| 65 |
L<C<action_object>|Marpa::R2::Scanless::G/"action_object"> |
| 66 |
setting, |
| 67 |
the SLIF's semantics will be in "legacy" mode. |
| 68 |
Otherwise, the SLIF's semantics will be |
| 69 |
in "standard" mode. |
| 70 |
|
| 71 |
Legacy mode is deprecated and should not be used in new |
| 72 |
applications. |
| 73 |
It is documented in L<its own section|/"Legacy mode">. |
| 74 |
Unless otherwise specified, |
| 75 |
the other sections of this document assume |
| 76 |
that "standard" mode is in use. |
| 77 |
|
| 78 |
=head1 Parse trees and parse series |
| 79 |
|
| 80 |
Because Marpa allows ambiguous parsing, |
| 81 |
each parse can produce a |
| 82 |
a series of zero or more parse trees, |
| 83 |
-- a B<parse series>. |
| 84 |
Each parse tree has its own parse result. |
| 85 |
Because the parse may fail, |
| 86 |
there may be zero parse |
| 87 |
trees in a parse series. |
| 88 |
Because a parse may be ambiguous, |
| 89 |
there may be more than one parse tree |
| 90 |
in a parse series. |
| 91 |
|
| 92 |
The SLIF allows the recognizer to |
| 93 |
be run several times on the same |
| 94 |
virtual input stream. |
| 95 |
Each of these runs is a separate B<parse series>. |
| 96 |
Most applications use only |
| 97 |
one parse series. |
| 98 |
|
| 99 |
The call to the |
| 100 |
L<SLIF recognizer's C<read()> |
| 101 |
method|Marpa::R2::Scanless::R/"read()"> |
| 102 |
is the |
| 103 |
start of the first parse series. |
| 104 |
The first parse series continues until there is |
| 105 |
a call to the |
| 106 |
L<C<series_restart()> |
| 107 |
method|Marpa::R2::Scanless::R/"series_restart()"> |
| 108 |
or until the recognizer is destroyed. |
| 109 |
Each call of the C<series_restart()> method |
| 110 |
starts a new parse series. |
| 111 |
|
| 112 |
Few applications need to use more than |
| 113 |
a single parse series. |
| 114 |
But they are useful in some specialized |
| 115 |
applications. |
| 116 |
Starting a new parse series allows the applications |
| 117 |
to change some settings |
| 118 |
and to extend the input. |
| 119 |
Specifically, |
| 120 |
|
| 121 |
=over 4 |
| 122 |
|
| 123 |
=item * |
| 124 |
|
| 125 |
Once input is read into a SLIF recognizer's |
| 126 |
virtual input stream, it is there for the life of the recognizer. |
| 127 |
It cannot be "unread". |
| 128 |
But during the Reading Phase of a new parse series, |
| 129 |
a recognizer can extend the virtual input stream, |
| 130 |
using the recognizer's |
| 131 |
L<C<resume()> method|Marpa::R2::Scanless::R/"resume()">. |
| 132 |
|
| 133 |
=item * |
| 134 |
|
| 135 |
Within a parse series, |
| 136 |
the semantics package cannot change. |
| 137 |
But you can specify a new semantics package |
| 138 |
after starting a new parse series. |
| 139 |
|
| 140 |
=item * |
| 141 |
|
| 142 |
Certain other SLIF recognizer settings can be |
| 143 |
changed |
| 144 |
after starting a new parse series. |
| 145 |
These include the SLIF recognizer's |
| 146 |
L<C<end> setting|Marpa::R2::Scanless::R/"end">. |
| 147 |
For details, see |
| 148 |
L<Marpa::R2::Scanless::R/"Recognizer settings">. |
| 149 |
|
| 150 |
=back |
| 151 |
|
| 152 |
=head1 SLIF recognizer phases |
| 153 |
|
| 154 |
A SLIF recognizer is always in one of three phases: |
| 155 |
|
| 156 |
=over 4 |
| 157 |
|
| 158 |
=item * The Initial Phase; |
| 159 |
|
| 160 |
=item * a Reading Phase; or |
| 161 |
|
| 162 |
=item * an Evaluation Phase. |
| 163 |
|
| 164 |
=back |
| 165 |
|
| 166 |
In these documents, |
| 167 |
phase and subphase names are capitalized |
| 168 |
for clarity. |
| 169 |
|
| 170 |
Reading and Evaluation Phases |
| 171 |
are always part of a parse series. |
| 172 |
The Initial Phase is never part of any parse series. |
| 173 |
|
| 174 |
=head1 The Initial Phase |
| 175 |
|
| 176 |
The B<Initial Phase> of a SLIF recognizer |
| 177 |
begins when the recognizer is created with the |
| 178 |
a call of the L<C<new()>|Marpa::R2::Scanless::R/"Constructor"> method. |
| 179 |
It ends when |
| 180 |
the L<C<read()>|Marpa::R2::Scanless::R/"read()"> method |
| 181 |
is called. |
| 182 |
It will also end, of course, if |
| 183 |
the recognizer is destroyed, |
| 184 |
but most applications will want to |
| 185 |
continue into the next phases. |
| 186 |
|
| 187 |
There is always exactly one Initial Phase for |
| 188 |
every recognizer. |
| 189 |
The Initial Phase is not part of any parse series. |
| 190 |
Very little can happen in the Initial Phase, |
| 191 |
but it is possible to change some recognizer settings |
| 192 |
using the L<C<set()>|Marpa::R2::Scanless::R/"set()"> method. |
| 193 |
|
| 194 |
=head1 The Reading Phase |
| 195 |
|
| 196 |
The Reading Phase starts |
| 197 |
when a parse series starts, |
| 198 |
that is, |
| 199 |
with either a call to the |
| 200 |
L<C<read()> |
| 201 |
method|Marpa::R2::Scanless::R/"read()"> |
| 202 |
or a call to the |
| 203 |
L<C<series_restart()> |
| 204 |
method|Marpa::R2::Scanless::R/"series_restart()">. |
| 205 |
The Reading Phase ends when the first of the following occurs: |
| 206 |
|
| 207 |
=over 4 |
| 208 |
|
| 209 |
=item * |
| 210 |
|
| 211 |
The recognizer is destroyed. |
| 212 |
|
| 213 |
=item * |
| 214 |
|
| 215 |
A new parse series is begun |
| 216 |
by calling the C<series_restart()> method. |
| 217 |
This and the previous case are unusual. |
| 218 |
Applications almost always |
| 219 |
want to continue the parse series into the Evaluation Phase. |
| 220 |
|
| 221 |
=item * The C<value()> method is called for this SLIF recognizer. |
| 222 |
This is the most common case. |
| 223 |
|
| 224 |
=back |
| 225 |
|
| 226 |
The C<read()> method can be called only once for a SLIF recognizer. |
| 227 |
But, while a Reading Phase continues, |
| 228 |
the C<resume()> method may be called multiple times to continue reading |
| 229 |
input. |
| 230 |
The C<resume()> method should only be called except during the |
| 231 |
Reading Phase of a parse series. |
| 232 |
|
| 233 |
A Reading Phase is always part of a parse series. |
| 234 |
There is always exactly one Reading Phase for every parse series. |
| 235 |
|
| 236 |
=head1 The Evaluation Phase |
| 237 |
|
| 238 |
The Evaluation Phase begins with a recognizer's first call |
| 239 |
to the SLIF's C<value()> method. |
| 240 |
It ends when the recognizer is destroyed, |
| 241 |
or when |
| 242 |
the SLIF's C<series_restart()> method |
| 243 |
is called for that recognizer. |
| 244 |
If a parse series ends before the C<value()> method is |
| 245 |
called, there is no Evaluation Phase. |
| 246 |
|
| 247 |
The L<C<value()>|Marpa::R2::Scanless::R/"value()"> method |
| 248 |
may be called more than once during the Evaluation Phase. |
| 249 |
The second and later calls of |
| 250 |
the L<C<value()>|Marpa::R2::Scanless::R/"value()"> method will return |
| 251 |
the result of the next parse tree, if there is one. |
| 252 |
When there are no more parse trees, |
| 253 |
the L<C<value()>|Marpa::R2::Scanless::R/"value()"> method will return |
| 254 |
a Perl C<undef>. |
| 255 |
|
| 256 |
If you call |
| 257 |
the L<C<resume()>|Marpa::R2::Scanless::R/"resume()"> method |
| 258 |
during an Evaluation Phase, |
| 259 |
the behavior of |
| 260 |
Marpa::R2 |
| 261 |
is not defined. |
| 262 |
You should not do this. |
| 263 |
|
| 264 |
When it occurs, an Evaluation Phase is always part of a parse series. |
| 265 |
But the Evaluation Phase is optional -- |
| 266 |
a parse series may end without entering into an Evalution Phase. |
| 267 |
|
| 268 |
The Evaluation Phase has two subphases: |
| 269 |
|
| 270 |
=over 4 |
| 271 |
|
| 272 |
=item * |
| 273 |
|
| 274 |
The Parse Tree Setup Subphase. |
| 275 |
|
| 276 |
=item * |
| 277 |
|
| 278 |
The Parse Tree Traversal Subphase. |
| 279 |
|
| 280 |
=back |
| 281 |
|
| 282 |
B<Node Evaluation Time> |
| 283 |
is the Tree Traversal Subphase, as seen from the point of view of |
| 284 |
each rule node. It is not a separate phase. |
| 285 |
|
| 286 |
=head1 Parse Tree Setup Subphase |
| 287 |
|
| 288 |
The Parse Tree Setup Subphase |
| 289 |
occurs during during the first call |
| 290 |
to the C<value()> method of every series. |
| 291 |
In the Parse Tree Setup Subphase, the following happens: |
| 292 |
|
| 293 |
=over 4 |
| 294 |
|
| 295 |
=item * |
| 296 |
|
| 297 |
The per-parse argument is created. |
| 298 |
If a per-parse constructor was found, |
| 299 |
it is run at this point, and the per-parse argument is |
| 300 |
its return value. |
| 301 |
|
| 302 |
=item * |
| 303 |
|
| 304 |
All action names are resolved to |
| 305 |
actions -- |
| 306 |
Perl scalars |
| 307 |
or rule evaluation closures. |
| 308 |
The rule evaluation closures are not called in the Parse Tree Setup Subphase. |
| 309 |
They will be called later, |
| 310 |
in the Parse Tree Traversal Subphase. |
| 311 |
|
| 312 |
=back |
| 313 |
|
| 314 |
Exactly one Parse Tree Setup Subphase occurs |
| 315 |
for each parse tree. |
| 316 |
|
| 317 |
=head1 Parse Tree Traversal Subphase |
| 318 |
|
| 319 |
During the Parse Tree Traversal Subphase, |
| 320 |
the rule evaluation closures are called. |
| 321 |
|
| 322 |
=head1 Legacy mode |
| 323 |
|
| 324 |
If the SLIF recognizer is created from a grammar |
| 325 |
which uses the |
| 326 |
L<C<action_object>|Marpa::R2::Scanless::G/"action_object"> |
| 327 |
SLIF grammar setting, |
| 328 |
the SLIF recognizer will be in "legacy" mode. |
| 329 |
Legacy mode is deprecated. |
| 330 |
It is documented here only for the support of legacy |
| 331 |
applications. |
| 332 |
|
| 333 |
In legacy mode, |
| 334 |
there is an additional evaluation subphase, |
| 335 |
the Parse Series Setup Subphase, |
| 336 |
and tasks performed in standard mode |
| 337 |
by the Parse Tree Setup Subphase |
| 338 |
are split between the two subphases. |
| 339 |
|
| 340 |
The Parse Series Setup Subphase |
| 341 |
occurs just before |
| 342 |
the Parse Tree Setup Subphase, |
| 343 |
in the first C<value> method call |
| 344 |
of B<the first> parse series. |
| 345 |
It is important to emphasize that |
| 346 |
the Parse Series Setup Subphase |
| 347 |
occurs B<only in the first parse series>. |
| 348 |
|
| 349 |
In legacy mode, |
| 350 |
the only task performed by |
| 351 |
the Parse Tree Setup Phase is creation |
| 352 |
of the per-parse argument. |
| 353 |
Action resolution |
| 354 |
is performed in the |
| 355 |
the Parse Series Setup Phase. |
| 356 |
|
| 357 |
One practical implication |
| 358 |
of this is that, |
| 359 |
in legacy mode, |
| 360 |
the semantics package cannot be changed |
| 361 |
by starting a new parse series. |
| 362 |
Both are decided once and for all |
| 363 |
in the Parse Series Setup Subphase, |
| 364 |
and |
| 365 |
the Parse Series Setup Subphase |
| 366 |
occurs only once during the life |
| 367 |
cycle of a SLIF recognizer. |
| 368 |
|
| 369 |
The precise timing of semantics |
| 370 |
operations is affected by the |
| 371 |
changes described above. |
| 372 |
These changed timings, in turn, |
| 373 |
may affect applications that rely on side |
| 374 |
effects of the semantics. |
| 375 |
|
| 376 |
=head1 Copyright and License |
| 377 |
|
| 378 |
=for Marpa::R2::Display |
| 379 |
ignore: 1 |
| 380 |
|
| 381 |
Copyright 2022 Jeffrey Kegler |
| 382 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 383 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 384 |
General Public License as published by the Free Software Foundation, |
| 385 |
either version 3 of the License, or (at your option) any later version. |
| 386 |
|
| 387 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 388 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 389 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 390 |
Lesser General Public License for more details. |
| 391 |
|
| 392 |
You should have received a copy of the GNU Lesser |
| 393 |
General Public License along with Marpa::R2. If not, see |
| 394 |
http://www.gnu.org/licenses/. |
| 395 |
|
| 396 |
=for Marpa::R2::Display::End |
| 397 |
|
| 398 |
=cut |
| 399 |
|
| 400 |
# Local Variables: |
| 401 |
# mode: cperl |
| 402 |
# cperl-indent-level: 4 |
| 403 |
# fill-column: 100 |
| 404 |
# End: |
| 405 |
# vim: expandtab shiftwidth=4: |