| 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 - How the SLIF evaluates parses |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
=for Marpa::R2::Display |
| 23 |
name: SLIF full synopsis |
| 24 |
normalize-whitespace: 1 |
| 25 |
|
| 26 |
use Marpa::R2; |
| 27 |
|
| 28 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 29 |
{ bless_package => 'My_Nodes', |
| 30 |
source => \(<<'END_OF_SOURCE'), |
| 31 |
:default ::= action => [values] bless => ::lhs |
| 32 |
lexeme default = action => [ start, length, value ] |
| 33 |
bless => ::name latm => 1 |
| 34 |
|
| 35 |
:start ::= Script |
| 36 |
Script ::= Expression+ separator => comma |
| 37 |
comma ~ [,] |
| 38 |
Expression ::= |
| 39 |
Number bless => primary |
| 40 |
| '(' Expression ')' bless => paren assoc => group |
| 41 |
|| Expression '**' Expression bless => exponentiate assoc => right |
| 42 |
|| Expression '*' Expression bless => multiply |
| 43 |
| Expression '/' Expression bless => divide |
| 44 |
|| Expression '+' Expression bless => add |
| 45 |
| Expression '-' Expression bless => subtract |
| 46 |
|
| 47 |
Number ~ [\d]+ |
| 48 |
:discard ~ whitespace |
| 49 |
whitespace ~ [\s]+ |
| 50 |
# allow comments |
| 51 |
:discard ~ <hash comment> |
| 52 |
<hash comment> ~ <terminated hash comment> | <unterminated |
| 53 |
final hash comment> |
| 54 |
<terminated hash comment> ~ '#' <hash comment body> <vertical space char> |
| 55 |
<unterminated final hash comment> ~ '#' <hash comment body> |
| 56 |
<hash comment body> ~ <hash comment char>* |
| 57 |
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 58 |
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 59 |
END_OF_SOURCE |
| 60 |
} |
| 61 |
); |
| 62 |
|
| 63 |
|
| 64 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 65 |
|
| 66 |
my $input = '42*2+7/3, 42*(2+7)/3, 2**7-3, 2**(7-3)'; |
| 67 |
$recce->read(\$input); |
| 68 |
my $value_ref = $recce->value(); |
| 69 |
die "No parse was found\n" if not defined $value_ref; |
| 70 |
|
| 71 |
# Result will be something like "86.33... 126 125 16" |
| 72 |
# depending on the floating point precision |
| 73 |
my $result = ${$value_ref}->doit(); |
| 74 |
|
| 75 |
package My_Nodes; |
| 76 |
|
| 77 |
sub My_Nodes::primary::doit { return $_[0]->[0]->doit() } |
| 78 |
sub My_Nodes::Number::doit { return $_[0]->[2] } |
| 79 |
sub My_Nodes::paren::doit { my ($self) = @_; $self->[1]->doit() } |
| 80 |
|
| 81 |
sub My_Nodes::add::doit { |
| 82 |
my ($self) = @_; |
| 83 |
$self->[0]->doit() + $self->[2]->doit(); |
| 84 |
} |
| 85 |
|
| 86 |
sub My_Nodes::subtract::doit { |
| 87 |
my ($self) = @_; |
| 88 |
$self->[0]->doit() - $self->[2]->doit(); |
| 89 |
} |
| 90 |
|
| 91 |
sub My_Nodes::multiply::doit { |
| 92 |
my ($self) = @_; |
| 93 |
$self->[0]->doit() * $self->[2]->doit(); |
| 94 |
} |
| 95 |
|
| 96 |
sub My_Nodes::divide::doit { |
| 97 |
my ($self) = @_; |
| 98 |
$self->[0]->doit() / $self->[2]->doit(); |
| 99 |
} |
| 100 |
|
| 101 |
sub My_Nodes::exponentiate::doit { |
| 102 |
my ($self) = @_; |
| 103 |
$self->[0]->doit()**$self->[2]->doit(); |
| 104 |
} |
| 105 |
|
| 106 |
sub My_Nodes::Script::doit { |
| 107 |
my ($self) = @_; |
| 108 |
return join q{ }, map { $_->doit() } @{$self}; |
| 109 |
} |
| 110 |
|
| 111 |
=for Marpa::R2::Display::End |
| 112 |
|
| 113 |
=head1 About this document |
| 114 |
|
| 115 |
This document describes the semantics for |
| 116 |
Marpa's primary interface, the SLIF. |
| 117 |
|
| 118 |
=head1 What is semantics? |
| 119 |
|
| 120 |
A parser is an algorithm |
| 121 |
that takes a string of symbols (tokens or characters) and finds a structure |
| 122 |
in it. |
| 123 |
Traditionally, that structure is a tree. |
| 124 |
|
| 125 |
Rarely is an application interested only in the tree. |
| 126 |
Usually the idea is that the string "means" something: |
| 127 |
the idea is that the string has a B<semantics>. |
| 128 |
Traditionally and most often, the tree |
| 129 |
is an intermediate step in producing a value, |
| 130 |
a value which represents the "meaning" or "semantics" of the string. |
| 131 |
|
| 132 |
"Evaluating" a tree means finding its semantics. |
| 133 |
The rest of this document describes Marpa's methods for evaluating trees. |
| 134 |
Those of you who have dealt with other traditional parsers, such as yacc and bison, |
| 135 |
will find Marpa's approach familiar. |
| 136 |
|
| 137 |
=head1 Instances |
| 138 |
|
| 139 |
At the start of evaluation, semantics is associated with instances |
| 140 |
of rule alternatives or of lexemes. |
| 141 |
An B<instance> is an occurrence in terms of G1 locations. |
| 142 |
Every instance has two locations: a start location and |
| 143 |
an end location. |
| 144 |
|
| 145 |
A rule alternative is the LHS of a rule, together with one of its |
| 146 |
RHS alternatives. |
| 147 |
Unless a rule is a prioritized rule, it has exactly one rule alternative. |
| 148 |
|
| 149 |
Prioritized rules very often only have one rule alternative, |
| 150 |
in which case they are called trivial prioritized rules. |
| 151 |
But prioritized rules may have many rule alternatives. |
| 152 |
|
| 153 |
When a rule has only one rule alternative, |
| 154 |
or when context makes it clear what is meant, |
| 155 |
a rule alternative is often simply called a rule. |
| 156 |
In particular, a B<rule alternative instance> is almost always |
| 157 |
called simply a B<rule instance>. |
| 158 |
|
| 159 |
=head1 Nodes |
| 160 |
|
| 161 |
In a parse tree, nodes are points where the tree branches or terminates. |
| 162 |
Tree terminations are also called terminals or "leaves". |
| 163 |
|
| 164 |
Every rule instance in a Marpa parse is represented by a branch point (or "node") |
| 165 |
in the tree. |
| 166 |
The topmost node of a tree is its "root node". |
| 167 |
(Trees are easiest to draw upside down, so traditionally in programming, |
| 168 |
the top of a tree is its root.) |
| 169 |
|
| 170 |
A node, or branch point, "branches" into zero or more "child nodes". |
| 171 |
The node just above a child node, |
| 172 |
the one from which the child node branches out, |
| 173 |
is called its parent node. |
| 174 |
|
| 175 |
If the node is for a non-quantified rule instance, |
| 176 |
the parent node is the LHS of the rule, |
| 177 |
and the child nodes are the RHS of the rule alternative. |
| 178 |
If the node is for a quantified rule, |
| 179 |
the parent node is the LHS of the quantified rule, |
| 180 |
and the child nodes are the items of the sequence of symbols on the |
| 181 |
right hand side. |
| 182 |
If the node is for a lexeme, |
| 183 |
the node represents the lexeme's symbol |
| 184 |
and there will be no child nodes. |
| 185 |
|
| 186 |
A parent node can have zero or more children. |
| 187 |
Rule instances with zero children are nulled rule instances, |
| 188 |
and are "leaf nodes". |
| 189 |
Leaf nodes are also called B<terminals>. |
| 190 |
In Marpa's parse trees, every terminal is either |
| 191 |
a lexeme or a nulled rule instance. |
| 192 |
|
| 193 |
In Marpa, |
| 194 |
evaluation only takes place within the structural (G1) subgrammar, |
| 195 |
and the descriptions of the behaviors of rule and lexeme instances below |
| 196 |
applies only to the G1 subgrammar. |
| 197 |
L0 rule alternatives and terminal symbols do not become nodes |
| 198 |
in the parse tree, |
| 199 |
and are never evaluated. |
| 200 |
|
| 201 |
=head1 The order of node evaluation |
| 202 |
|
| 203 |
The nodes of a Marpa parse tree are |
| 204 |
evaluated recursively, left-to-right and bottom-up. |
| 205 |
This means that, |
| 206 |
when a parent node is evaluated, |
| 207 |
the values of all child nodes are known |
| 208 |
and available for use by the semantics. |
| 209 |
The final value of a parse is the value of the top node |
| 210 |
of the parse tree. |
| 211 |
|
| 212 |
=head1 Parse trees |
| 213 |
|
| 214 |
The calls of the |
| 215 |
L<C<value()> method|Marpa::R2::Scanless::R/"value()"> |
| 216 |
by a SLIF recognizer produce a series of zero |
| 217 |
or more parses trees, |
| 218 |
called a B<parse series>. |
| 219 |
A recognizer will have only one parse series, |
| 220 |
unless it calls |
| 221 |
the L<C<series_restart()> method|Marpa::R2::Scanless::R/"series_restart()">. |
| 222 |
|
| 223 |
There may be zero parses in a parse series, |
| 224 |
because there may be no valid parse of a virtual input. |
| 225 |
There may be more than one parse in a parse series, |
| 226 |
because Marpa allows ambiguous parsing. |
| 227 |
Full details about the life cycle of a Marpa |
| 228 |
recognizer, |
| 229 |
including a full treatment of parse series |
| 230 |
can be found in |
| 231 |
L<another document|Marpa::R2::Semantics::Phases>. |
| 232 |
|
| 233 |
=head1 Nulled subtrees |
| 234 |
|
| 235 |
A nulled subtree is a subtree of the parse tree formed by a nulled node and its direct and |
| 236 |
indirect child nodes. |
| 237 |
(All these child nodes will also be nulled nodes.) |
| 238 |
Before evaluation, |
| 239 |
Marpa prunes all nulled subtrees back to their topmost nulled |
| 240 |
node. |
| 241 |
Of all the ways of dealing with nulled subtrees, this is the |
| 242 |
simplest and |
| 243 |
Marpa's users have found it a natural approach. |
| 244 |
More detail on the semantics of nulled symbols and subtrees |
| 245 |
can be found in |
| 246 |
L<a separate document|Marpa::R2::Semantics::Null>. |
| 247 |
|
| 248 |
=head1 Actions and how Marpa finds them |
| 249 |
|
| 250 |
The way in which the SLIF finds the value of a node is called that node's B<action>. |
| 251 |
Actions can be explicit or implicit. |
| 252 |
An explicit action is one that is explicitly specified by the application, |
| 253 |
in one of the ways |
| 254 |
to be described below. |
| 255 |
A node's implicit action is the one it performs if it has no |
| 256 |
explicit action. |
| 257 |
|
| 258 |
=head2 Lexeme actions |
| 259 |
|
| 260 |
The implicit action for a lexeme is to return |
| 261 |
its literal value in the input stream, as a string. |
| 262 |
An explicit default action name for lexemes may be set using the |
| 263 |
L<the lexeme default statement|Marpa::R2::Scanless::DSL/"Lexeme default statement">. |
| 264 |
A lexeme action cannot be a Perl closure action -- |
| 265 |
it must be one of the built-in actions that are |
| 266 |
appropriate for lexemes. |
| 267 |
|
| 268 |
=head2 Rule actions |
| 269 |
|
| 270 |
The implicit action for a rule instance is to return a Perl C<undef>. |
| 271 |
An explicit action for a RHS alternative can be specified using |
| 272 |
L<the C<action> adverb|Marpa::R2::Scanless::DSL/"action"> |
| 273 |
for the its RHS alternative. |
| 274 |
A default explicit action for RHS alternatives can be specified with a |
| 275 |
L<default pseudo-rule|Marpa::R2::Scanless::DSL/"Default pseudo-rule">. |
| 276 |
|
| 277 |
=head2 Nulled symbol actions |
| 278 |
|
| 279 |
As mentioned, nulled subtrees are pruned back to their topmost symbol. |
| 280 |
Lexemes are never nulled, so a nulled symbol is always the LHS of a rule instance, |
| 281 |
and the action is determined from the rule alternative, |
| 282 |
as L<just described|/"Rule actions">. |
| 283 |
|
| 284 |
A complication arises if the symbol appears on the LHS of more than one |
| 285 |
nullable rule alternative. Because the symbol is nulled, the input is no help in determining |
| 286 |
which rule alternative to use. The rule alternative whose semantics are used for a nulled symbol |
| 287 |
is determined as follows: |
| 288 |
|
| 289 |
=over 4 |
| 290 |
|
| 291 |
=item * If all nullable rule alternatives have the same semantics, that semantics is used. |
| 292 |
|
| 293 |
=item * If one of the nullable rule alternatives |
| 294 |
is empty (that is, has a zero-length RHS), |
| 295 |
then the empty alternative's semantics are used. |
| 296 |
|
| 297 |
=item * |
| 298 |
In the remaining case, |
| 299 |
two or more of the rule alternatives have different action names, |
| 300 |
but none of the alternatives has a zero-length RHS. |
| 301 |
When this happens, Marpa throws an exception. |
| 302 |
One easy way |
| 303 |
to fix the issue, |
| 304 |
is to add an empty rule with the intended semantics. |
| 305 |
|
| 306 |
=back |
| 307 |
|
| 308 |
In determining whether the semantics of two nullable rule alternatives |
| 309 |
are "the same", |
| 310 |
the blessing is taken into account. |
| 311 |
Two rule alternatives are considered to have different semantics if |
| 312 |
they are blessed differently. |
| 313 |
The SLIF's null semantics are described in more detail |
| 314 |
L<in a separate document|Marpa::R2::Semantics::Null>. |
| 315 |
|
| 316 |
=head1 Blessings |
| 317 |
|
| 318 |
Part of a rule alternative's or lexeme's action may be a blessing. |
| 319 |
A blessing is the name of a Perl package. |
| 320 |
In the case of a rule evaluation closure, |
| 321 |
the argument containing its child values will be blessed |
| 322 |
into that package. |
| 323 |
|
| 324 |
Not all actions are rule evaluation closures. |
| 325 |
An action may be, for example, an array descriptor action. |
| 326 |
In cases where the action is not a rule evaluation closure, |
| 327 |
the value |
| 328 |
of the action will be blessed into that package. |
| 329 |
|
| 330 |
Only Perl objects pointed to by references can be blessed. |
| 331 |
It is a fatal error to try to use a blessing with an inappropriate |
| 332 |
action. |
| 333 |
|
| 334 |
Implicitly |
| 335 |
(that is, if no blessing was explicitly specified), |
| 336 |
an action is not blessed. |
| 337 |
The implicit action itself cannot be blessed -- |
| 338 |
an attempt to do so is a fatal error. |
| 339 |
|
| 340 |
Explicit blessings are made using |
| 341 |
the C<bless> adverb. |
| 342 |
The C<bless> adverb is allowed |
| 343 |
|
| 344 |
=over 4 |
| 345 |
|
| 346 |
=item * for RHS alternatives; |
| 347 |
|
| 348 |
=item * for lexemes; |
| 349 |
|
| 350 |
=item * for the default lexeme statement; |
| 351 |
|
| 352 |
=item * and for the default pseudo-rule. |
| 353 |
|
| 354 |
=back |
| 355 |
|
| 356 |
An L0 RHS alternative cannot have a C<bless> adverb. |
| 357 |
|
| 358 |
The value of a C<bless> adverb is called a B<blessing>. |
| 359 |
If the blessing is a Perl word |
| 360 |
(a string of alphanumerics or underscores), |
| 361 |
the name of the class will be formed by prepending |
| 362 |
the value of L<the C<bless_package> |
| 363 |
named argument|Marpa::R2::Scanless::G/"bless_package">, followed |
| 364 |
by a double colon ("C<::>"). |
| 365 |
|
| 366 |
If the blessing begins with a double colon ("C<::>"), it is a |
| 367 |
reserved blessing. |
| 368 |
The reserved blessings are as follows: |
| 369 |
|
| 370 |
=over 4 |
| 371 |
|
| 372 |
=item C<::undef> |
| 373 |
|
| 374 |
The RHS alternatives or lexemes will not be |
| 375 |
blessed. |
| 376 |
When this document |
| 377 |
states that a RHS alternative or lexeme has a blessing |
| 378 |
of C<::undef>, |
| 379 |
it means exactly the same thing as when it states |
| 380 |
that a RHS alternative or lexeme will not be blessed. |
| 381 |
For both RHS alternatives and lexemes, |
| 382 |
the implicit blessing is C<::undef>. |
| 383 |
|
| 384 |
=item C<::lhs> |
| 385 |
|
| 386 |
The RHS alternative is blessed into |
| 387 |
a class whose name is based on the LHS of the RHS alternative. |
| 388 |
A blessing of C<::lhs> is not allowed for a lexeme. |
| 389 |
|
| 390 |
The class will be the name of the LHS with whitespace |
| 391 |
changed to an underscore. |
| 392 |
(As a reminder, the whitespace in symbol names will have been normalized, |
| 393 |
with leading and trailing whitespace removed, |
| 394 |
and all other whitespace sequences changed to a single ASCII space.) |
| 395 |
When a C<::lhs> blessing value applies to a rule alternative, it is a fatal |
| 396 |
error if the LHS contains anything other than alphanumerics |
| 397 |
and whitespace. |
| 398 |
In particular, the LHS cannot already contain an underscore ("C<_>"). |
| 399 |
The C<::lhs> blessing is most useful in a default pseudo-rule. |
| 400 |
|
| 401 |
=item C<::name> |
| 402 |
|
| 403 |
The lexeme |
| 404 |
is blessed into |
| 405 |
a class whose name is based on the name of the lexeme. |
| 406 |
The C<::name> blessing is not allowed for a RHS alternative. |
| 407 |
|
| 408 |
The class is derived from the symbol name in the same way, |
| 409 |
and subject to the same restrictions, |
| 410 |
as described above for deriving a class name from the LHS |
| 411 |
of a rule alternative. |
| 412 |
The C<::name> reserved blessing is most useful in |
| 413 |
the lexeme default statement. |
| 414 |
|
| 415 |
=back |
| 416 |
|
| 417 |
If any rule alternative or lexeme of a SLIF grammar has |
| 418 |
a blessing other than C<::undef>, |
| 419 |
a C<bless_package> is required, |
| 420 |
and failure to specify one results in |
| 421 |
a fatal error. |
| 422 |
|
| 423 |
=head1 Explicit actions |
| 424 |
|
| 425 |
There are four kinds of explicit action names: |
| 426 |
|
| 427 |
=over 4 |
| 428 |
|
| 429 |
=item * Array descriptors |
| 430 |
|
| 431 |
=item * Reserved action names |
| 432 |
|
| 433 |
=item * Perl identifiers |
| 434 |
|
| 435 |
=item * Perl names |
| 436 |
|
| 437 |
=back |
| 438 |
|
| 439 |
An explicit action is either a built-in action |
| 440 |
or a Perl closure action. |
| 441 |
Array descriptors and reserved action names |
| 442 |
are B<built-in actions>. |
| 443 |
The other actions are B<Perl closure actions>. |
| 444 |
|
| 445 |
=head1 Array descriptor actions |
| 446 |
|
| 447 |
=for Marpa::R2::Display |
| 448 |
name: SLIF DSL synopsis |
| 449 |
partial: 1 |
| 450 |
normalize-whitespace: 1 |
| 451 |
|
| 452 |
lexeme default = action => [ start, length, value ] |
| 453 |
bless => ::name latm => 1 |
| 454 |
|
| 455 |
=for Marpa::R2::Display::End |
| 456 |
|
| 457 |
If an action is enclosed in square brackets, it is an B<array descriptor>, |
| 458 |
and the value of the lexeme or rule alternative will be an array. |
| 459 |
Inside the array descriptor is a comma separated list of zero or more array item |
| 460 |
descriptors. |
| 461 |
The B<array item descriptors> are keywords that describe how the array is to be filled |
| 462 |
out. |
| 463 |
|
| 464 |
If the array descriptor is an empty pair of square brackets ("C<[]>"), |
| 465 |
then there are zero array item descriptors, |
| 466 |
and the value will be an empty array. |
| 467 |
Otherwise the array item descriptors are interpreted as lists |
| 468 |
and those lists are used to fill out the |
| 469 |
array. |
| 470 |
|
| 471 |
=over 4 |
| 472 |
|
| 473 |
=item C<g1len> |
| 474 |
|
| 475 |
The C<g1len> array item descriptor puts |
| 476 |
a single-element list into the array. |
| 477 |
That one element will be the length of the rule or lexeme instance, |
| 478 |
in G1 locations. |
| 479 |
|
| 480 |
=item C<g1length> |
| 481 |
|
| 482 |
THE C<g1length> ARRAY ITEM DESCRIPTOR IS DEPRECATED. |
| 483 |
It exists for hostirical reasons. |
| 484 |
The C<g1length> array item descriptor puts |
| 485 |
a single-element list into the array. |
| 486 |
That one element is C<g1len> plus one. |
| 487 |
|
| 488 |
=item C<g1start> |
| 489 |
|
| 490 |
The C<g1start> array item descriptor puts |
| 491 |
a single-element list into the array. |
| 492 |
That one element will be |
| 493 |
the G1 start location of the rule or lexeme instance. |
| 494 |
Together |
| 495 |
the C<g1length> and C<g1start> array item descriptors |
| 496 |
describe a G1 location |
| 497 |
L<span|Marpa::R2::Scanless::R/"Spans">. |
| 498 |
|
| 499 |
Typical applications will prefer to use |
| 500 |
the C<start> and C<length> array item descriptors, |
| 501 |
which report their results in terms of |
| 502 |
physical input stream locations, |
| 503 |
instead of G1 locations. |
| 504 |
G1 locations are useful in special cases, |
| 505 |
for example with application which do not scan |
| 506 |
monotonically forward in the physical input, |
| 507 |
but instead jump backwards in it. |
| 508 |
G1 locations are described in detail |
| 509 |
L<in another document|Marpa::R2::Scanless::R/"G1 locations">. |
| 510 |
|
| 511 |
=item C<length> |
| 512 |
|
| 513 |
The C<length> array item descriptor puts |
| 514 |
a single-element list into the array. |
| 515 |
That one element will be the length of the rule or lexeme instance. |
| 516 |
Length is in characters. |
| 517 |
|
| 518 |
=item C<lhs> |
| 519 |
|
| 520 |
The C<lhs> array item descriptor puts |
| 521 |
a single-element list into the array. |
| 522 |
That one element will be the LHS symbol ID of |
| 523 |
the rule. |
| 524 |
Because of historical reasons, |
| 525 |
for a lexeme instance, |
| 526 |
it will the symbol ID, |
| 527 |
but for a nulling symbol it will be a Perl C<undef>. |
| 528 |
|
| 529 |
=item C<name> |
| 530 |
|
| 531 |
The C<name> array item descriptor puts |
| 532 |
a single-element list into the array. |
| 533 |
This will always be a string. |
| 534 |
For a rule whose name is defined, |
| 535 |
that one element will be the rule name. |
| 536 |
For an unnamed rule, |
| 537 |
it will be the name of the LHS symbol. |
| 538 |
For a lexeme, |
| 539 |
it will be the symbol name of the lexeme. |
| 540 |
For a nulling symbol |
| 541 |
it will be the name of that symbol. |
| 542 |
|
| 543 |
=item C<rule> |
| 544 |
|
| 545 |
The C<rule> array item descriptor puts |
| 546 |
a single-element list into the array. |
| 547 |
For a rule, |
| 548 |
that one element will be the rule ID. |
| 549 |
In other cases, that one element will be a Perl C<undef>. |
| 550 |
|
| 551 |
=item C<start> |
| 552 |
|
| 553 |
The C<start> array item descriptor puts |
| 554 |
a single-element list into the array. |
| 555 |
That one element will be |
| 556 |
the start location of the rule or lexeme instance. |
| 557 |
The start location is an offset in the input string. |
| 558 |
The elements of the C<length> and C<start> item descriptors are defined |
| 559 |
such that the end location |
| 560 |
is always start location plus length. |
| 561 |
|
| 562 |
=item C<symbol> |
| 563 |
|
| 564 |
The C<symbol> array item descriptor puts |
| 565 |
a single-element list into the array. |
| 566 |
This will always be the name of a symbol. |
| 567 |
For a rule, |
| 568 |
it will be the name of the LHS symbol. |
| 569 |
For a lexeme, |
| 570 |
it will be the symbol name of the lexeme. |
| 571 |
For a nulling symbol |
| 572 |
it will be the name of that symbol. |
| 573 |
|
| 574 |
=item C<value> |
| 575 |
|
| 576 |
For a rule alternative, |
| 577 |
the C<value> array item descriptor puts |
| 578 |
a list of zero or more elements into the array. |
| 579 |
The list will contain |
| 580 |
the values of |
| 581 |
the rule instance's children, in left-to-right order. |
| 582 |
|
| 583 |
For a lexeme, |
| 584 |
the C<value> array item descriptor puts |
| 585 |
a single-element list into the array. |
| 586 |
That one element |
| 587 |
will be a list |
| 588 |
containing a single element, |
| 589 |
the token value of the lexeme. |
| 590 |
|
| 591 |
=item C<values> |
| 592 |
|
| 593 |
The C<value> and C<values> array item descriptors are synonyms, |
| 594 |
and may be used interchangeably for both rule alternatives and lexemes. |
| 595 |
|
| 596 |
=back |
| 597 |
|
| 598 |
=head2 Example |
| 599 |
|
| 600 |
The array item descriptors fill out the array in |
| 601 |
the order in which they appear in the array descriptor. |
| 602 |
For example, if we are dealing with a rule, |
| 603 |
and the array descriptor is "C<[ start, length, value ]>", |
| 604 |
then the return value is an reference to an array, |
| 605 |
whose length will vary, but which will contain at least |
| 606 |
two elements. |
| 607 |
The first element will be the start location in the |
| 608 |
input string of this rule instance, |
| 609 |
and the second will be its length. |
| 610 |
The remaining elements will be |
| 611 |
the values of the rule instance's RHS children, |
| 612 |
in lexical order. |
| 613 |
If the rule instance is nulled, the array will contain only two elements: |
| 614 |
start location and length. |
| 615 |
|
| 616 |
=head1 Reserved action names |
| 617 |
|
| 618 |
If the action value begins with a double colon ("C<::>"), |
| 619 |
it is a reserved action. |
| 620 |
The following are recognized: |
| 621 |
|
| 622 |
=over 4 |
| 623 |
|
| 624 |
=item * C<::array> |
| 625 |
|
| 626 |
C<::array> is equivalent to C<[values]>. |
| 627 |
This means that, for both lexeme and rule instances, |
| 628 |
the actions C<[values]>, C<[value]> and C<::array> |
| 629 |
will do exactly the same thing. |
| 630 |
|
| 631 |
=item * C<::first> |
| 632 |
|
| 633 |
The value of the rule instance is that of the rule instance's first child. |
| 634 |
If there is no such child, the value is a Perl C<undef>. |
| 635 |
It is a fatal error if a |
| 636 |
RHS alternative with a C<::first> action |
| 637 |
is blessed. |
| 638 |
It is also a fatal error to use a C<::first> action |
| 639 |
with a lexeme. |
| 640 |
|
| 641 |
=item * C<::undef> |
| 642 |
|
| 643 |
The value of the rule or lexeme instance will be |
| 644 |
a Perl C<undef>. |
| 645 |
It is a fatal error if a |
| 646 |
RHS alternative with an C<::undef> action |
| 647 |
is blessed. |
| 648 |
|
| 649 |
=back |
| 650 |
|
| 651 |
=head1 Perl identifiers as action names |
| 652 |
|
| 653 |
An action name is considered to be |
| 654 |
a Perl identifier, if it is a sequence of one or more alphanumerics |
| 655 |
and underscores. |
| 656 |
If the action name is a Perl identifier, |
| 657 |
it is treated as the name of a Perl variable. |
| 658 |
To successfully resolve to actions, |
| 659 |
Perl identifiers must be resolved to Perl names, |
| 660 |
as L<described below|/"Resolving Perl identifiers to Perl names">. |
| 661 |
|
| 662 |
=head1 Perl names as action names |
| 663 |
|
| 664 |
For this purpose, a Perl name |
| 665 |
is a series of two or more Perl identifiers |
| 666 |
separated by double colons ("C<::>"). |
| 667 |
Note that, by this definition, a Perl name cannot start with a double colon. |
| 668 |
Action names starting with double colons |
| 669 |
are always treated as reserved |
| 670 |
action names. |
| 671 |
|
| 672 |
Action names which are Perl names by this definition are |
| 673 |
treated as if they were fully qualified Perl names. |
| 674 |
Fully qualified Perl names are resolved to variables in Perl's namespace, |
| 675 |
L<as described below|/"Resolving Perl names to Perl variables">. |
| 676 |
|
| 677 |
=head1 The semantics package |
| 678 |
|
| 679 |
To resolve Perl identifiers to Perl names, |
| 680 |
a semantics package must be defined. |
| 681 |
The semantics package can be defined using the |
| 682 |
L<SLIF recognizer's C<semantics_package> named |
| 683 |
argument|Marpa::R2::Scanless::R/"semantics_package">, |
| 684 |
or it can be taken from the argument to the first C<value()> |
| 685 |
call of the parse series. |
| 686 |
(All of a recognizer's C<value()> method calls |
| 687 |
refer to a single parse series unless |
| 688 |
the recognizer calls |
| 689 |
the |
| 690 |
L<C<series_restart()> method|Marpa::R2::Scanless::R/"series_restart()">.) |
| 691 |
The C<semantics_package> named argument takes precedence. |
| 692 |
|
| 693 |
If the arguments to the C<value()> method are used to specify |
| 694 |
the semantics package, within a parse series they must consistently |
| 695 |
specify the same package. |
| 696 |
For details, see the description of |
| 697 |
L<SLIF recognizer's C<value()> |
| 698 |
method|Marpa::R2::Scanless::R/"value()">. |
| 699 |
|
| 700 |
If the user wants the Perl variables implementing the semantics |
| 701 |
in the C<main> namespace, |
| 702 |
she can specify |
| 703 |
C<"main"> |
| 704 |
as the semantics package. |
| 705 |
This is fine for small scripts and applications. |
| 706 |
For a large project, |
| 707 |
it is usually good practice to keep |
| 708 |
Perl variables intended for use by Marpa's semantics |
| 709 |
in their own namespace. |
| 710 |
|
| 711 |
=head1 Resolving Perl identifiers to Perl names |
| 712 |
|
| 713 |
A Perl identifier is resolved to a Perl name by prepending |
| 714 |
the semantic package, followed by a double colon ("C<::>"). |
| 715 |
For a Perl identifier to resolve successfully |
| 716 |
to a Perl name, |
| 717 |
a semantics package must be defined. |
| 718 |
|
| 719 |
For example, if the action name |
| 720 |
is "C<some_var>", the action name will be regarded as a Perl identifer. |
| 721 |
If the semantics package is "C<My_Actions>", Marpa will convert the action |
| 722 |
name to "C<My_Actions::some_var>", and hand |
| 723 |
it on for processing as a fully qualified Perl name. |
| 724 |
|
| 725 |
=head1 Resolving Perl names to Perl variables |
| 726 |
|
| 727 |
Once Marpa has a fully qualified Perl name, it looks in Perl's symbol tables for |
| 728 |
a Perl variable with that name, |
| 729 |
either the name of a subroutine, or of a scalar. |
| 730 |
It is important to note that for the purposes of Perl's symbol tables, |
| 731 |
and therefore for the purposes of Marpa's resolution of Perl names, |
| 732 |
references are scalars. |
| 733 |
|
| 734 |
If Marpa finds a Perl subroutine with that fully qualified Perl name, |
| 735 |
the action name is resolved to that subroutine, |
| 736 |
which then becomes a B<rule evaluation closure>. |
| 737 |
If Marpa does not find |
| 738 |
a Perl subroutine with that name, |
| 739 |
but does find a Perl scalar with that name, |
| 740 |
the action name is resolved to that Perl scalar. |
| 741 |
(Again, for this purpose a Perl reference is a kind of Perl scalar.) |
| 742 |
|
| 743 |
=head1 Executing rule evaluation closures |
| 744 |
|
| 745 |
A rule evaluation closure action is always called in scalar context, |
| 746 |
and its return value will be used as the value of its node. |
| 747 |
Arguments to the rule evaluation closure will be as follows: |
| 748 |
|
| 749 |
=over 4 |
| 750 |
|
| 751 |
=item * |
| 752 |
|
| 753 |
If the rule instance is not nulled and |
| 754 |
the rule alternative is not blessed, |
| 755 |
the second and subsequent arguments are the values of its child nodes, in lexical order. |
| 756 |
|
| 757 |
=item * |
| 758 |
|
| 759 |
If the rule instance is nulled, there will be only one argument: |
| 760 |
the per-parse argument. |
| 761 |
|
| 762 |
=item * |
| 763 |
|
| 764 |
If the rule alternative is blessed, and |
| 765 |
the rule instance is not nulled, |
| 766 |
the closure will always have exactly two arguments. |
| 767 |
The first will be the per-parse argument, |
| 768 |
and the second will be a blessed array that contains |
| 769 |
the child values in lexical order. |
| 770 |
(The grouping of child values into an array |
| 771 |
is required in order to allow the blessing to take effect.) |
| 772 |
|
| 773 |
=back |
| 774 |
|
| 775 |
Note that, in every case, |
| 776 |
the first argument of a rule evaluation closure is the per-parse argument. |
| 777 |
|
| 778 |
=head1 Quantified rule nodes |
| 779 |
|
| 780 |
Everything just said about rule nodes |
| 781 |
applies to nodes for |
| 782 |
L<quantified |
| 783 |
rules|Marpa::R2::Scanless::DSL/"Quantified rule">. |
| 784 |
But there is a difference between quantified rules |
| 785 |
and others, and it a big one if you are writing a rule |
| 786 |
evaluation closure. |
| 787 |
|
| 788 |
In other rules, the right hand side |
| 789 |
is fixed in length, |
| 790 |
and therefore the number of child nodes is known in advance. |
| 791 |
This is not the case with a quantified rule. |
| 792 |
The rule evaluation closure |
| 793 |
for a quantified rule |
| 794 |
must be capable of |
| 795 |
dealing with |
| 796 |
a variable number of child nodes. |
| 797 |
|
| 798 |
=head1 Action context |
| 799 |
|
| 800 |
=for Marpa::R2::Display |
| 801 |
name: SLIF action context synopsis |
| 802 |
normalize-whitespace: 1 |
| 803 |
|
| 804 |
sub do_S { |
| 805 |
my ($action_object) = @_; |
| 806 |
my $rule_id = $Marpa::R2::Context::rule; |
| 807 |
my $slg = $Marpa::R2::Context::slg; |
| 808 |
my ( $lhs, @rhs ) = |
| 809 |
map { $slg->symbol_display_form($_) } $slg->rule_expand($rule_id); |
| 810 |
$action_object->{text} = |
| 811 |
"rule $rule_id: $lhs ::= " |
| 812 |
. ( join q{ }, @rhs ) . "\n" |
| 813 |
. "locations: " |
| 814 |
. ( join q{-}, Marpa::R2::Context::location() ) . "\n"; |
| 815 |
return $action_object; |
| 816 |
} ## end sub do_S |
| 817 |
|
| 818 |
=for Marpa::R2::Display::End |
| 819 |
|
| 820 |
In addition to the per-parse argument |
| 821 |
and their child values, |
| 822 |
rule evaluation closures also have access |
| 823 |
to B<context variables>. |
| 824 |
|
| 825 |
=over |
| 826 |
|
| 827 |
=item * C<$Marpa::R2::Context::slg> is set to |
| 828 |
L<the SLIF grammar|Marpa::R2::Scanless::G> being parsed. |
| 829 |
|
| 830 |
=item * C<$Marpa::R2::Context::rule> is the ID of the |
| 831 |
current rule alternative. |
| 832 |
Given the rule alternative ID, an application can find |
| 833 |
its LHS and RHS symbols using |
| 834 |
L<the SLIF grammar's C<rule_expand()> method|Marpa::R2::Scanless::G/"rule_expand()">. |
| 835 |
|
| 836 |
=item * C<Marpa::R2::Context::location()> returns the start |
| 837 |
and end G1 locations of the current rule instance. |
| 838 |
Note that these are |
| 839 |
L<G1 locations|Marpa::R2::Scanless::R/"G1 locations">, |
| 840 |
not input stream locations. |
| 841 |
|
| 842 |
=back |
| 843 |
|
| 844 |
=head1 Bailing out of parse evaluation |
| 845 |
|
| 846 |
=for Marpa::R2::Display |
| 847 |
name: SLIF bail synopsis |
| 848 |
normalize-whitespace: 1 |
| 849 |
|
| 850 |
my $bail_message = "This is a bail out message!"; |
| 851 |
|
| 852 |
sub do_bail_with_message_if_A { |
| 853 |
my ($action_object, $terminal) = @_; |
| 854 |
Marpa::R2::Context::bail($bail_message) if $terminal eq 'A'; |
| 855 |
} |
| 856 |
|
| 857 |
sub do_bail_with_object_if_A { |
| 858 |
my ($action_object, $terminal) = @_; |
| 859 |
Marpa::R2::Context::bail([$bail_message]) if $terminal eq 'A'; |
| 860 |
} |
| 861 |
|
| 862 |
=for Marpa::R2::Display::End |
| 863 |
|
| 864 |
The C<Marpa::R2::Context::bail()> static method is used to |
| 865 |
"bail out" of the evaluation of a parse tree. |
| 866 |
It will cause an exception to be thrown. |
| 867 |
If its first and only argument is a reference, |
| 868 |
that reference is the exception object. |
| 869 |
Otherwise, an exception message is created |
| 870 |
by converting the method's arguments to strings, |
| 871 |
concatenating them, |
| 872 |
and prepending them with a message indicating |
| 873 |
the file and line number at which the |
| 874 |
C<Marpa::R2::Context::bail()> method was called. |
| 875 |
|
| 876 |
=head1 Perl scalars as actions |
| 877 |
|
| 878 |
If a Perl scalar is the action, |
| 879 |
it becomes the value of the node, as is. |
| 880 |
References are scalars in this context so that, |
| 881 |
for example, the value of the node could be a reference to an array. |
| 882 |
|
| 883 |
Another possibility is that the Perl scalar action is a reference to code. |
| 884 |
What happens in this case is very different from the case where the action |
| 885 |
is a rule evaluation closure. |
| 886 |
A rule evaluation closure is executed to produce the value of the node. |
| 887 |
In contrast, the reference to a subroutine is B<NOT> executed -- it becomes the |
| 888 |
value of the node directly. |
| 889 |
|
| 890 |
Assuming no trickery, such as use of Perl's C<local> keyword, takes place, |
| 891 |
resolution to a Perl scalar will always resolve to a single, global scalar. |
| 892 |
Any modification of this scalar will be seen |
| 893 |
by other nodes of the current parse, |
| 894 |
and by other parses. |
| 895 |
All this suggests that, |
| 896 |
as a matter of good practice, |
| 897 |
Perl scalar actions should only be used as constants. |
| 898 |
|
| 899 |
For example, |
| 900 |
assume that actions are in a package named C<My_Actions>, |
| 901 |
which contains a hash reference named C<empty_hash>, |
| 902 |
|
| 903 |
=for Marpa::R2::Display |
| 904 |
ignore: 1 |
| 905 |
|
| 906 |
package My_Actions; |
| 907 |
our $empty_hash = {}; |
| 908 |
|
| 909 |
=for Marpa::R2::Display::End |
| 910 |
|
| 911 |
It can be tempting, in building objects which are hashes, |
| 912 |
to start with a left node whose action is C<empty_hash> |
| 913 |
and to add contents to it as the object is passed up the evaluation |
| 914 |
tree. |
| 915 |
But C<$empty_hash> points to a single hash object. |
| 916 |
This single hash object will shared by all nodes, |
| 917 |
with all nodes seeing each other's changes. |
| 918 |
Worse, all Marpa parsers which use the same C<My_Actions> |
| 919 |
namespace will share the same hash object. |
| 920 |
The correct way to define an |
| 921 |
C<empty_hash> action that |
| 922 |
initializes an empty hash is as a rule |
| 923 |
evaluation closure |
| 924 |
that returns C<{}>. |
| 925 |
|
| 926 |
=for Marpa::R2::Display |
| 927 |
ignore: 1 |
| 928 |
|
| 929 |
sub My_Actions::empty_hash { return {}; } |
| 930 |
|
| 931 |
=for Marpa::R2::Display::End |
| 932 |
|
| 933 |
=head1 Visibility of Perl object actions |
| 934 |
|
| 935 |
Most applications do not manipulate the Perl symbol table at runtime, |
| 936 |
and do not make use of Perl's C<local> keyword for declarations. |
| 937 |
Applications which use the Perl global namespace in conventional ways, |
| 938 |
and which use the same names to point to the same variables |
| 939 |
throughout Marpa execution, |
| 940 |
can ignore questions about the visibility |
| 941 |
of the Perl variables used in actions. |
| 942 |
|
| 943 |
Less conventional applications should be aware that, |
| 944 |
for resolution from a Perl name to a Perl variable to take place, |
| 945 |
that Perl name must |
| 946 |
refer to the intended variable, |
| 947 |
and this variable must be visible, |
| 948 |
at the time when actions are resolved. |
| 949 |
The timing of action resolution |
| 950 |
is specified in |
| 951 |
L<a separate document|Marpa::R2::Semantics::Phases>. |
| 952 |
|
| 953 |
=head1 The per-parse argument |
| 954 |
|
| 955 |
The first argument of every rule evaluation closure is |
| 956 |
the B<per-parse> argument. |
| 957 |
This is initialized |
| 958 |
|
| 959 |
=over 4 |
| 960 |
|
| 961 |
=item * To the argument to the SLIF recognizer's C<value()> method, if that |
| 962 |
argument is defined. |
| 963 |
|
| 964 |
=item * Otherwise, to the result returned by the per-parse constructor, |
| 965 |
if there is a per-parse constructor. |
| 966 |
|
| 967 |
=item * Otherwise, as a last resort, to an empty hashref. |
| 968 |
|
| 969 |
=back |
| 970 |
|
| 971 |
The per-parse argument is destroyed once the evaluation of the parse tree is finished. |
| 972 |
Between creation and destruction, the per-parse argument is not touched by Marpa's internals -- |
| 973 |
it is reserved for use by the application. |
| 974 |
|
| 975 |
The primary way of passing data while evaluating a parse tree is purely functional -- |
| 976 |
results from child nodes are passed up to parent nodes. |
| 977 |
Applications can use |
| 978 |
the per-parse argument for data which does not conveniently fit the functional model. |
| 979 |
Symbol tables are |
| 980 |
one common example of data that is best handled outside the functional model. |
| 981 |
|
| 982 |
=head1 The per-parse constructor |
| 983 |
|
| 984 |
The per-parse constructor |
| 985 |
is the C<new()> method of the semantics package. |
| 986 |
If there is no semantics package, or if it has no C<new()> method, |
| 987 |
there is no per-parse constructor. |
| 988 |
The per-parse constructor is called with one argument: |
| 989 |
the name of the semantics package. |
| 990 |
|
| 991 |
The per-parse constructor is called |
| 992 |
in the |
| 993 |
L<Parse Tree Setup Subphase|Marpa::R2::Semantics::Phases/"Parse Tree Setup Subphase">. |
| 994 |
The Parse Tree Setup Subphase occurs during |
| 995 |
the first call to a recognizer's L<C<value()>|Marpa::R2::Scanless::R/"value()"> |
| 996 |
method in a parse series. |
| 997 |
(All of a recognizer's C<value()> method calls |
| 998 |
refer to a single parse series unless |
| 999 |
the recognizer calls |
| 1000 |
the |
| 1001 |
L<C<series_restart()> method|Marpa::R2::Scanless::R/"series_restart()">.) |
| 1002 |
More details about |
| 1003 |
Parse Tree Setup Subphase |
| 1004 |
can be found in |
| 1005 |
L<the document that describes the processing phases of Marpa's |
| 1006 |
semantics|Marpa::R2::Semantics::Phases>. |
| 1007 |
|
| 1008 |
=head1 Parse order |
| 1009 |
|
| 1010 |
If a parse is ambiguous, all parses are returned, |
| 1011 |
with no duplication. |
| 1012 |
By default, the order is arbitrary, but |
| 1013 |
it is also possible to control the order. |
| 1014 |
Details are in L<the document |
| 1015 |
on parse order|Marpa::R2::Semantics::Order>. |
| 1016 |
|
| 1017 |
=head1 Infinite loops |
| 1018 |
|
| 1019 |
Grammars with infinite loops (cycles) |
| 1020 |
are generally regarded as useless in practical applications. |
| 1021 |
Due to lack of interest, |
| 1022 |
the SLIF does not support them, |
| 1023 |
although Libmarpa itself and Marpa's thin interface do. |
| 1024 |
|
| 1025 |
=head1 Copyright and License |
| 1026 |
|
| 1027 |
=for Marpa::R2::Display |
| 1028 |
ignore: 1 |
| 1029 |
|
| 1030 |
Copyright 2022 Jeffrey Kegler |
| 1031 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 1032 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 1033 |
General Public License as published by the Free Software Foundation, |
| 1034 |
either version 3 of the License, or (at your option) any later version. |
| 1035 |
|
| 1036 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 1037 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1038 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 1039 |
Lesser General Public License for more details. |
| 1040 |
|
| 1041 |
You should have received a copy of the GNU Lesser |
| 1042 |
General Public License along with Marpa::R2. If not, see |
| 1043 |
http://www.gnu.org/licenses/. |
| 1044 |
|
| 1045 |
=for Marpa::R2::Display::End |
| 1046 |
|
| 1047 |
=cut |
| 1048 |
|
| 1049 |
# Local Variables: |
| 1050 |
# mode: cperl |
| 1051 |
# cperl-indent-level: 4 |
| 1052 |
# fill-column: 100 |
| 1053 |
# End: |
| 1054 |
# vim: expandtab shiftwidth=4: |