| 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::Scanless::DSL - The DSL for the Scanless interface |
| 19 |
|
| 20 |
=head1 Synopsis |
| 21 |
|
| 22 |
=for Marpa::R2::Display |
| 23 |
name: SLIF DSL synopsis |
| 24 |
partial: 1 |
| 25 |
normalize-whitespace: 1 |
| 26 |
|
| 27 |
use Marpa::R2; |
| 28 |
|
| 29 |
my $grammar = Marpa::R2::Scanless::G->new( |
| 30 |
{ bless_package => 'My_Nodes', |
| 31 |
source => \(<<'END_OF_SOURCE'), |
| 32 |
:default ::= action => [values] bless => ::lhs |
| 33 |
lexeme default = action => [ start, length, value ] |
| 34 |
bless => ::name latm => 1 |
| 35 |
|
| 36 |
:start ::= Script |
| 37 |
Script ::= Expression+ separator => comma |
| 38 |
comma ~ [,] |
| 39 |
Expression ::= |
| 40 |
Number bless => primary |
| 41 |
| '(' Expression ')' bless => paren assoc => group |
| 42 |
|| Expression '**' Expression bless => exponentiate assoc => right |
| 43 |
|| Expression '*' Expression bless => multiply |
| 44 |
| Expression '/' Expression bless => divide |
| 45 |
|| Expression '+' Expression bless => add |
| 46 |
| Expression '-' Expression bless => subtract |
| 47 |
|
| 48 |
Number ~ [\d]+ |
| 49 |
:discard ~ whitespace |
| 50 |
whitespace ~ [\s]+ |
| 51 |
# allow comments |
| 52 |
:discard ~ <hash comment> |
| 53 |
<hash comment> ~ <terminated hash comment> | <unterminated |
| 54 |
final hash comment> |
| 55 |
<terminated hash comment> ~ '#' <hash comment body> <vertical space char> |
| 56 |
<unterminated final hash comment> ~ '#' <hash comment body> |
| 57 |
<hash comment body> ~ <hash comment char>* |
| 58 |
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 59 |
<hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 60 |
END_OF_SOURCE |
| 61 |
} |
| 62 |
); |
| 63 |
|
| 64 |
=for Marpa::R2::Display::End |
| 65 |
|
| 66 |
=head1 About this document |
| 67 |
|
| 68 |
This is the reference document for |
| 69 |
the domain-specific language (DSL) |
| 70 |
of Marpa's Scanless interface (SLIF). |
| 71 |
The SLIF's DSL is |
| 72 |
an extension of BNF. |
| 73 |
The SLIF DSL is used to specify other |
| 74 |
DSL's, and is therefore a "meta-DSL". |
| 75 |
|
| 76 |
=head1 Lexical conventions |
| 77 |
|
| 78 |
The SLIF source string consists of a series of rules, |
| 79 |
pseudo-rules and statements. |
| 80 |
Whitespace separates tokens, but is otherwise ignored. |
| 81 |
|
| 82 |
=head2 Comment |
| 83 |
|
| 84 |
A hash ("C<#>") character starts a comment, |
| 85 |
which continues to the end of the line. |
| 86 |
Comments are equivalent to whitespace. |
| 87 |
|
| 88 |
=head2 Symbol names |
| 89 |
|
| 90 |
Symbol names can be either "bare" or enclosed in angle brackets. |
| 91 |
Bare symbol names must consist entirely of Perl word characters |
| 92 |
(alphanumerics, plus the underscore). |
| 93 |
Symbol names are case-sensitive. |
| 94 |
|
| 95 |
The angle brackets, if used, serve to "quote" |
| 96 |
the symbol name, |
| 97 |
and will not be part of the explicit symbol name. |
| 98 |
|
| 99 |
If angle brackets are used, symbol names may also contain whitespace, |
| 100 |
as in |
| 101 |
|
| 102 |
=for Marpa::R2::Display |
| 103 |
ignore: 1 |
| 104 |
|
| 105 |
<op comma> |
| 106 |
|
| 107 |
=for Marpa::R2::Display::End |
| 108 |
|
| 109 |
A whitespace sequence inside angle brackets can |
| 110 |
include any whitespace character that is legal in Perl, |
| 111 |
including newlines. |
| 112 |
This allows very long symbol names to be line wrapped, |
| 113 |
if necessary. |
| 114 |
|
| 115 |
Unlike the angle brackets, |
| 116 |
the whitespace in a bracketed symbol token |
| 117 |
B<does> become part of |
| 118 |
the explicit symbol name, |
| 119 |
but it does so in a "normalized" form. |
| 120 |
Leading and trailing whitespace in the name is discarded, |
| 121 |
and all other whitespace sequences are converted to a single ASCII |
| 122 |
space character. |
| 123 |
This means that |
| 124 |
|
| 125 |
=for Marpa::R2::Display |
| 126 |
ignore: 1 |
| 127 |
|
| 128 |
< op comma > |
| 129 |
<op comma> |
| 130 |
< op comma> |
| 131 |
|
| 132 |
=for Marpa::R2::Display::End |
| 133 |
|
| 134 |
and even |
| 135 |
|
| 136 |
|
| 137 |
=for Marpa::R2::Display |
| 138 |
ignore: 1 |
| 139 |
|
| 140 |
<op |
| 141 |
comma> |
| 142 |
|
| 143 |
=for Marpa::R2::Display::End |
| 144 |
|
| 145 |
will all be regarded as the same symbol name. |
| 146 |
The explicit form of that symbol name |
| 147 |
is C<< <op comma> >>, except that, again, the |
| 148 |
angle brackets are for clarity, |
| 149 |
and are not part of the explicit name. |
| 150 |
|
| 151 |
Explicit, reserved and internal symbol names are often |
| 152 |
displayed between angle brackets, |
| 153 |
regardless of whether the symbol was originally |
| 154 |
specified in bracketed form. |
| 155 |
|
| 156 |
When a SLIF symbol needs to be |
| 157 |
referred to by name in Perl code, |
| 158 |
it is the symbol's explicit name that is used. |
| 159 |
|
| 160 |
=head2 Single-quoted strings |
| 161 |
|
| 162 |
=for Marpa::R2::Display |
| 163 |
name: SLIF DSL synopsis |
| 164 |
partial: 1 |
| 165 |
normalize-whitespace: 1 |
| 166 |
|
| 167 |
Expression ::= |
| 168 |
Number bless => primary |
| 169 |
| '(' Expression ')' bless => paren assoc => group |
| 170 |
|| Expression '**' Expression bless => exponentiate assoc => right |
| 171 |
|| Expression '*' Expression bless => multiply |
| 172 |
| Expression '/' Expression bless => divide |
| 173 |
|| Expression '+' Expression bless => add |
| 174 |
| Expression '-' Expression bless => subtract |
| 175 |
|
| 176 |
=for Marpa::R2::Display::End |
| 177 |
|
| 178 |
=for Marpa::R2::Display |
| 179 |
name: Case-insensitive characters examples |
| 180 |
partial: 1 |
| 181 |
normalize-whitespace: 1 |
| 182 |
|
| 183 |
Child ~ 'cHILd':i |
| 184 |
|
| 185 |
=for Marpa::R2::Display::End |
| 186 |
|
| 187 |
Single quotes can be used in prioritized |
| 188 |
rules to indicate character strings. |
| 189 |
The characters inside the single quote |
| 190 |
will be matched in the input, literally and one-for-one. |
| 191 |
Single-quoted strings can contain any characters with |
| 192 |
the exception of |
| 193 |
single quotes and vertical whitespace. |
| 194 |
|
| 195 |
Single-quoted strings do not allow "escaped" characters. |
| 196 |
A backslash ("C<\>") represents itself and has no effect |
| 197 |
on the interpretation of the next character. |
| 198 |
If a rule needs to match one of the forbidden characters |
| 199 |
(single quote or vertical whitespace), it must use a |
| 200 |
character class. |
| 201 |
|
| 202 |
Single-quoted strings are always interpreted at the |
| 203 |
L0 level, but they may be used in either structural |
| 204 |
or lexical rules. |
| 205 |
When a single-quoted string is used in a structural rule, |
| 206 |
Marpa creates a virtual L0 rule on behalf of the application. |
| 207 |
This is handy, but it does have a real disadvantage -- |
| 208 |
the name of the virtual rule's LHS |
| 209 |
will be one assigned automatically by Marpa. |
| 210 |
When tracing and debugging parses and grammars, |
| 211 |
these virtual LHS's can be harder for a programmer |
| 212 |
to interpret. |
| 213 |
|
| 214 |
A modifier can appear after the string. |
| 215 |
It must appear immediately after the string, |
| 216 |
with no intervening whitespace. |
| 217 |
Currently only the "C<:ic>" |
| 218 |
and "C<:i>" modifier are availables. |
| 219 |
These have exactly the same effect -- they make the string |
| 220 |
match case-insensitive. |
| 221 |
|
| 222 |
=head2 Character classes |
| 223 |
|
| 224 |
=for Marpa::R2::Display |
| 225 |
name: SLIF DSL synopsis |
| 226 |
partial: 1 |
| 227 |
normalize-whitespace: 1 |
| 228 |
|
| 229 |
<vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}] |
| 230 |
|
| 231 |
=for Marpa::R2::Display::End |
| 232 |
|
| 233 |
=for Marpa::R2::Display |
| 234 |
name: Case-insensitive characters examples |
| 235 |
partial: 1 |
| 236 |
normalize-whitespace: 1 |
| 237 |
|
| 238 |
word ~ [\w]:ic + |
| 239 |
|
| 240 |
=for Marpa::R2::Display::End |
| 241 |
|
| 242 |
A character class in square brackets ("C<[]>") |
| 243 |
can be used in a RHS alternative of a prioritized rule, |
| 244 |
a quantified rule or a discard pseudo-rule. |
| 245 |
Marpa character classes may contain anything acceptable to Perl, |
| 246 |
and follow the same escaping conventions as Perl's character classes. |
| 247 |
|
| 248 |
Character classes are always interpreted at the |
| 249 |
L0 level, but they may be used in either structural |
| 250 |
or lexical rules. |
| 251 |
When a character class is used in a structural rule, |
| 252 |
Marpa creates a virtual L0 rule on behalf of the application. |
| 253 |
This is handy, but it does have a real disadvantage -- |
| 254 |
the name of the virtual rule's LHS |
| 255 |
will be one assigned automatically by Marpa. |
| 256 |
When tracing and debugging parses and grammars, |
| 257 |
these virtual LHS's can be harder for a programmer |
| 258 |
to interpret. |
| 259 |
|
| 260 |
An implementation note: character classes are |
| 261 |
interpreted by Perl, but this involves minimal overhead |
| 262 |
when the parse is of any length. |
| 263 |
Each character class is |
| 264 |
passed to Perl to interpret exactly once and the result is |
| 265 |
memoized in a C language structure for future use. |
| 266 |
|
| 267 |
The modifiers allowed after single-quoted strings are also allowed allowed after character classes. |
| 268 |
Modifiers must appear immediately after the closing square bracket, |
| 269 |
with no intervening whitespace. |
| 270 |
For more details, see L<the section on single-quoted strings|/"Single-quoted strings">. |
| 271 |
|
| 272 |
=head2 Event name |
| 273 |
|
| 274 |
The name of an event may be either a bare name, |
| 275 |
a single-quoted event name, |
| 276 |
or an event pseudo-name. |
| 277 |
A bare event name must be one or more word characters, |
| 278 |
starting with an alphabetic character. |
| 279 |
|
| 280 |
A single-quoted event name may contain any character |
| 281 |
except a single quote or vertical space. |
| 282 |
The whitespace in single-quoted event names is normalized |
| 283 |
in similar fashion to the normalization of symbol names -- |
| 284 |
leading and trailing whitespace is removed, |
| 285 |
and all sequences of internal whitespace are changed to |
| 286 |
a single ASCII space character. |
| 287 |
Names containing single quotes (which, in any case, |
| 288 |
are impossible to add using |
| 289 |
current syntax) are reserved for future use. |
| 290 |
|
| 291 |
A event pseudo-name is a colon, |
| 292 |
followed by one or more word characters. |
| 293 |
As present, there is only one event pseudo-name, |
| 294 |
C<:symbol>. |
| 295 |
The C<:symbol> pseudo-name is only allowed in |
| 296 |
L<discard pseudo-rules|/"Discard pseudo-rule">, |
| 297 |
and in the |
| 298 |
L<discard default statement|/"Discard default statement">. |
| 299 |
|
| 300 |
If the name of an event is specified as |
| 301 |
the C<:symbol> pseudo-name, an event's name will be |
| 302 |
based on the RHS of its discard rule. |
| 303 |
This will always be either a single symbol, |
| 304 |
or a character class. |
| 305 |
If the RHS is a single symbol, |
| 306 |
the actual event name will be the name |
| 307 |
of that symbol. |
| 308 |
|
| 309 |
If the RHS is a character class, |
| 310 |
the actual event name will be the string |
| 311 |
specifying that character class in the DSL. |
| 312 |
The string specifying the event name for a character |
| 313 |
class is taken literally from the |
| 314 |
text specifying the DSL. |
| 315 |
This means that the character classes |
| 316 |
"C<[\x3B]>" and "C<[:]>" will have two different |
| 317 |
event names, |
| 318 |
event though both classes |
| 319 |
specify exactly the same set of characters. |
| 320 |
|
| 321 |
=head1 Event initializer |
| 322 |
|
| 323 |
=for Marpa::R2::Display |
| 324 |
name: SLIF completed event statement synopsis |
| 325 |
partial: 1 |
| 326 |
normalize-whitespace: 1 |
| 327 |
|
| 328 |
event 'a' = completed A |
| 329 |
event 'b'=off = completed B |
| 330 |
event 'c'=on = completed C |
| 331 |
|
| 332 |
=for Marpa::R2::Display::End |
| 333 |
|
| 334 |
=for Marpa::R2::Display |
| 335 |
name: SLIF discard event statement synopsis 2 |
| 336 |
partial: 1 |
| 337 |
normalize-whitespace: 1 |
| 338 |
|
| 339 |
:discard ~ [,] event => comma=off |
| 340 |
:discard ~ [;] event => 'semicolon'=on |
| 341 |
:discard ~ [.] event => period |
| 342 |
|
| 343 |
=for Marpa::R2::Display::End |
| 344 |
|
| 345 |
=for Marpa::R2::Display |
| 346 |
name: SLIF nulled event statement synopsis |
| 347 |
partial: 1 |
| 348 |
normalize-whitespace: 1 |
| 349 |
|
| 350 |
event '!a' = nulled A |
| 351 |
event '!b'=off = nulled B |
| 352 |
event '!c'=on = nulled C |
| 353 |
|
| 354 |
=for Marpa::R2::Display::End |
| 355 |
|
| 356 |
An event initializer is an event name, |
| 357 |
optionally with an explicit initialization. |
| 358 |
If there is an explicit initialization, |
| 359 |
it consists of the equal sign ('C<=>') |
| 360 |
followed by a value indicating |
| 361 |
the event's initial activation setting |
| 362 |
(C<on> or C<off>). |
| 363 |
|
| 364 |
If the initialization value is C<on>, |
| 365 |
or if there is no explicit initialization, |
| 366 |
the event's activation |
| 367 |
setting is initially on. |
| 368 |
If the initialization value is C<off>, |
| 369 |
the event's activation |
| 370 |
setting is initially off. |
| 371 |
|
| 372 |
=head1 L0, G1 and lexemes |
| 373 |
|
| 374 |
In reading this document, it is important to keep in mind |
| 375 |
the distinction, on one hand, between L0 and G1 rules and, |
| 376 |
on the other hand, between rules and lexemes. |
| 377 |
G1 rules have a semantics, |
| 378 |
which can be specified as described |
| 379 |
in this document. |
| 380 |
L0 rules simply recognize symbols in the input. |
| 381 |
L0 rules do not have a semantics. |
| 382 |
|
| 383 |
Top-level L0 rules correspond to a string in the input. |
| 384 |
The top-level L0 rules are seen by G1 as lexemes, |
| 385 |
and the string to which a top-level L0 rule |
| 386 |
corresponds becomes the default value of the lexeme. |
| 387 |
The L0 grammar can be thought of as similar |
| 388 |
in behavior to a set of regular expressions |
| 389 |
with the lexemes being seen as similar to named captures. |
| 390 |
|
| 391 |
Lexemes are the symbols which form the interface between |
| 392 |
G1 and L0. Lexemes, like G1 rules, have a semantics. |
| 393 |
The semantics of lexemes is |
| 394 |
specified separately from the |
| 395 |
semantics of G1 rules, |
| 396 |
as described below. |
| 397 |
|
| 398 |
=head1 Statements |
| 399 |
|
| 400 |
The SLIF DSL consists of a series of statements. |
| 401 |
The statements are of three kinds, as indicated by their declarator: |
| 402 |
|
| 403 |
=over 4 |
| 404 |
|
| 405 |
=item * G1 rule |
| 406 |
|
| 407 |
The BNF operator ("C<::=>"), |
| 408 |
coming between the LHS and the first RHS alternative of a rule, |
| 409 |
indicates that the rule is |
| 410 |
a G1 rule. |
| 411 |
|
| 412 |
=item * L0 rule |
| 413 |
|
| 414 |
The match operator ("C<~>"), |
| 415 |
coming between the LHS and the first RHS alternative |
| 416 |
of a rule, |
| 417 |
indicates a L0 rule. |
| 418 |
|
| 419 |
=item * Global statements |
| 420 |
|
| 421 |
Global statements |
| 422 |
are signified by the assignment operator ("C<=>"). |
| 423 |
The location of a statement in the DSL source |
| 424 |
will never affect the result. |
| 425 |
|
| 426 |
=back |
| 427 |
|
| 428 |
Rules differ from statements in that |
| 429 |
the effect of a rule |
| 430 |
is sometimes lexical -- |
| 431 |
that is, the effect may vary depending |
| 432 |
on the position of the rule in the DSL source. |
| 433 |
Some rules are called pseudo-rules. |
| 434 |
Pseudo-rules do not correspond to BNF rules, |
| 435 |
but instead use the rule format as a convenient |
| 436 |
way to express other information. |
| 437 |
|
| 438 |
=head2 The structure of rules |
| 439 |
|
| 440 |
Every rule declaration consists of, in order: |
| 441 |
|
| 442 |
=over 4 |
| 443 |
|
| 444 |
=item * A left hand side (LHS). |
| 445 |
This will be a |
| 446 |
symbol or a pseudo-symbol. |
| 447 |
|
| 448 |
=item * A declaration operator ("C<::=>" or "C<~>"). |
| 449 |
|
| 450 |
=item * A right side declaration, which contains one or more RHS |
| 451 |
alternatives. |
| 452 |
Details of the right side declaration vary by the type of rule. |
| 453 |
For each type of rule, |
| 454 |
the right side declaration is described in detail below. |
| 455 |
|
| 456 |
=back |
| 457 |
|
| 458 |
=head2 RHS alternatives |
| 459 |
|
| 460 |
The right side declaration of a rule will often contain one or |
| 461 |
more RHS alternatives. |
| 462 |
A RHS alternative is a series of RHS primaries, |
| 463 |
where a RHS primary may be a symbol name, |
| 464 |
a character class, |
| 465 |
or a single-quoted string. |
| 466 |
A list of one or more adverbs is often |
| 467 |
associated with the |
| 468 |
RHS alternatives. |
| 469 |
Each adverb consists of a keyword, |
| 470 |
the adverb operator ("C<< => >>"), |
| 471 |
and the adverb's value. |
| 472 |
|
| 473 |
Within an alternative, primaries may be enclosed in parentheses. |
| 474 |
A primary enclosed in parentheses is hidden from |
| 475 |
L<Marpa's semantics|Marpa::R2::Semantics>. |
| 476 |
A set of parentheses may contain more than one primary, |
| 477 |
in which case the entire sequence of primaries is hidden, |
| 478 |
as if they had been enclosed in parentheses individually. |
| 479 |
"Hiding" primaries in this way can be convenient for primaries whose values |
| 480 |
the semantics will ignore, perhaps because the value is constant. |
| 481 |
|
| 482 |
For example, |
| 483 |
in the following rule |
| 484 |
|
| 485 |
=for Marpa::R2::Display |
| 486 |
ignore: 1 |
| 487 |
|
| 488 |
a ::= b (',' c) d action => ::first |
| 489 |
|
| 490 |
=for Marpa::R2::Display::End |
| 491 |
|
| 492 |
there is |
| 493 |
|
| 494 |
=over 4 |
| 495 |
|
| 496 |
=item * |
| 497 |
|
| 498 |
A LHS, in this case the symbol "C<a>". |
| 499 |
|
| 500 |
=item * |
| 501 |
|
| 502 |
A declarator, "C<::=>", which indicates this is |
| 503 |
a G1 rule. |
| 504 |
|
| 505 |
=item * |
| 506 |
|
| 507 |
A RHS alternative consisting of four RHS primaries. |
| 508 |
The first RHS primary is the symbol "C<b>". |
| 509 |
The second RHS primary is a short single-quoted string C<','>. |
| 510 |
The third and fourth RHS primaries are symbols: "C<c>" and "C<d>". |
| 511 |
The parentheses around the second and third RHS primaries "hide" |
| 512 |
them from the semantics. |
| 513 |
Marpa's semantics will see this as a rule with only two RHS values. |
| 514 |
|
| 515 |
=item * |
| 516 |
|
| 517 |
The adverb list associated with the RHS alternative, |
| 518 |
consisting of a single adverb. |
| 519 |
The adverb consists of its keyword "L<C<action>|/"action">", |
| 520 |
followed by the adverb operator ("C<< => >>"), |
| 521 |
and the adverb's value "C<::first>". |
| 522 |
|
| 523 |
=back |
| 524 |
|
| 525 |
The rule in the above example is one of a very common type: |
| 526 |
a trivial prioritized rule. |
| 527 |
A prioritized rule is one that contains one or more prioritized RHS alternatives. |
| 528 |
Prioritized rules are the only rules which may contain more than one RHS |
| 529 |
alternative, but even prioritized rules usually have only one RHS alternative. |
| 530 |
If there is only one RHS alternative, as in this case, the prioritization |
| 531 |
is B<trivial> -- |
| 532 |
there is only one priority. |
| 533 |
|
| 534 |
=head2 Start rule |
| 535 |
|
| 536 |
=for Marpa::R2::Display |
| 537 |
name: SLIF DSL synopsis |
| 538 |
partial: 1 |
| 539 |
normalize-whitespace: 1 |
| 540 |
|
| 541 |
:start ::= Script |
| 542 |
|
| 543 |
=for Marpa::R2::Display::End |
| 544 |
|
| 545 |
By default, the start symbol of the grammar is the LHS of the |
| 546 |
first G1 rule. |
| 547 |
This default can be make explicit or overriden by using an explicit |
| 548 |
start rule. |
| 549 |
The LHS of this rule is the C<:start> pseudo-symbol. |
| 550 |
Only one RHS alternative is allowed. |
| 551 |
This RHS alternative must contain only one symbol name, |
| 552 |
and that symbol will be the start symbol of the G1 grammar. |
| 553 |
No adverbs should be associated with the RHS alternative. |
| 554 |
Start rules must be G1 rules. |
| 555 |
|
| 556 |
=head2 Empty rule |
| 557 |
|
| 558 |
An empty rule is a rule with an empty RHS. |
| 559 |
The empty RHS, technically, is a RHS alternative, one with zero RHS primaries. |
| 560 |
The L<C<action>|/"action"> and |
| 561 |
L<C<bless>|/"bless"> adverbs are allowed |
| 562 |
for the empty RHS alternative, |
| 563 |
but no others. |
| 564 |
A empty rule makes its LHS symbol a nullable symbol. |
| 565 |
|
| 566 |
=head2 Quantified rule |
| 567 |
|
| 568 |
=for Marpa::R2::Display |
| 569 |
name: SLIF DSL synopsis |
| 570 |
partial: 1 |
| 571 |
normalize-whitespace: 1 |
| 572 |
|
| 573 |
Script ::= Expression+ separator => comma |
| 574 |
|
| 575 |
=for Marpa::R2::Display::End |
| 576 |
|
| 577 |
A quantified rule has only one RHS alternative, |
| 578 |
which is followed by a quantifier. |
| 579 |
The RHS alternative must consist of a single RHS primary. |
| 580 |
This RHS primary must be a symbol name or a character class. |
| 581 |
The quantifer is either |
| 582 |
a star ("C<*>"), |
| 583 |
or a plus sign ("C<+>") |
| 584 |
indicating, respectively, that the sequence rule has a minimum length |
| 585 |
of 0 or 1. |
| 586 |
|
| 587 |
Adverbs may be associated with the RHS alternative. |
| 588 |
The adverb list must follow the quantifier. |
| 589 |
The adverbs allowed are L<C<action>|/"action">, |
| 590 |
L<C<bless>|/"bless">, |
| 591 |
L<C<proper>|/"proper"> and |
| 592 |
L<C<separator>|/"separator">. |
| 593 |
|
| 594 |
=head2 Prioritized rule |
| 595 |
|
| 596 |
=for Marpa::R2::Display |
| 597 |
name: SLIF DSL synopsis |
| 598 |
partial: 1 |
| 599 |
normalize-whitespace: 1 |
| 600 |
|
| 601 |
Expression ::= |
| 602 |
Number bless => primary |
| 603 |
| '(' Expression ')' bless => paren assoc => group |
| 604 |
|| Expression '**' Expression bless => exponentiate assoc => right |
| 605 |
|| Expression '*' Expression bless => multiply |
| 606 |
| Expression '/' Expression bless => divide |
| 607 |
|| Expression '+' Expression bless => add |
| 608 |
| Expression '-' Expression bless => subtract |
| 609 |
|
| 610 |
=for Marpa::R2::Display::End |
| 611 |
|
| 612 |
A prioritized rule contains a series of one or more RHS alternatives, |
| 613 |
separated by either the alternation operator ("C<|>") |
| 614 |
or the loosen operators ("C<||>"). |
| 615 |
In a typical grammar, most rules are prioritized rules, |
| 616 |
but they are often trivially prioritized, |
| 617 |
consisting of only one RHS alternative. |
| 618 |
For brevity, RHS alternatives are often called B<alternatives>. |
| 619 |
|
| 620 |
Each alternative may be followed by a list of associated adverbs. |
| 621 |
The |
| 622 |
L<C<action>|/"action">, |
| 623 |
L<C<assoc>|/"assoc"> and |
| 624 |
L<C<bless>|/"bless"> adverbs are allowed. |
| 625 |
|
| 626 |
The RHS alternatives in a prioritized right hand side proceed |
| 627 |
from tightest (highest) priority to loosest. |
| 628 |
The double "or" symbol ("C<||>") is the "loosen" operator -- |
| 629 |
the alternatives after it |
| 630 |
have a looser (lower) priority than the alternatives before it. |
| 631 |
The single "or" symbol ("C<|>") is the ordinary "alternative" operator -- |
| 632 |
alternatives on each side of it have the same priority. |
| 633 |
Associativity is specified using adverbs, as described below. |
| 634 |
|
| 635 |
These rules are also called "precedenced" rules. |
| 636 |
The term "precedenced" |
| 637 |
has an advantage -- |
| 638 |
it is much |
| 639 |
less overloaded than |
| 640 |
the term "prioritized". |
| 641 |
|
| 642 |
By design, a precedenced rule expresses precedence |
| 643 |
for all rules with the same LHS. |
| 644 |
Accordingly, if a symbol appears on the LHS of a precedenced |
| 645 |
rule, it should not be the LHS of any other rule. |
| 646 |
If two precedenced rules have the same LHS, they will be |
| 647 |
considered to be duplicate rules, |
| 648 |
and that duplication will be reported as a fatal error. |
| 649 |
|
| 650 |
=head2 Discard pseudo-rule |
| 651 |
|
| 652 |
=for Marpa::R2::Display |
| 653 |
name: SLIF DSL synopsis |
| 654 |
partial: 1 |
| 655 |
normalize-whitespace: 1 |
| 656 |
|
| 657 |
:discard ~ whitespace |
| 658 |
|
| 659 |
=for Marpa::R2::Display::End |
| 660 |
|
| 661 |
=for Marpa::R2::Display |
| 662 |
name: SLIF discard event synopsis |
| 663 |
partial: 1 |
| 664 |
normalize-whitespace: 1 |
| 665 |
|
| 666 |
:discard ~ whitespace event => ws |
| 667 |
|
| 668 |
=for Marpa::R2::Display::End |
| 669 |
|
| 670 |
A discard pseudo-rule is a rule whose LHS is |
| 671 |
the C<:discard> pseudo-symbol, |
| 672 |
and which has only one RHS alternative. |
| 673 |
The RHS alternative must contain |
| 674 |
|
| 675 |
=over 4 |
| 676 |
|
| 677 |
=item * |
| 678 |
|
| 679 |
exactly one symbol name, or |
| 680 |
|
| 681 |
=item * |
| 682 |
|
| 683 |
exactly one character class. |
| 684 |
|
| 685 |
=back |
| 686 |
|
| 687 |
The symbol specified by the RHS of a discard pseudo-rule |
| 688 |
is called the B<discarded symbol>. |
| 689 |
Discard pseudo-rules indicate that the discarded symbol is a top-level L0 |
| 690 |
symbol, but one which is not a lexeme. |
| 691 |
When a discarded symbol is recognized, |
| 692 |
it is not passed as a lexeme to the G1 parser, but is |
| 693 |
(as the name suggests) discarded. |
| 694 |
Discard pseudo-rules must be L0 rules. |
| 695 |
|
| 696 |
Only the C<event> adverb is allowed. |
| 697 |
Its value must be an event initializer. |
| 698 |
The format of an event initializer is |
| 699 |
described |
| 700 |
L<above|/"Event initializer">. |
| 701 |
If present, it defines a discard event, |
| 702 |
as described in |
| 703 |
L<the document on SLIF parse |
| 704 |
events|Marpa::R2::Event/"Discard events">. |
| 705 |
|
| 706 |
=head2 Default pseudo-rule |
| 707 |
|
| 708 |
=for Marpa::R2::Display |
| 709 |
name: SLIF DSL synopsis |
| 710 |
partial: 1 |
| 711 |
normalize-whitespace: 1 |
| 712 |
|
| 713 |
:default ::= action => [values] bless => ::lhs |
| 714 |
|
| 715 |
=for Marpa::R2::Display::End |
| 716 |
|
| 717 |
=for Marpa::R2::Display |
| 718 |
name: SLIF "time flies" DSL synopsis |
| 719 |
partial: 1 |
| 720 |
normalize-whitespace: 1 |
| 721 |
|
| 722 |
:default ::= action => [ name, values ] |
| 723 |
|
| 724 |
=for Marpa::R2::Display::End |
| 725 |
|
| 726 |
The purpose of |
| 727 |
the default pseudo-rule is to change the defaults for |
| 728 |
rule adverbs. |
| 729 |
Technically, it has one RHS alternative, but this must always contain |
| 730 |
zero RHS primaries. |
| 731 |
Default pseudo-rules do not affect the defaults for L0 rules |
| 732 |
or for lexemes. |
| 733 |
There may be more than one default pseudo-rule. |
| 734 |
The scope of default pseudo-rules is lexical, applying only to rules |
| 735 |
that appear afterwards in the DSL source. |
| 736 |
|
| 737 |
Currently only the L<C<action>|/"action"> and L<C<bless>|/"bless"> adverbs |
| 738 |
can be specified in a default pseudo-rule. |
| 739 |
Each default pseudo-rule creates a completely new |
| 740 |
set of defaults -- if an adverb is not specified, |
| 741 |
the default is reset to its implicit value, |
| 742 |
the value which it had prior to any explicit settings. |
| 743 |
|
| 744 |
=head2 Lexeme pseudo-rule |
| 745 |
|
| 746 |
=for Marpa::R2::Display |
| 747 |
name: SLIF lexeme rule synopsis |
| 748 |
partial: 1 |
| 749 |
normalize-whitespace: 1 |
| 750 |
|
| 751 |
:lexeme ~ <say keyword> priority => 1 |
| 752 |
|
| 753 |
=for Marpa::R2::Display::End |
| 754 |
|
| 755 |
The purpose of |
| 756 |
the C<:lexeme> pseudo-rule is to allow adverbs to |
| 757 |
change the treatment of a lexeme. |
| 758 |
This pseudo-rule always has exactly one RHS alternative, |
| 759 |
and that RHS alternative must contain exactly one symbol. |
| 760 |
This RHS symbol identifies the lexeme which the adverbs will affect. |
| 761 |
The only adverbs allowed in a C<:lexeme> rule |
| 762 |
are |
| 763 |
L<C<event>|/"event">, |
| 764 |
L<C<pause>|/"pause">, |
| 765 |
and |
| 766 |
L<C<priority>|/"priority">. |
| 767 |
|
| 768 |
As a side effect, a C<:lexeme> pseudo-rule |
| 769 |
declares that its RHS symbol is expected to be a lexeme. |
| 770 |
This declaration does not "force" lexeme status -- |
| 771 |
if the symbol does not meet the criteria for a lexeme |
| 772 |
based on its use in L0 and G1 rules, |
| 773 |
the result will be a fatal error. |
| 774 |
Applications may find this ability to "declare" |
| 775 |
lexemes useful for debugging, |
| 776 |
and for documenting grammars. |
| 777 |
|
| 778 |
=head2 Discard default statement |
| 779 |
|
| 780 |
=for Marpa::R2::Display |
| 781 |
name: SLIF default discard event statement synopsis 1 |
| 782 |
partial: 1 |
| 783 |
normalize-whitespace: 1 |
| 784 |
|
| 785 |
discard default = event => :symbol=on |
| 786 |
|
| 787 |
=for Marpa::R2::Display::End |
| 788 |
|
| 789 |
=for Marpa::R2::Display |
| 790 |
name: SLIF default discard event statement synopsis 2 |
| 791 |
partial: 1 |
| 792 |
normalize-whitespace: 1 |
| 793 |
|
| 794 |
discard default = event => :symbol |
| 795 |
|
| 796 |
=for Marpa::R2::Display::End |
| 797 |
|
| 798 |
The discard default statement changes the defaults for |
| 799 |
discard pseudo-rules. |
| 800 |
Only the default for |
| 801 |
the L<C<event>|/"event"> |
| 802 |
adverb |
| 803 |
can be specified in a lexeme default statement. |
| 804 |
Only one discard default statement is allowed in a grammar. |
| 805 |
|
| 806 |
Typically in a discard default statement, |
| 807 |
the event name will be the pseudo-name |
| 808 |
C<:symbol>. |
| 809 |
For details about event pseudo-names, |
| 810 |
see L<the section on event |
| 811 |
names|/"Event name">. |
| 812 |
|
| 813 |
=head2 Lexeme default statement |
| 814 |
|
| 815 |
=for Marpa::R2::Display |
| 816 |
name: SLIF DSL synopsis |
| 817 |
partial: 1 |
| 818 |
normalize-whitespace: 1 |
| 819 |
|
| 820 |
lexeme default = action => [ start, length, value ] |
| 821 |
bless => ::name latm => |
| 822 |
|
| 823 |
=for Marpa::R2::Display::End |
| 824 |
|
| 825 |
=for Marpa::R2::Display |
| 826 |
name: SLIF "time flies" DSL synopsis |
| 827 |
partial: 1 |
| 828 |
normalize-whitespace: 1 |
| 829 |
|
| 830 |
lexeme default = action => [ name, value ] |
| 831 |
|
| 832 |
=for Marpa::R2::Display::End |
| 833 |
|
| 834 |
The lexeme default statement changes the defaults for |
| 835 |
lexeme adverbs. |
| 836 |
It only changes the defaults for lexemes, |
| 837 |
and does not affect rules. |
| 838 |
Only the defaults for |
| 839 |
the L<C<action>|/"action">, |
| 840 |
L<C<bless>|/"bless">, |
| 841 |
and L<C<latm>|/"latm"> |
| 842 |
adverbs |
| 843 |
can be specified in a lexeme default statement. |
| 844 |
Only one lexeme default statement is allowed in a grammar. |
| 845 |
|
| 846 |
=head2 Named event statement |
| 847 |
|
| 848 |
=for Marpa::R2::Display |
| 849 |
name: SLIF completed event statement synopsis |
| 850 |
partial: 1 |
| 851 |
normalize-whitespace: 1 |
| 852 |
|
| 853 |
event 'a' = completed A |
| 854 |
event 'b'=off = completed B |
| 855 |
event 'c'=on = completed C |
| 856 |
event 'd' = completed D |
| 857 |
|
| 858 |
=for Marpa::R2::Display::End |
| 859 |
|
| 860 |
=for Marpa::R2::Display |
| 861 |
name: SLIF nulled event statement synopsis |
| 862 |
partial: 1 |
| 863 |
normalize-whitespace: 1 |
| 864 |
|
| 865 |
event '!a' = nulled A |
| 866 |
event '!b'=off = nulled B |
| 867 |
event '!c'=on = nulled C |
| 868 |
event '!d' = nulled D |
| 869 |
|
| 870 |
=for Marpa::R2::Display::End |
| 871 |
|
| 872 |
=for Marpa::R2::Display |
| 873 |
name: SLIF predicted event statement synopsis |
| 874 |
partial: 1 |
| 875 |
normalize-whitespace: 1 |
| 876 |
|
| 877 |
event '^a' = predicted A |
| 878 |
event '^b'=off = predicted B |
| 879 |
event '^c'=on = predicted C |
| 880 |
event '^d' = predicted D |
| 881 |
|
| 882 |
=for Marpa::R2::Display::End |
| 883 |
|
| 884 |
The named event statement sets up a |
| 885 |
SLIF parse event. |
| 886 |
A named event statement consists of, in order |
| 887 |
|
| 888 |
=over 4 |
| 889 |
|
| 890 |
=item * |
| 891 |
|
| 892 |
The C<event> keyword. |
| 893 |
|
| 894 |
=item * |
| 895 |
|
| 896 |
An event initializer, |
| 897 |
as described in L<the section on |
| 898 |
event initializers|/"Event initializer">. |
| 899 |
|
| 900 |
=item * |
| 901 |
|
| 902 |
An equal sign ('C<=>'). |
| 903 |
|
| 904 |
=item * |
| 905 |
|
| 906 |
A keyword, which is one of C<completed>, |
| 907 |
C<nulled>, or C<predicted>, |
| 908 |
to indicate the event type. |
| 909 |
|
| 910 |
=item * |
| 911 |
|
| 912 |
A symbol name. |
| 913 |
|
| 914 |
=back |
| 915 |
|
| 916 |
The SLIF's event-triggering methods are |
| 917 |
L<the Scanless recognizer's read()|Marpa::R2::Scanless::R/"read()">, |
| 918 |
L<resume()|Marpa::R2::Scanless::R/"resume()">, |
| 919 |
L<lexeme_complete()|Marpa::R2::Scanless::R/"lexeme_complete()">, |
| 920 |
and L<lexeme_read()|Marpa::R2::Scanless::R/"lexeme_read()">. |
| 921 |
If the condition described |
| 922 |
by the named event statement |
| 923 |
occurs during an event-triggering method, |
| 924 |
the method will return immediately, |
| 925 |
with the current location at the trigger location. |
| 926 |
Once triggered, |
| 927 |
named events may be queried using |
| 928 |
L<the Scanless recognizer's events() |
| 929 |
method|Marpa::R2::Scanless::R/"events()">. |
| 930 |
For details, see |
| 931 |
L<the document on SLIF parse |
| 932 |
events|Marpa::R2::Event>. |
| 933 |
|
| 934 |
=head2 Inaccessible symbol statement |
| 935 |
|
| 936 |
=for Marpa::R2::Display |
| 937 |
name: inaccessible is ok statement |
| 938 |
partial: 1 |
| 939 |
normalize-whitespace: 1 |
| 940 |
|
| 941 |
inaccessible is ok by default |
| 942 |
|
| 943 |
=for Marpa::R2::Display::End |
| 944 |
|
| 945 |
=for Marpa::R2::Display |
| 946 |
name: inaccessible is fatal statement |
| 947 |
partial: 1 |
| 948 |
normalize-whitespace: 1 |
| 949 |
|
| 950 |
inaccessible is fatal by default |
| 951 |
|
| 952 |
=for Marpa::R2::Display::End |
| 953 |
|
| 954 |
Inaccessible symbols are symbols |
| 955 |
which cannot be reached from the start symbol. |
| 956 |
Often, they |
| 957 |
are the result of an error in grammar writing. |
| 958 |
But inaccessible symbols can also occur for legitimate reasons -- |
| 959 |
for example, |
| 960 |
you may have rules and symbols in grammar intended for future use. |
| 961 |
|
| 962 |
The default can be specified |
| 963 |
or changed with a statement of the form: |
| 964 |
|
| 965 |
=for Marpa::R2::Display |
| 966 |
ignore: 1 |
| 967 |
|
| 968 |
inaccessible is TREATMENT by default |
| 969 |
|
| 970 |
=for Marpa::R2::Display |
| 971 |
|
| 972 |
where C<TREATMENT> is one of C<warn>, C<ok>, or C<fatal>. |
| 973 |
|
| 974 |
C<fatal> indicates that an inaccessible symbol should be |
| 975 |
a fatal error. |
| 976 |
C<warn> indicates that Marpa should print a warning message, |
| 977 |
but proceed with the parse. |
| 978 |
C<warn> is the default. |
| 979 |
C<ok> indicates that the parse should proceed without |
| 980 |
warning messages. |
| 981 |
|
| 982 |
=head1 Ambiguity |
| 983 |
|
| 984 |
Marpa parses ambiguous grammars and the design of the SLIF exploits this. |
| 985 |
A flexible, but potentially ambiguous, syntax is used. |
| 986 |
Actual ambiguities are obvious to the human eye, |
| 987 |
and users will create them, |
| 988 |
so that |
| 989 |
the techniques of this section |
| 990 |
will rarely be needed. |
| 991 |
|
| 992 |
If and when an actual ambiguity does occur, an error message reports |
| 993 |
the ambiguity and its exact location. |
| 994 |
It will always be possible to disambiguate a SLIF DSL, |
| 995 |
and there will always |
| 996 |
be more than one way to do this. |
| 997 |
|
| 998 |
=head2 Separating statements with semicolons |
| 999 |
|
| 1000 |
=for Marpa::R2::Display |
| 1001 |
name: statements separted by semicolon |
| 1002 |
partial: 1 |
| 1003 |
normalize-whitespace: 1 |
| 1004 |
|
| 1005 |
:default ::= action => ::array |
| 1006 |
quartet ::= a a a a; |
| 1007 |
inaccessible is warn by default |
| 1008 |
a ~ 'a' |
| 1009 |
|
| 1010 |
=for Marpa::R2::Display::End |
| 1011 |
|
| 1012 |
A statement may be terminated with a semicolon ("C<;>"). |
| 1013 |
|
| 1014 |
=head2 Grouping statements in curly braces |
| 1015 |
|
| 1016 |
=for Marpa::R2::Display |
| 1017 |
name: statements grouped in curly braces |
| 1018 |
partial: 1 |
| 1019 |
normalize-whitespace: 1 |
| 1020 |
|
| 1021 |
{ |
| 1022 |
:default ::= action => ::array |
| 1023 |
quartet ::= a a a a |
| 1024 |
} |
| 1025 |
inaccessible is warn by default |
| 1026 |
a ~ 'a' |
| 1027 |
|
| 1028 |
=for Marpa::R2::Display::End |
| 1029 |
|
| 1030 |
Statements can be grouped, using curly braces. |
| 1031 |
These do B<not> create scopes -- the curly braces |
| 1032 |
serve merely to group and to separate groups of |
| 1033 |
statements. |
| 1034 |
|
| 1035 |
=head2 Other ways to disambiguate |
| 1036 |
|
| 1037 |
There are many other ways to disambiguate SLIF statements. |
| 1038 |
If the ambiguity is between keywords and symbol names, |
| 1039 |
enclosing a symbol name in angle brackets will force it to be |
| 1040 |
treated only as a symbol name. |
| 1041 |
And while it is never necessary, statements can be re-ordered. |
| 1042 |
|
| 1043 |
=head1 Adverbs |
| 1044 |
|
| 1045 |
Adverbs consist of a keyword, the adverb operator ("C<< => >>"), |
| 1046 |
and the adverb's value. |
| 1047 |
The keyword must be one of those described in this section. |
| 1048 |
The adverb's value must be as described for each keyword. |
| 1049 |
|
| 1050 |
=head2 action |
| 1051 |
|
| 1052 |
The C<action> adverb is allowed for |
| 1053 |
|
| 1054 |
=over 4 |
| 1055 |
|
| 1056 |
=item * |
| 1057 |
|
| 1058 |
An RHS alternative, in which the action is for the alternative. |
| 1059 |
|
| 1060 |
=item * |
| 1061 |
|
| 1062 |
The default pseudo-rule, in which case the action is for all rules which do not |
| 1063 |
have their own action explicitly specified. |
| 1064 |
|
| 1065 |
=item * |
| 1066 |
|
| 1067 |
The lexeme default statement, in which case the action is for all lexemes. |
| 1068 |
|
| 1069 |
=back |
| 1070 |
|
| 1071 |
The C<action> adverb is not allowed for L0 rules. |
| 1072 |
The possible values of actions are described, |
| 1073 |
along with other details of the semantics, in |
| 1074 |
L<a separate document|Marpa::R2::Semantics>. |
| 1075 |
|
| 1076 |
=head2 assoc |
| 1077 |
|
| 1078 |
The C<assoc> adverb is only valid in a prioritized rule. |
| 1079 |
Its value must be one of |
| 1080 |
C<left>, |
| 1081 |
C<right> or |
| 1082 |
C<group>. |
| 1083 |
C<left> is the default. |
| 1084 |
The effect |
| 1085 |
of the C<assoc> adverb |
| 1086 |
will be as described |
| 1087 |
L<below|/"Precedence">. |
| 1088 |
|
| 1089 |
=head2 bless |
| 1090 |
|
| 1091 |
The C<bless> adverb causes the result of the semantics to be |
| 1092 |
blessed into the class indicated by the value of the adverb. |
| 1093 |
Details of its use may be found in |
| 1094 |
L<the semantics |
| 1095 |
document|Marpa::R2::Semantics/"Blessings">. |
| 1096 |
|
| 1097 |
=head2 event |
| 1098 |
|
| 1099 |
=for Marpa::R2::Display |
| 1100 |
name: SLIF named lexeme event synopsis |
| 1101 |
normalize-whitespace: 1 |
| 1102 |
|
| 1103 |
:lexeme ~ <a> pause => before event => 'before a' |
| 1104 |
:lexeme ~ <b> pause => after event => 'after b'=on |
| 1105 |
:lexeme ~ <c> pause => before event => 'before c'=off |
| 1106 |
:lexeme ~ <d> pause => after event => 'after d' |
| 1107 |
|
| 1108 |
=for Marpa::R2::Display::End |
| 1109 |
|
| 1110 |
The C<event> adverb applies only to lexemes and |
| 1111 |
is only allowed in |
| 1112 |
L<a C<:lexeme> pseudo-rule|/"Lexeme pseudo-rule">. |
| 1113 |
It names the event specified by |
| 1114 |
the L<C<pause>|/"pause"> adverb. |
| 1115 |
It is a fatal error to specify the C<event> adverb |
| 1116 |
if the L<C<pause>|/"pause"> adverb is not also specified. |
| 1117 |
|
| 1118 |
The value of the C<event> adverb is an event |
| 1119 |
initializer. |
| 1120 |
Event initializers are as described |
| 1121 |
L<above|/"Event initializer">. |
| 1122 |
|
| 1123 |
When an event declared with the |
| 1124 |
the L<C<pause>|/"pause"> adverb |
| 1125 |
is not named using the C<event> adverb, |
| 1126 |
an B<unnamed event> results. |
| 1127 |
An unnamed event cannot be accessed by normal methods |
| 1128 |
and the use of unnamed events is |
| 1129 |
strongly discouraged. |
| 1130 |
SLIF parse events are described in detail in |
| 1131 |
L<a separate document|Marpa::R2::Event>. |
| 1132 |
|
| 1133 |
=head2 forgiving |
| 1134 |
|
| 1135 |
=for Marpa::R2::Display |
| 1136 |
name: forgiving adverb example |
| 1137 |
partial: 1 |
| 1138 |
normalize-whitespace: 1 |
| 1139 |
|
| 1140 |
:lexeme ~ <name> forgiving => 1 |
| 1141 |
|
| 1142 |
=for Marpa::R2::Display::End |
| 1143 |
|
| 1144 |
The forgiving adverb is a synonym for |
| 1145 |
L<the C<latm> adverb|/"latm">. |
| 1146 |
|
| 1147 |
=head2 latm |
| 1148 |
|
| 1149 |
=for Marpa::R2::Display |
| 1150 |
name: latm adverb example |
| 1151 |
partial: 1 |
| 1152 |
normalize-whitespace: 1 |
| 1153 |
|
| 1154 |
:lexeme ~ value latm => 1 |
| 1155 |
|
| 1156 |
=for Marpa::R2::Display::End |
| 1157 |
|
| 1158 |
The C<latm> adverb applies only to lexemes and |
| 1159 |
is only allowed in |
| 1160 |
L<a C<:lexeme> pseudo-rule|/"Lexeme pseudo-rule"> and |
| 1161 |
L<a C<lexeme default> statement|/"Lexeme default statement">. |
| 1162 |
Its value is a boolean. |
| 1163 |
If the boolean is set it indicates that a token |
| 1164 |
is LATM. |
| 1165 |
A value of 1 is recommended, which indicates |
| 1166 |
that a token is LATM. |
| 1167 |
The default value is 0, |
| 1168 |
for reasons of backward compatibility. |
| 1169 |
|
| 1170 |
LATM means "longest acceptable tokens match". |
| 1171 |
In this, |
| 1172 |
the lexer find those tokens that |
| 1173 |
are the longest that |
| 1174 |
would be accepted by the G1 grammar. |
| 1175 |
There may be more than one |
| 1176 |
such "longest" acceptable token, |
| 1177 |
in which case, the lexing will be ambiguous, |
| 1178 |
and the parse will use all of the matching tokens. |
| 1179 |
|
| 1180 |
The alternative to LATM, and the default, is |
| 1181 |
the "longest tokens match" (LTM) discipline. |
| 1182 |
LTM is similar to LATM, except that it |
| 1183 |
takes no account of whether a token would |
| 1184 |
be acceptable to the G1 grammar. |
| 1185 |
This makes it possible that LTM will find |
| 1186 |
one or more lexemes that are a longest match, |
| 1187 |
and none of them will be acceptable to G1. |
| 1188 |
When that happens, the parse fails with an error message. |
| 1189 |
This failure occurs even if shorter |
| 1190 |
lexemes would have been |
| 1191 |
found using LATM, |
| 1192 |
lexemes which would have |
| 1193 |
been acceptable to the G1 grammar. |
| 1194 |
This means that matching succeeds more often under LATM than |
| 1195 |
under LTM. |
| 1196 |
|
| 1197 |
Intuitively, LATM is a longest tokens match that considers context, |
| 1198 |
while LTM is a longest tokens match that ignores context. |
| 1199 |
LATM is usually preferable. |
| 1200 |
Usually if LATM is chosen, a parse will want to use the |
| 1201 |
L<a C<lexeme default> statement|/"Lexeme default statement"> |
| 1202 |
and use LATM globally. |
| 1203 |
It is possible to use LATM adverb on a lexeme by |
| 1204 |
lexeme basis. When that is done, the lexemes marked |
| 1205 |
LATM will match only if acceptable to the G1 grammar, |
| 1206 |
and the lexemes not marked LATM will match regardless |
| 1207 |
of their acceptability to the G1 grammar. |
| 1208 |
|
| 1209 |
Whichever token discipline is chosen, |
| 1210 |
all tokens matched will be of the same length. |
| 1211 |
Shorter tokens will not be considered. |
| 1212 |
|
| 1213 |
LTM is the default for historical reasons. |
| 1214 |
LTM was the SLIF's original token matching discipline because |
| 1215 |
it more closely models traditional lexing. |
| 1216 |
Also for historical reasons, LATM lexemes |
| 1217 |
are sometimes called "forgiving" -- in the original implementation, an |
| 1218 |
LTM search was always done for all lexemes, |
| 1219 |
and LATM was implemented by "forgiving" rejection by the G1 grammar, |
| 1220 |
and backing up over the input to find acceptable lexemes. |
| 1221 |
Marpa now does LATM far more efficiently -- |
| 1222 |
the G1 grammar indicates to the lexer, in advance, |
| 1223 |
which lexemes are acceptable, and the |
| 1224 |
lexer searches only for those. |
| 1225 |
|
| 1226 |
=head2 name |
| 1227 |
|
| 1228 |
=for Marpa::R2::Display |
| 1229 |
name: symbol, name array descriptor example |
| 1230 |
partial: 1 |
| 1231 |
normalize-whitespace: 1 |
| 1232 |
|
| 1233 |
start ::= number1 number2 name => top |
| 1234 |
number1 ::= <forty two> name => 'number 1' |
| 1235 |
number2 ::= <forty three> name => 'number 2' |
| 1236 |
|
| 1237 |
=for Marpa::R2::Display::End |
| 1238 |
|
| 1239 |
The C<name> adverb applies only to rules and |
| 1240 |
rule alternatives. |
| 1241 |
When specified, it defines a name for that rule |
| 1242 |
alternative. |
| 1243 |
|
| 1244 |
=head2 null-ranking |
| 1245 |
|
| 1246 |
=for Marpa::R2::Display |
| 1247 |
name: null-ranking adverb example |
| 1248 |
partial: 1 |
| 1249 |
normalize-whitespace: 1 |
| 1250 |
|
| 1251 |
S ::= A A A A null-ranking => high |
| 1252 |
|
| 1253 |
=for Marpa::R2::Display::End |
| 1254 |
|
| 1255 |
The C<null-ranking> adverb applies only to G1 rules |
| 1256 |
(L0 rules do not have a semantics) and is ignored unless |
| 1257 |
the SLIF recognizer's C<ranking_method> named argument |
| 1258 |
is set to something other than its default. |
| 1259 |
Some rule alternatives can match the same input in several ways, |
| 1260 |
depending on which symbols are nulled. |
| 1261 |
These different ways of nulling symbols in a rule |
| 1262 |
are called its null variants. |
| 1263 |
The C<null-ranking> named argument allows the application |
| 1264 |
to control the order in which null variants |
| 1265 |
are returned by the C<value()> method. |
| 1266 |
|
| 1267 |
If C<null-ranking> is undefined, |
| 1268 |
the order of the null variants will be arbitrary. |
| 1269 |
This is the default, |
| 1270 |
and is acceptable to most applications. |
| 1271 |
For details on using the |
| 1272 |
C<null-ranking> adverb, |
| 1273 |
see L<the document on parse order|Marpa::R2::Semantics::Order>. |
| 1274 |
|
| 1275 |
=head2 pause |
| 1276 |
|
| 1277 |
=for Marpa::R2::Display |
| 1278 |
name: SLIF named lexeme event synopsis |
| 1279 |
normalize-whitespace: 1 |
| 1280 |
|
| 1281 |
:lexeme ~ <a> pause => before event => 'before a' |
| 1282 |
:lexeme ~ <b> pause => after event => 'after b'=on |
| 1283 |
:lexeme ~ <c> pause => before event => 'before c'=off |
| 1284 |
:lexeme ~ <d> pause => after event => 'after d' |
| 1285 |
|
| 1286 |
=for Marpa::R2::Display::End |
| 1287 |
|
| 1288 |
The C<pause> adverb applies only to lexemes and |
| 1289 |
is only allowed in |
| 1290 |
L<a C<:lexeme> pseudo-rule|/"Lexeme pseudo-rule">. |
| 1291 |
The C<pause> adverb declares a SLIF parse event. |
| 1292 |
The L<event adverb|/"event"> names the SLIF parse event |
| 1293 |
declared by |
| 1294 |
the C<pause> adverb. |
| 1295 |
|
| 1296 |
When an event declared with the |
| 1297 |
the L<C<pause>|/"pause"> adverb |
| 1298 |
is not named using the C<event> adverb, |
| 1299 |
an B<unnamed event> results. |
| 1300 |
An unnamed event cannot be accessed by normal methods |
| 1301 |
and the use of unnamed events is |
| 1302 |
strongly discouraged. |
| 1303 |
|
| 1304 |
=head2 priority |
| 1305 |
|
| 1306 |
The C<priority> adverb is only allowed in |
| 1307 |
L<a C<:lexeme> |
| 1308 |
pseudo-rule|/"Lexeme pseudo-rule">. |
| 1309 |
It sets the lexeme priority for the lexeme. |
| 1310 |
The priority must be an integer, |
| 1311 |
but it may be negative. |
| 1312 |
An increase in numerical value means a higher priority. |
| 1313 |
For example, |
| 1314 |
a priority of 1 is greater than a priority of 0. |
| 1315 |
A priority of 0, in turn, is greater than a priority of -1. |
| 1316 |
The default priority is zero. |
| 1317 |
|
| 1318 |
Where more than one lexeme can be accepted at |
| 1319 |
a location, the lexeme priority limits the lexemes |
| 1320 |
that will be considered. |
| 1321 |
Only lexemes with the highest priority are considered. |
| 1322 |
If several lexemes have the same priority, |
| 1323 |
all of them will be accepted. |
| 1324 |
|
| 1325 |
The only effect of the lexeme priority |
| 1326 |
is on the choice |
| 1327 |
of lexemes when |
| 1328 |
|
| 1329 |
=over 4 |
| 1330 |
|
| 1331 |
=item * all of them would be accepted; |
| 1332 |
|
| 1333 |
=item * all started at the same string location; |
| 1334 |
|
| 1335 |
=item * all end at the same string location; and therefore |
| 1336 |
|
| 1337 |
=item * all have the same length. |
| 1338 |
|
| 1339 |
=back |
| 1340 |
|
| 1341 |
Lexeme priorities only have |
| 1342 |
an effect when lexemes are accepted. |
| 1343 |
The intent of this scheme is to avoid situations where a lexeme |
| 1344 |
with a high priority is rejected, and causes a parse to fail, |
| 1345 |
even though another lower priority lexeme is acceptable |
| 1346 |
and would allow the parse to continue. |
| 1347 |
|
| 1348 |
For example, suppose that "C<say>" can be both |
| 1349 |
a keyword (C<< <say keyword> >>), |
| 1350 |
and a variable name (C<< <variable> >>). |
| 1351 |
Suppose further that |
| 1352 |
the grammar specifies that C<< <say keyword> >> has a priority of 1, |
| 1353 |
and C<< <variable> >> is left at the default priority of 0. |
| 1354 |
When L0 finds a occurrence of "C<say>", |
| 1355 |
where both the C<say> keyword and a variable name would |
| 1356 |
be accepted by G1, then only the C<say> keyword is read by G1, |
| 1357 |
because of the priorities. |
| 1358 |
|
| 1359 |
But, suppose instead that the parse is at a location |
| 1360 |
where G1 is not accepting |
| 1361 |
the C<< <say keyword> >>. |
| 1362 |
Since only lexeme priorites of acceptable lexemes are considered, |
| 1363 |
C<< <variable> >> lexeme |
| 1364 |
has the highest priority, |
| 1365 |
and the literal string "C<say>" will be read as a |
| 1366 |
C<< <variable> >> |
| 1367 |
token. |
| 1368 |
|
| 1369 |
=head2 proper |
| 1370 |
|
| 1371 |
The C<proper> keyword is only valid for a quantified right side, |
| 1372 |
and its value must be a boolean, |
| 1373 |
in the form of a binary digit (C<0> or C<1>). |
| 1374 |
It is only relevant if a separator |
| 1375 |
is defined and is 1 if proper separation |
| 1376 |
is required, and 0 if Perl separation is allowed. |
| 1377 |
"Perl separation" allows a final separator. |
| 1378 |
"Proper separation" is so called, because it requires |
| 1379 |
that separators be "proper" in the sense that they |
| 1380 |
must actually separate |
| 1381 |
sequence items. |
| 1382 |
|
| 1383 |
=head2 rank |
| 1384 |
|
| 1385 |
=for Marpa::R2::Display |
| 1386 |
name: rank adverb example |
| 1387 |
partial: 1 |
| 1388 |
normalize-whitespace: 1 |
| 1389 |
|
| 1390 |
unspecial ::= ('I' 'am' 'special') words ('--' 'NOT!' ';') rank => 1 |
| 1391 |
special ::= words (';') rank => -1 |
| 1392 |
|
| 1393 |
=for Marpa::R2::Display::End |
| 1394 |
|
| 1395 |
C<rank> is ignored unless |
| 1396 |
the recognizer's C<ranking_method> named argument |
| 1397 |
is set to something other than its default. |
| 1398 |
The range allowed for C<rank> is implementation-defined, |
| 1399 |
but numbers in the range |
| 1400 |
between -134,217,727 and 134,217,727 |
| 1401 |
will always be allowed. |
| 1402 |
C<rank> is 0 by default. |
| 1403 |
For details on using the |
| 1404 |
C<rank> named argument, |
| 1405 |
see L<the document on parse order|Marpa::R2::Semantics::Order>. |
| 1406 |
|
| 1407 |
=head2 separator |
| 1408 |
|
| 1409 |
The C<separator> keyword is only valid for a quantified right side, |
| 1410 |
and its value must be a single symbol -- |
| 1411 |
either a single symbol name, |
| 1412 |
or a character class. |
| 1413 |
If specified, the separator must separate items of the sequence. |
| 1414 |
A separator may not be nullable. |
| 1415 |
|
| 1416 |
=head1 Precedence |
| 1417 |
|
| 1418 |
Marpa's precedence is the traditional one, |
| 1419 |
but generalized. |
| 1420 |
Traditional precedence parsing required |
| 1421 |
the classification of operators as postfix, infix, etc. |
| 1422 |
Marpa's precedence parsing is NOT based on |
| 1423 |
the special treatment of operators. |
| 1424 |
|
| 1425 |
For the purpose of precedence, |
| 1426 |
an operand is an occurrence in a RHS alternative |
| 1427 |
of the LHS symbol. |
| 1428 |
An operator is considered to be anything |
| 1429 |
that is not an operand. |
| 1430 |
The arity of an alternative is the number of operands that |
| 1431 |
it contains. |
| 1432 |
All arities are allowed, from zero to the arbitrary |
| 1433 |
number imposed by system limits such as memory and file size. |
| 1434 |
|
| 1435 |
For example, in the synopsis, the LHS symbol is |
| 1436 |
C<Expression>. The alternative |
| 1437 |
|
| 1438 |
=for Marpa::R2::Display |
| 1439 |
name: Stuifzand Synopsis |
| 1440 |
partial: 1 |
| 1441 |
normalize-whitespace: 1 |
| 1442 |
|
| 1443 |
(<op lparen>) Expression (<op rparen>) |
| 1444 |
|
| 1445 |
=for Marpa::R2::Display::End |
| 1446 |
|
| 1447 |
contains one occurrence of C<Expression> and therefore has an arity of one. |
| 1448 |
The C<< <op lparen> >> and C<< <op rparen> >> are considered |
| 1449 |
to be operators. |
| 1450 |
|
| 1451 |
In the RHS alternative |
| 1452 |
|
| 1453 |
=for Marpa::R2::Display |
| 1454 |
name: Stuifzand Synopsis |
| 1455 |
partial: 1 |
| 1456 |
normalize-whitespace: 1 |
| 1457 |
|
| 1458 |
Expression (<op pow>) Expression |
| 1459 |
|
| 1460 |
=for Marpa::R2::Display::End |
| 1461 |
|
| 1462 |
C<Expression> occurs twice, and therefore the arity is 2. |
| 1463 |
C<< <op pow> >> is considered to be an operator. |
| 1464 |
|
| 1465 |
Because for this purpose an operator is defined as anything that |
| 1466 |
is not an operand, Marpa treats some symbols as operators |
| 1467 |
that would not be considered operators in the traditional approach. |
| 1468 |
For example, |
| 1469 |
in the RHS alternative |
| 1470 |
|
| 1471 |
=for Marpa::R2::Display |
| 1472 |
name: Stuifzand Synopsis |
| 1473 |
partial: 1 |
| 1474 |
normalize-whitespace: 1 |
| 1475 |
|
| 1476 |
Number |
| 1477 |
|
| 1478 |
=for Marpa::R2::Display::End |
| 1479 |
|
| 1480 |
there are no occurrences of C<Expression>, so that the alternative |
| 1481 |
has an arity of zero -- it is nullary. |
| 1482 |
The symbol C<Number> is considered to be an operator. |
| 1483 |
|
| 1484 |
An alternative with arity 0 is nullary. |
| 1485 |
Precedence and associativity |
| 1486 |
are meaningless in this |
| 1487 |
case and will be ignored. |
| 1488 |
|
| 1489 |
An alternative with arity 1 is unary. |
| 1490 |
Precedence will have effect, |
| 1491 |
but left and right associativity will not. |
| 1492 |
|
| 1493 |
An alternative with arity 2 is binary. |
| 1494 |
Precedence will have effect, |
| 1495 |
and left and right associativity will behave |
| 1496 |
in the traditional way. |
| 1497 |
The traditional behavior for binary alternatives |
| 1498 |
is exactly as described next for the I<N>-ary case. |
| 1499 |
|
| 1500 |
An alternative with an arity of I<N>, |
| 1501 |
where I<N> is 2 or greater, is I<N>-ary. |
| 1502 |
Precedence will have effect. |
| 1503 |
For left associativity, |
| 1504 |
only the leftmost operand of an I<N>-ary alternative |
| 1505 |
associates -- operands |
| 1506 |
after the first will have the |
| 1507 |
next-tightest priority level. |
| 1508 |
For right associativity, |
| 1509 |
only the rightmost operand of an I<N>-ary alternative |
| 1510 |
associates -- all operands |
| 1511 |
except the last |
| 1512 |
will have the next-tightest priority level. |
| 1513 |
|
| 1514 |
Marpa also allows "group" associativity. |
| 1515 |
In "group" associativity, all operands associate |
| 1516 |
at the loosest (lowest) priority. |
| 1517 |
That is, in an alternative with group associativity, |
| 1518 |
each operand may be a full expression of the kind |
| 1519 |
defined by the prioritized rule. |
| 1520 |
"Group" associativity is used, for example, |
| 1521 |
in implementing the traditional function of parentheses |
| 1522 |
in Marpa. |
| 1523 |
Group associativity is meaningless for nullary alternatives, |
| 1524 |
and is ignored. |
| 1525 |
|
| 1526 |
=head2 Precedence and ambiguous grammars |
| 1527 |
|
| 1528 |
Marpa's generalization of precedence works |
| 1529 |
for all grammars that |
| 1530 |
can be defined by prioritized rules. |
| 1531 |
It is efficient (linear) for all grammars that could be |
| 1532 |
parsed by the traditional precedence parsing methods. |
| 1533 |
Marpa also allows you to define alternatives |
| 1534 |
not allowed by traditional methods. |
| 1535 |
Many of these are useful, |
| 1536 |
and most of the useful ones can be parsed efficiently. |
| 1537 |
|
| 1538 |
Because of the many forms of recursion allowed, |
| 1539 |
it is possible |
| 1540 |
to define highly ambiguous grammars using the precedence mechanism. |
| 1541 |
This can occur even by accident. |
| 1542 |
|
| 1543 |
The user should especially be careful with |
| 1544 |
right hand side alternatives in |
| 1545 |
which all the symbols are operands. |
| 1546 |
These can be useful. |
| 1547 |
For example, an implicit operation can be defined using |
| 1548 |
a binary alternative with no non-operands, |
| 1549 |
and this could implement, for example, |
| 1550 |
the standard notation for concatenation or multiplication. |
| 1551 |
But to do this efficiently |
| 1552 |
requires either avoiding ambiguity, |
| 1553 |
or controlling its use carefully. |
| 1554 |
|
| 1555 |
Marpa does catch the case where an alternative consists |
| 1556 |
only of a single operand -- a "unit rule". |
| 1557 |
This causes a fatal error. |
| 1558 |
Unit rules are easy to define by accident in |
| 1559 |
the SLIF. |
| 1560 |
The author knows of no practical use for them, |
| 1561 |
and their presence in a grammar is usually |
| 1562 |
unintentional. |
| 1563 |
Note that, in the event an application |
| 1564 |
does find a use for a grammar with unit rules, |
| 1565 |
Libmarpa itself and the Thin interface |
| 1566 |
can parse it. |
| 1567 |
|
| 1568 |
=head1 Copyright and License |
| 1569 |
|
| 1570 |
=for Marpa::R2::Display |
| 1571 |
ignore: 1 |
| 1572 |
|
| 1573 |
Copyright 2022 Jeffrey Kegler |
| 1574 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 1575 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 1576 |
General Public License as published by the Free Software Foundation, |
| 1577 |
either version 3 of the License, or (at your option) any later version. |
| 1578 |
|
| 1579 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 1580 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 1581 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 1582 |
Lesser General Public License for more details. |
| 1583 |
|
| 1584 |
You should have received a copy of the GNU Lesser |
| 1585 |
General Public License along with Marpa::R2. If not, see |
| 1586 |
http://www.gnu.org/licenses/. |
| 1587 |
|
| 1588 |
=for Marpa::R2::Display::End |
| 1589 |
|
| 1590 |
=cut |
| 1591 |
|
| 1592 |
# Local Variables: |
| 1593 |
# mode: cperl |
| 1594 |
# cperl-indent-level: 4 |
| 1595 |
# fill-column: 100 |
| 1596 |
# End: |
| 1597 |
# vim: expandtab shiftwidth=4: |