| 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 - Release 2 of Marpa |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
=for Marpa::R2::Display |
| 23 |
name: Landing page synopsis |
| 24 |
normalize-whitespace: 1 |
| 25 |
|
| 26 |
use Marpa::R2; |
| 27 |
|
| 28 |
my $dsl = <<'END_OF_DSL'; |
| 29 |
:default ::= action => [name,values] |
| 30 |
lexeme default = latm => 1 |
| 31 |
|
| 32 |
Calculator ::= Expression action => ::first |
| 33 |
|
| 34 |
Factor ::= Number action => ::first |
| 35 |
Term ::= |
| 36 |
Term '*' Factor action => do_multiply |
| 37 |
| Factor action => ::first |
| 38 |
Expression ::= |
| 39 |
Expression '+' Term action => do_add |
| 40 |
| Term action => ::first |
| 41 |
Number ~ digits |
| 42 |
digits ~ [\d]+ |
| 43 |
:discard ~ whitespace |
| 44 |
whitespace ~ [\s]+ |
| 45 |
END_OF_DSL |
| 46 |
|
| 47 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); |
| 48 |
my $input = '42 * 1 + 7'; |
| 49 |
my $value_ref = $grammar->parse( \$input, 'My_Actions' ); |
| 50 |
|
| 51 |
sub My_Actions::do_add { |
| 52 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 53 |
return $t1 + $t2; |
| 54 |
} |
| 55 |
|
| 56 |
sub My_Actions::do_multiply { |
| 57 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 58 |
return $t1 * $t2; |
| 59 |
} |
| 60 |
|
| 61 |
=for Marpa::R2::Display::End |
| 62 |
|
| 63 |
=head1 Updates |
| 64 |
|
| 65 |
Users should consult Marpa::R2's "updates" page, |
| 66 |
which contains notes, errata, etc., |
| 67 |
added since the most recent release. |
| 68 |
The "updates" page is C<UPDATES.md> in the |
| 69 |
current repo. |
| 70 |
At this point, the link is |
| 71 |
L<https://github.com/jeffreykegler/Marpa--R2/blob/master/UPDATES.md>. |
| 72 |
|
| 73 |
=head1 Description |
| 74 |
|
| 75 |
=head2 Overview |
| 76 |
|
| 77 |
Marpa parses any language whose |
| 78 |
grammar can be written in BNF. |
| 79 |
That includes recursive grammars, |
| 80 |
ambiguous grammars, infinitely ambiguous grammars and |
| 81 |
grammars with useless or empty productions. |
| 82 |
Marpa does both left- and right-recursion in linear time -- |
| 83 |
in fact if a grammar is in any class currently |
| 84 |
in practical use, Marpa will parse it in linear time. |
| 85 |
|
| 86 |
This document centers around a short tutorial |
| 87 |
of the Scanless interface (SLIF). |
| 88 |
This is the interface most suitable for beginners. |
| 89 |
The SLIF is the most suitable interface for most advanced uses as well. |
| 90 |
|
| 91 |
=head1 A simple calculator |
| 92 |
|
| 93 |
The synopsis shows the code for an extremely simple calculator. |
| 94 |
It handles only |
| 95 |
addition and multiplication of integers. |
| 96 |
The sections which follow explain, line by line, how it works. |
| 97 |
The explanation will assume that the reader |
| 98 |
understands BNF and the basics of grammars -- what rules are, |
| 99 |
what symbols are, what the start symbol of a grammar is, etc. |
| 100 |
|
| 101 |
=head2 Marpa::R2::Scanless::G::new |
| 102 |
|
| 103 |
=for Marpa::R2::Display |
| 104 |
name: Landing page synopsis |
| 105 |
partial: 1 |
| 106 |
normalize-whitespace: 1 |
| 107 |
|
| 108 |
my $dsl = <<'END_OF_DSL'; |
| 109 |
:default ::= action => [name,values] |
| 110 |
lexeme default = latm => 1 |
| 111 |
|
| 112 |
Calculator ::= Expression action => ::first |
| 113 |
|
| 114 |
Factor ::= Number action => ::first |
| 115 |
Term ::= |
| 116 |
Term '*' Factor action => do_multiply |
| 117 |
| Factor action => ::first |
| 118 |
Expression ::= |
| 119 |
Expression '+' Term action => do_add |
| 120 |
| Term action => ::first |
| 121 |
Number ~ digits |
| 122 |
digits ~ [\d]+ |
| 123 |
:discard ~ whitespace |
| 124 |
whitespace ~ [\s]+ |
| 125 |
END_OF_DSL |
| 126 |
|
| 127 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); |
| 128 |
|
| 129 |
=for Marpa::R2::Display::End |
| 130 |
|
| 131 |
The code first creates a new SLIF grammar. |
| 132 |
SLIF grammars are |
| 133 |
L<C<Marpa::R2::Scanless:G>|Marpa::R2::Scanless::G> objects. |
| 134 |
They are created |
| 135 |
with the |
| 136 |
L<Marpa::R2::Scanless:G::new|Marpa::R2::Scanless::G/"Constructor"> |
| 137 |
constructor. |
| 138 |
The arguments to |
| 139 |
L<Marpa::R2::Scanless::G::new|Marpa::R2::Scanless::G/"Constructor"> |
| 140 |
are references to |
| 141 |
hashes of named arguments. |
| 142 |
In the key/value pairs of these hashes, |
| 143 |
the |
| 144 |
hash key |
| 145 |
is the |
| 146 |
name of the argument, |
| 147 |
and the |
| 148 |
hash value |
| 149 |
is the |
| 150 |
value of the named argument. |
| 151 |
|
| 152 |
In the example, there is only one named argument to the SLIF grammar constructor: |
| 153 |
C<source>. |
| 154 |
The value of C<source> must be a reference to a string in the |
| 155 |
L<SLIF's domain-specific language|Marpa::R2::Scanless::DSL> |
| 156 |
(DSL). |
| 157 |
In this example, the DSL consists of several rules |
| 158 |
and pseudo-rules. |
| 159 |
|
| 160 |
=head2 The default pseudo-rule |
| 161 |
|
| 162 |
=for Marpa::R2::Display |
| 163 |
name: Landing page synopsis |
| 164 |
partial: 1 |
| 165 |
normalize-whitespace: 1 |
| 166 |
|
| 167 |
:default ::= action => [name,values] |
| 168 |
lexeme default = latm => 1 |
| 169 |
|
| 170 |
=for Marpa::R2::Display::End |
| 171 |
|
| 172 |
These two lines set useful defaults. |
| 173 |
The first sets a default semantics, |
| 174 |
one which is especially useful for development. |
| 175 |
This is a finished script, so the default semantics |
| 176 |
is not used much. |
| 177 |
We'll talk about this more when we discuss |
| 178 |
L<semantics at the end|/"Semantics">. |
| 179 |
|
| 180 |
The second line sets the longest acceptable tokens match (LATM) |
| 181 |
style of lexing, |
| 182 |
which is what you'll almost always want. |
| 183 |
It is not the default for historical reasons, so your scripts will |
| 184 |
almost always start with this line. |
| 185 |
|
| 186 |
=head2 A G1 rule |
| 187 |
|
| 188 |
Next follows a G1, or structural rule. |
| 189 |
The first G1 rule in a script |
| 190 |
will usually be the start rule of the |
| 191 |
grammar. |
| 192 |
(It is also possible to |
| 193 |
L<specify the start rule |
| 194 |
explicitly|Marpa::R2::Scanless::DSL/"Start rule">.) |
| 195 |
|
| 196 |
Structural rules are the kinds of rules typically seen in BNF -- |
| 197 |
they describe the symbols which provide the structure of the grammar, |
| 198 |
but leave out details of whitespace. |
| 199 |
The SLIF also handles the lexical details in this example. |
| 200 |
It does this via L0 rules, |
| 201 |
which we will see shortly. |
| 202 |
|
| 203 |
=for Marpa::R2::Display |
| 204 |
name: Landing page synopsis |
| 205 |
partial: 1 |
| 206 |
normalize-whitespace: 1 |
| 207 |
|
| 208 |
Calculator ::= Expression action => ::first |
| 209 |
|
| 210 |
=for Marpa::R2::Display::End |
| 211 |
|
| 212 |
As is normal for BNF rules, the first rule consists of |
| 213 |
a left hand side symbol ("C<Calculator>"), |
| 214 |
the BNF operator ("C<::=>") |
| 215 |
and a series of right hand side (RHS) symbols. |
| 216 |
There is always exactly one left hand side (LHS) |
| 217 |
symbol. |
| 218 |
There may be any number of RHS symbols. |
| 219 |
In the case of an empty rule, |
| 220 |
the number of RHS symbols would be zero. |
| 221 |
In this rule, there is one RHS |
| 222 |
symbol, "C<Expression>". |
| 223 |
|
| 224 |
The BNF operator ("C<::=>") is what makes this rule |
| 225 |
a G1 (structural) rule. |
| 226 |
Later we will see lexical rules, which will use |
| 227 |
the match operator ("C<~>"). |
| 228 |
|
| 229 |
After the rule is an adverb: |
| 230 |
C<< action => ::first >>. |
| 231 |
We'll explain the purpose of the C<action> |
| 232 |
adverbs when we discuss |
| 233 |
L<semantics|/"Semantics"> |
| 234 |
|
| 235 |
The second rule is very similar to the first: |
| 236 |
|
| 237 |
=for Marpa::R2::Display |
| 238 |
name: Landing page synopsis |
| 239 |
partial: 1 |
| 240 |
normalize-whitespace: 1 |
| 241 |
|
| 242 |
Factor ::= Number action => ::first |
| 243 |
|
| 244 |
=for Marpa::R2::Display::End |
| 245 |
|
| 246 |
=head2 More complicated G1 rules |
| 247 |
|
| 248 |
=for Marpa::R2::Display |
| 249 |
name: Landing page synopsis |
| 250 |
partial: 1 |
| 251 |
normalize-whitespace: 1 |
| 252 |
|
| 253 |
Term ::= |
| 254 |
Term '*' Factor action => do_multiply |
| 255 |
| Factor action => ::first |
| 256 |
|
| 257 |
|
| 258 |
=for Marpa::R2::Display::End |
| 259 |
|
| 260 |
This rule says that an C<Term> may be one of two |
| 261 |
alternatives: |
| 262 |
|
| 263 |
=over 4 |
| 264 |
|
| 265 |
=item * |
| 266 |
|
| 267 |
A C<Term> and a C<Factor> separated by an multiplication operator; or |
| 268 |
|
| 269 |
=item * |
| 270 |
|
| 271 |
a C<Factor>. |
| 272 |
|
| 273 |
=back |
| 274 |
|
| 275 |
Immediately following is another G1 rule defining |
| 276 |
a C<Term>. It is very similar in form |
| 277 |
to the one for C<Expression>. |
| 278 |
|
| 279 |
=for Marpa::R2::Display |
| 280 |
name: Landing page synopsis |
| 281 |
partial: 1 |
| 282 |
normalize-whitespace: 1 |
| 283 |
|
| 284 |
Expression ::= |
| 285 |
Expression '+' Term action => do_add |
| 286 |
| Term action => ::first |
| 287 |
|
| 288 |
=for Marpa::R2::Display::End |
| 289 |
|
| 290 |
=head2 L0 rules |
| 291 |
|
| 292 |
The structural rules define the high-level structure of the grammar, |
| 293 |
and ignore details of whitespace, comments, etc. |
| 294 |
Now we look at how the low-level, lexical issues are handled. |
| 295 |
This very simple calculator language does not allow comments, |
| 296 |
but it does define whitespace. |
| 297 |
|
| 298 |
=for Marpa::R2::Display |
| 299 |
name: Landing page synopsis |
| 300 |
partial: 1 |
| 301 |
normalize-whitespace: 1 |
| 302 |
|
| 303 |
:discard ~ whitespace |
| 304 |
whitespace ~ [\s]+ |
| 305 |
|
| 306 |
=for Marpa::R2::Display::End |
| 307 |
|
| 308 |
The C<:discard> rule is a pseudo-rule, which tells Marpa to use whatever |
| 309 |
it matches to separate G1 symbols, |
| 310 |
but otherwise to ignore it -- to "discard" it. |
| 311 |
C<whitespace> is defined in the next rule as a sequence of one or more spaces. |
| 312 |
|
| 313 |
Note the match operator ("C<~>") in the rule defining whitespace. |
| 314 |
It tells Marpa that this rule is lexical and |
| 315 |
should be interpreted exactly as written, character |
| 316 |
by character. |
| 317 |
|
| 318 |
The C<whitespace> rule is a special kind of rule in two respects. |
| 319 |
First, its RHS is followed by a quantifier ("C<+>"), which makes it |
| 320 |
a sequence rule. |
| 321 |
Aside from the quantifier, |
| 322 |
sequence rules may only have a single symbol or character class on their RHS. |
| 323 |
The plus quantifier ("C<+>") means a sequence of one or more items. |
| 324 |
The star quantifier ("C<*>") is also allowed, and it indicates a sequence of |
| 325 |
zero or more items. |
| 326 |
|
| 327 |
The whitespace items are defined by a character class: C<[\s]>. |
| 328 |
Marpa supports the same character classes, and the same character class syntax, |
| 329 |
as Perl does. |
| 330 |
|
| 331 |
The next pair of L0 rules define the C<Number> symbol |
| 332 |
|
| 333 |
=for Marpa::R2::Display |
| 334 |
name: Landing page synopsis |
| 335 |
partial: 1 |
| 336 |
normalize-whitespace: 1 |
| 337 |
|
| 338 |
Number ~ digits |
| 339 |
digits ~ [\d]+ |
| 340 |
|
| 341 |
=for Marpa::R2::Display::End |
| 342 |
|
| 343 |
The above two rules say that a C<Number> is a sequence of one or more digits. |
| 344 |
C<Number> is a lexeme -- a G1 symbol which is defined and recognized at the lexical |
| 345 |
(L0) level. |
| 346 |
In this example, there are three other lexemes: |
| 347 |
C<whitespace>, and the addition and multiplication operators. |
| 348 |
|
| 349 |
We've already looked at |
| 350 |
the C<whitespace> lexeme, which will be discarded without being seen by G1. |
| 351 |
The addition and multiplication operators were defined with single quoted strings |
| 352 |
in the G1 rules. As a reminder, here's the rule for C<Term> again: |
| 353 |
|
| 354 |
=for Marpa::R2::Display |
| 355 |
name: Landing page synopsis |
| 356 |
partial: 1 |
| 357 |
normalize-whitespace: 1 |
| 358 |
|
| 359 |
Expression ::= |
| 360 |
Expression '+' Term action => do_add |
| 361 |
| Term action => ::first |
| 362 |
|
| 363 |
=for Marpa::R2::Display::End |
| 364 |
|
| 365 |
In the above rule, the single-quoted string C<'+'> implicitly defines a L0 lexeme. |
| 366 |
Something similar happens with the C<'*'> string in the rule for a C<Term>. |
| 367 |
|
| 368 |
The SLIF's lexer mostly "does what you mean". |
| 369 |
While the input is being read, it looks for all lexemes defined in the DSL. |
| 370 |
Almost always, you'll want Marpa to look only for tokens that are |
| 371 |
actually acceptable to the parse. |
| 372 |
Telling Marpa to do so was the purpose of this line: |
| 373 |
|
| 374 |
=for Marpa::R2::Display |
| 375 |
name: Landing page synopsis |
| 376 |
partial: 1 |
| 377 |
normalize-whitespace: 1 |
| 378 |
|
| 379 |
lexeme default = latm => 1 |
| 380 |
|
| 381 |
=for Marpa::R2::Display::End |
| 382 |
|
| 383 |
LATM means "longest acceptable tokens match". |
| 384 |
(LATM is not the default for historical reasons.) |
| 385 |
|
| 386 |
Among the acceptable tokens, |
| 387 |
Marpa looks for longest matches -- if multiple tokens match, |
| 388 |
the longest match is the winner. |
| 389 |
Marpa tolerates ambiguity, |
| 390 |
so one feature special to Marpa is that LATM is a longest acceptable B<tokens> match -- |
| 391 |
if more than one token is longest, all of them are considered in the parse. |
| 392 |
The logic of SLIF lexing is described with more precision in the |
| 393 |
L<SLIF overview document|Marpa::R2::Scanless>. |
| 394 |
|
| 395 |
=head2 Marpa::R2::Scanless::G::parse |
| 396 |
|
| 397 |
=for Marpa::R2::Display |
| 398 |
name: Landing page synopsis |
| 399 |
partial: 1 |
| 400 |
normalize-whitespace: 1 |
| 401 |
|
| 402 |
my $input = '42 * 1 + 7'; |
| 403 |
my $value_ref = $grammar->parse( \$input, 'My_Actions' ); |
| 404 |
|
| 405 |
=for Marpa::R2::Display::End |
| 406 |
|
| 407 |
To parse a string, |
| 408 |
we use |
| 409 |
the C<Marpa::R2::Scanless::G::parse()> method. |
| 410 |
C<Marpa::R2::Scanless::G::parse()> requires a reference |
| 411 |
to a string as its first argument. |
| 412 |
Optionally, the second argument is |
| 413 |
another string specifying the "semantics package". |
| 414 |
The "C<semantics_package>" |
| 415 |
tells Marpa the name of the Perl package that contains |
| 416 |
the closures implementing the semantics for this grammar. |
| 417 |
We will talk more about this below. |
| 418 |
|
| 419 |
=head2 Semantics |
| 420 |
|
| 421 |
The value of the parse result, as returned via the C<parse()> method, |
| 422 |
is determined by the parse's B<semantics>. |
| 423 |
Marpa's semantics are the traditional ones: |
| 424 |
The input is seen as a tree which takes its structure from the G1 rules. |
| 425 |
(This is why the G1 rules are called structural.) |
| 426 |
The value of the parse results from repeatedly evaluating nodes of this |
| 427 |
tree, starting at the bottom, with the results of child nodes made available |
| 428 |
to their parent node when the parent node is evaluated. |
| 429 |
|
| 430 |
Parse trees are usually drawn upside-down |
| 431 |
with their root at the top, |
| 432 |
and their "leaves" at the bottom. |
| 433 |
In Marpa::R2's SLIF, the "leaves" are the symbols |
| 434 |
that the G1 (structural) rules share with the L0 (lexical) |
| 435 |
rules. |
| 436 |
The symbols shared by L0 and G1 |
| 437 |
are those lexemes which are not |
| 438 |
discarded. |
| 439 |
In this example, the lexemes visible to G1 are |
| 440 |
C<Number> and two operators |
| 441 |
which are specified |
| 442 |
with a quoted string: |
| 443 |
"C<+>" |
| 444 |
and |
| 445 |
"C<*>". |
| 446 |
|
| 447 |
Marpa assigns values to the nodes of the tree, |
| 448 |
starting with the leaves. |
| 449 |
Marpa's "leaves" will always be L0 symbols, |
| 450 |
and their value by default is the literal |
| 451 |
value at their location in the input stream. |
| 452 |
In the case of the two operators described |
| 453 |
by quoted string, |
| 454 |
the value is that quoted string. |
| 455 |
That is, the value of 'C<+>' is |
| 456 |
'C<+>', |
| 457 |
and the value of 'C<*>' is |
| 458 |
'C<*>'. |
| 459 |
The value of C<Number> will be the portion |
| 460 |
of the input that matched the C<[\d]+> |
| 461 |
pattern. |
| 462 |
|
| 463 |
Starting with the values for leaves, |
| 464 |
Marpa::R2 moves recursively |
| 465 |
"up" the tree to its root, |
| 466 |
assigning a value to each node of the tree |
| 467 |
based on the value of its child nodes. |
| 468 |
Each non-leaf node corresponds to a G1 rule, |
| 469 |
and the children of the non-leaf node |
| 470 |
correspond to the RHS symbols |
| 471 |
of the rule. |
| 472 |
When the non-leaf node is valued, its value becomes |
| 473 |
the value of its LHS symbol, |
| 474 |
and this value will become |
| 475 |
the value of |
| 476 |
a RHS symbol of another node with one |
| 477 |
exception. |
| 478 |
|
| 479 |
The one exception, the node with a LHS symbol |
| 480 |
that does not become a RHS |
| 481 |
symbol, |
| 482 |
is the value of the top (or "root") node. |
| 483 |
The value of the top node becomes the |
| 484 |
value of the parse, |
| 485 |
and this is the parse result value |
| 486 |
to which the C<value()> method |
| 487 |
returns a reference. |
| 488 |
|
| 489 |
=for Marpa::R2::Display |
| 490 |
name: Landing page synopsis |
| 491 |
partial: 1 |
| 492 |
normalize-whitespace: 1 |
| 493 |
|
| 494 |
:default ::= action => [name,values] |
| 495 |
|
| 496 |
=for Marpa::R2::Display::End |
| 497 |
|
| 498 |
Each non-leaf node determines its value with an action. |
| 499 |
The default pseudo-rule allows you to specify the default action. |
| 500 |
(It is a pseudo-rule because its LHS, "C<:default>", is a pseudo-symbol, |
| 501 |
not a real one.) |
| 502 |
Often actions are Perl functions, which in this context are |
| 503 |
called Perl semantic closures. |
| 504 |
|
| 505 |
=for Marpa::R2::Display |
| 506 |
name: Landing page synopsis |
| 507 |
partial: 1 |
| 508 |
normalize-whitespace: 1 |
| 509 |
|
| 510 |
my $value_ref = $grammar->parse( \$input, 'My_Actions' ); |
| 511 |
|
| 512 |
=for Marpa::R2::Display::End |
| 513 |
|
| 514 |
When we did the parse, |
| 515 |
we used the C<semantics_package> named argument. |
| 516 |
The value of the C<semantics_package> argument |
| 517 |
specifies the package that is used |
| 518 |
to find the Perl semantic closures. |
| 519 |
|
| 520 |
In this example the default semantics, |
| 521 |
as specified by the C<default_action> named argument, |
| 522 |
come from a "array descriptor" named "C<[name,values]>". |
| 523 |
This indicates that, by default, the value of a rule is to be |
| 524 |
a reference to an array consisting of the rule's name, |
| 525 |
followed by the values of its children. |
| 526 |
|
| 527 |
In this case, |
| 528 |
the semantics is not actually used, |
| 529 |
and you would usually change it to something more convenient |
| 530 |
for your application. |
| 531 |
But "C<[name,values]>" is an excellent starting point when you're |
| 532 |
first developing a DSL and, since this code is intended as a |
| 533 |
template, we've kept it. |
| 534 |
For more about array descriptors, see the L<semantics |
| 535 |
document|Marpa::R2::Semantics/"Array descriptor actions"> |
| 536 |
|
| 537 |
The other way we specify semantics in this example |
| 538 |
is by using an |
| 539 |
C<action> adverb |
| 540 |
for a RHS alternative. |
| 541 |
We've seen the C<action> adverb several times, |
| 542 |
but skipped over it. |
| 543 |
Now it is time to look at it. |
| 544 |
|
| 545 |
=for Marpa::R2::Display |
| 546 |
name: Landing page synopsis |
| 547 |
partial: 1 |
| 548 |
normalize-whitespace: 1 |
| 549 |
|
| 550 |
Term ::= |
| 551 |
Term '*' Factor action => do_multiply |
| 552 |
| Factor action => ::first |
| 553 |
Expression ::= |
| 554 |
Expression '+' Term action => do_add |
| 555 |
| Term action => ::first |
| 556 |
|
| 557 |
=for Marpa::R2::Display::End |
| 558 |
|
| 559 |
The "C<::first>" action indicates that the value of a rule is to be |
| 560 |
the value of its first child, |
| 561 |
that is, the value corresponding to the first symbol of the rule's RHS. |
| 562 |
(In the case of an empty rule, the value would be a Perl C<undef>). |
| 563 |
(The initial double colon indicates a reserved action.) |
| 564 |
|
| 565 |
The action for the first RHS alternative defining C<Expression> is C<do_add>, |
| 566 |
and the action for the first RHS alternative defining C<Term> is C<do_multiply>. |
| 567 |
To implement these actions, we need to "resolve" their names -- |
| 568 |
map the action names into |
| 569 |
the Perl closures which actually carry out the semantics. |
| 570 |
|
| 571 |
The C<semantics_package> specified the package where we can find the actions: |
| 572 |
"C<My_Actions>". |
| 573 |
So, to resolve the C<do_multiply> action, |
| 574 |
Marpa looks for a closure whose fully qualified name is C<My_Actions::do_multiply>, |
| 575 |
which it finds: |
| 576 |
|
| 577 |
=for Marpa::R2::Display |
| 578 |
name: Landing page synopsis |
| 579 |
partial: 1 |
| 580 |
normalize-whitespace: 1 |
| 581 |
|
| 582 |
sub My_Actions::do_multiply { |
| 583 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 584 |
return $t1 * $t2; |
| 585 |
} |
| 586 |
|
| 587 |
=for Marpa::R2::Display::End |
| 588 |
|
| 589 |
The C<do_add> action is resolved |
| 590 |
to a Perl semantic closure |
| 591 |
in much the same way: |
| 592 |
|
| 593 |
=for Marpa::R2::Display |
| 594 |
name: Landing page synopsis |
| 595 |
partial: 1 |
| 596 |
normalize-whitespace: 1 |
| 597 |
|
| 598 |
sub My_Actions::do_add { |
| 599 |
my ( undef, $t1, undef, $t2 ) = @_; |
| 600 |
return $t1 + $t2; |
| 601 |
} |
| 602 |
|
| 603 |
=for Marpa::R2::Display::End |
| 604 |
|
| 605 |
The Perl semantic closures are callbacks. |
| 606 |
They are called as each node in a parse tree is evaluated. |
| 607 |
|
| 608 |
Each Perl semantic closure is called with one or more arguments. |
| 609 |
The first argument to a value action is always a per-parse-tree |
| 610 |
object, which the callbacks can use as a scratchpad. |
| 611 |
In this example, the per-parse-tree object is not used. |
| 612 |
The remaining arguments will be the values of the node's "children" -- |
| 613 |
in other words, the values computed for each of its RHS symbols, in order. |
| 614 |
If the action is for an empty rule, |
| 615 |
the per-parse-tree object will be its only argument. |
| 616 |
|
| 617 |
Every value action is expected to return a value. |
| 618 |
With one exception, |
| 619 |
this value is passed up to a parent node |
| 620 |
as an argument. |
| 621 |
The exception is the value for the start rule. |
| 622 |
The return value for the start rule becomes |
| 623 |
the parse result. |
| 624 |
|
| 625 |
=head1 Tainted data |
| 626 |
|
| 627 |
Marpa::R2 exists to allow its input to alter execution in |
| 628 |
flexible and powerful ways. |
| 629 |
Marpa should not be used with untrusted input. |
| 630 |
In Perl' s taint mode, it is a fatal error to use Marpa's SLIF |
| 631 |
interface |
| 632 |
with a tainted grammar, a tainted input string, |
| 633 |
or tainted token values. |
| 634 |
|
| 635 |
=head1 Threads |
| 636 |
|
| 637 |
When used in a thread-safe Perl, Marpa::R2 should be thread-safe, |
| 638 |
with one important restriction: |
| 639 |
All Marpa objects that share the same grammar must be created |
| 640 |
and used within a single thread. |
| 641 |
|
| 642 |
This restriction may be lifted someday, but |
| 643 |
in practice it does not seem onerous. |
| 644 |
Note that |
| 645 |
you can use |
| 646 |
the same grammar in different |
| 647 |
threads by creating grammars |
| 648 |
that are exact |
| 649 |
copies of each other, |
| 650 |
one grammar per thread. |
| 651 |
|
| 652 |
=head1 The Marpa:: namespace |
| 653 |
|
| 654 |
The C<Marpa::> top-level namespace is reserved. |
| 655 |
For extensions to Marpa, |
| 656 |
one appropriate place is the C<MarpaX::> namespace. |
| 657 |
This practice helps avoid namespace collisions, |
| 658 |
and follows a CPAN standard, as exemplified by |
| 659 |
the |
| 660 |
C<DBIx::> |
| 661 |
C<LWPx::> |
| 662 |
and |
| 663 |
C<MooseX::> |
| 664 |
which are for extensions of, respectively, |
| 665 |
DBI, LWP and Moose. |
| 666 |
|
| 667 |
=head1 Other documents |
| 668 |
|
| 669 |
This document gives a semi-tutorial overview of Marpa's Scanless interface (SLIF). |
| 670 |
For a continuation of this tutorial, which describes how to get finer control |
| 671 |
of Marpa and access more of its features, |
| 672 |
see |
| 673 |
L<the followup tutorial to this one|Marpa::R2::Tutorial2>. |
| 674 |
If you are beginner who wants to learn more about Marpa, |
| 675 |
you probably want to go next to |
| 676 |
L<the overview of the SLIF interface|Marpa::R2::Scanless>, |
| 677 |
and then the pages describing |
| 678 |
L<its DSL|Marpa::R2::Scanless::DSL>, |
| 679 |
L<its grammar methods|Marpa::R2::Scanless::G>, |
| 680 |
and |
| 681 |
L<its recognizer methods|Marpa::R2::Scanless::R>. |
| 682 |
|
| 683 |
Marpa has another interface. |
| 684 |
L<The thin |
| 685 |
interface|Marpa::R2::Advanced::Thin> |
| 686 |
provides direct access to the underlying |
| 687 |
Libmarpa C library. |
| 688 |
Of the Perl interfaces to Marpa, |
| 689 |
the thin interface is the most low-level. |
| 690 |
The thin interface offers efficient access to the full power of the |
| 691 |
Marpa parse engine, |
| 692 |
but it requires the application to do a lot of the work itself. |
| 693 |
|
| 694 |
L<Marpa::R2::Vocabulary> is intended as a quick refresher in |
| 695 |
parsing terminology, |
| 696 |
emphasizing how the standard terms are used |
| 697 |
in the Marpa context. |
| 698 |
Marpa's standard semantics are fully described in the |
| 699 |
L<Marpa::R2::Semantics> document. |
| 700 |
Techniques for tracing and for debugging your Marpa grammars |
| 701 |
are described in the |
| 702 |
L<Marpa::R2::Tracing> document and the |
| 703 |
L<Marpa::R2::Progress> document. |
| 704 |
For those with a theoretical bent, |
| 705 |
my sources, and other useful references, are described in |
| 706 |
L<Marpa::R2::Advanced::Bibliography>. |
| 707 |
|
| 708 |
=head1 Author |
| 709 |
|
| 710 |
Jeffrey Kegler |
| 711 |
|
| 712 |
=head2 Why is it called "Marpa"? |
| 713 |
|
| 714 |
Marpa is the name of the greatest of the Tibetan "translators". |
| 715 |
In his time (the 11th century AD) Indian Buddhism was |
| 716 |
at its height. |
| 717 |
Marpa's generation of scholars was devoted |
| 718 |
to producing Tibetan versions of Buddhism's Sanskrit scriptures. |
| 719 |
Marpa became the greatest of them, |
| 720 |
and today is known as Marpa Lotsawa: "Marpa the Translator". |
| 721 |
|
| 722 |
=head2 Blatant plug |
| 723 |
|
| 724 |
Marpa is a character in my novel, B<The God Proof>. |
| 725 |
B<The God |
| 726 |
Proof> centers around Kurt GE<ouml>del's proof of God's existence. |
| 727 |
Yes, I<that> Kurt GE<ouml>del, and yes, he really did work out a |
| 728 |
God Proof (it's in his I<Collected Works>, Vol. 3, pp. 403-404). |
| 729 |
B<The God Proof> is available |
| 730 |
as a free download (L<http://www.lulu.com/content/933192>). |
| 731 |
It can be purchased in print form at Amazon.com: |
| 732 |
L<http://www.amazon.com/God-Proof-Jeffrey-Kegler/dp/1434807355>. |
| 733 |
|
| 734 |
=head1 Support |
| 735 |
|
| 736 |
L<Marpa::R2> comes without warranty. |
| 737 |
Support is provided |
| 738 |
on a volunteer basis |
| 739 |
through the standard mechanisms for CPAN modules. |
| 740 |
L<The Support document|Marpa::R2::Support> has details. |
| 741 |
|
| 742 |
=head1 Copyright and License |
| 743 |
|
| 744 |
=for Marpa::R2::Display |
| 745 |
ignore: 1 |
| 746 |
|
| 747 |
Copyright 2022 Jeffrey Kegler |
| 748 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 749 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 750 |
General Public License as published by the Free Software Foundation, |
| 751 |
either version 3 of the License, or (at your option) any later version. |
| 752 |
|
| 753 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 754 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 755 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 756 |
Lesser General Public License for more details. |
| 757 |
|
| 758 |
You should have received a copy of the GNU Lesser |
| 759 |
General Public License along with Marpa::R2. If not, see |
| 760 |
http://www.gnu.org/licenses/. |
| 761 |
|
| 762 |
=for Marpa::R2::Display::End |
| 763 |
|
| 764 |
=cut |
| 765 |
|
| 766 |
# Local Variables: |
| 767 |
# mode: cperl |
| 768 |
# cperl-indent-level: 4 |
| 769 |
# fill-column: 100 |
| 770 |
# End: |
| 771 |
# vim: expandtab shiftwidth=4: |
| 772 |
|