| 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 - Scanless interface |
| 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 |
| 116 |
is an introduction and overview |
| 117 |
to Marpa's Scanless interface (SLIF). |
| 118 |
Marpa::R2's top-level page has |
| 119 |
L<an SLIF tutorial in Marpa's top-level page|Marpa::R2>. |
| 120 |
If you are new to Marpa or its SLIF, |
| 121 |
you probably want to start with that. |
| 122 |
|
| 123 |
This document follows up on the tutorial, |
| 124 |
looking more deeply and carefully at the concepts |
| 125 |
behind the SLIF. |
| 126 |
Separate documents provide |
| 127 |
the reference documentation for |
| 128 |
L<Scanless grammar objects|Marpa::R2::Scanless::G>, |
| 129 |
L<Scanless recognizer objects|Marpa::R2::Scanless::R> |
| 130 |
and |
| 131 |
L<the Scanless DSL|Marpa::R2::Scanless::DSL>. |
| 132 |
|
| 133 |
=head1 The two levels of language description |
| 134 |
|
| 135 |
Programmers usually |
| 136 |
describe the syntax of a language at two levels. |
| 137 |
The same two-level approach can be convenient for implementing |
| 138 |
a parser of the language. |
| 139 |
But, implementation aside, |
| 140 |
a two-level description |
| 141 |
seems to be a natural approach to |
| 142 |
the design issues that arise in languages |
| 143 |
intended for practical use. |
| 144 |
|
| 145 |
The first level is structural. |
| 146 |
For example, here is how the Perl docs describe one of |
| 147 |
the forms that Perl's C<use> statement takes: |
| 148 |
|
| 149 |
=for Marpa::R2::Display |
| 150 |
ignore: 1 |
| 151 |
|
| 152 |
use Module VERSION LIST |
| 153 |
|
| 154 |
=for Marpa::R2::Display::End |
| 155 |
|
| 156 |
and in Perl's source code (C<perly.y>) something similar |
| 157 |
drives the parser. |
| 158 |
|
| 159 |
The second level is lexical. |
| 160 |
For example, |
| 161 |
Perl's L<perlpodspec> page has a number of statements like this: |
| 162 |
|
| 163 |
=for Marpa::R2::Display |
| 164 |
ignore: 1 |
| 165 |
|
| 166 |
[...] you can distinguish URL-links from anything else |
| 167 |
by the fact that they match m/\A\w+:[^:\s]\S*\z/. |
| 168 |
|
| 169 |
=for Marpa::R2::Display::End |
| 170 |
|
| 171 |
The lexical level is character by character. |
| 172 |
The structural level is less well-defined, |
| 173 |
but in practice it ignores most of the character-by-character issues, |
| 174 |
and it almost always avoids dealing with whitespace. |
| 175 |
|
| 176 |
For reasons that will become clear later, |
| 177 |
I will sometimes call the lexical level, L0, |
| 178 |
and will sometimes call the structural level, G1. |
| 179 |
(For historic reasons, L0 is sometimes also called G0.) |
| 180 |
|
| 181 |
It is important to realize |
| 182 |
that the difference between L0 and G1 is one |
| 183 |
of level of description and |
| 184 |
NOT one of precision or exactness. |
| 185 |
A structural description of Perl's C<use> statement, |
| 186 |
much like the one I showed above, |
| 187 |
is in Perl's source code (C<perly.y>), |
| 188 |
along with many other, similar, |
| 189 |
structural-level descriptions. |
| 190 |
These are used to |
| 191 |
generate the production parser for Perl so, |
| 192 |
clearly, structural level descriptions are every bit |
| 193 |
as much a precision instrument as regular expressions. |
| 194 |
|
| 195 |
=head1 A very simple language |
| 196 |
|
| 197 |
In order to focus on very basic issues, |
| 198 |
I will use as an example, |
| 199 |
a very simple language with a very simple semantics. |
| 200 |
The language consists of decimal digits and ASCII spaces. |
| 201 |
The semantics will treat it as a series of integers to be added. |
| 202 |
|
| 203 |
Here are three strings in that language |
| 204 |
|
| 205 |
=for Marpa::R2::Display |
| 206 |
ignore: 1 |
| 207 |
|
| 208 |
8675311 |
| 209 |
867 5311 |
| 210 |
8 6 7 5 3 1 1 |
| 211 |
|
| 212 |
=for Marpa::R2::Display::End |
| 213 |
|
| 214 |
According to our semantics, |
| 215 |
the three strings contain respectively, |
| 216 |
one, two and seven integers. |
| 217 |
The values of the three strings are, |
| 218 |
according to our semantics, |
| 219 |
the sum of these integers: |
| 220 |
respectively, 8675311, 6178, and 31. |
| 221 |
|
| 222 |
It's sometimes said, in describing languages like the above, |
| 223 |
that "whitespace is ignored". |
| 224 |
From the purely structural point of view this can be, in one sense, true. |
| 225 |
But from the lexical point of view it's clearly quite false. |
| 226 |
|
| 227 |
Combining the two levels of description, |
| 228 |
it is very hard to justify an assertion that "whitespace is ignored". |
| 229 |
The three strings in the display above |
| 230 |
differ only in whitespace. |
| 231 |
Clearly the placement |
| 232 |
of the whitespace makes a vast difference, and has a major |
| 233 |
effect on the structure of string, |
| 234 |
which in turn has a determining effect on its semantics. |
| 235 |
|
| 236 |
=head1 Why the structural level? |
| 237 |
|
| 238 |
As we've seen, the structural level ignores essential aspects |
| 239 |
of the language. |
| 240 |
It is possible to describe a language using a single level of description. |
| 241 |
So why have a structural (G1) level of description? |
| 242 |
Why not a "unified" instead of a "split" description. |
| 243 |
|
| 244 |
It turns out that, for most languages of practical size, |
| 245 |
particularly those that deploy whitespace in a natural |
| 246 |
and intuitive way, |
| 247 |
a "unified" description rapidly becomes unwriteable, |
| 248 |
and even more rapidly becomes unreadable. |
| 249 |
The reader should be able to |
| 250 |
convince himself by taking the BNF from his favorite |
| 251 |
language standard and recasting it so that |
| 252 |
every rule takes into account whitespace. |
| 253 |
As one example, consider declarations in the C language. |
| 254 |
|
| 255 |
=for Marpa::R2::Display |
| 256 |
ignore: 1 |
| 257 |
|
| 258 |
unsigned int a; |
| 259 |
unsigned*b; |
| 260 |
|
| 261 |
=for Marpa::R2::Display::End |
| 262 |
|
| 263 |
In the first of the two lines above the whitespace is necessary. |
| 264 |
In the second of the two lines whitespace would be allowed, |
| 265 |
but is not necessary. |
| 266 |
You cannot simply insist on whitespace between all symbols, |
| 267 |
because whitespace is and should be optional between |
| 268 |
some symbols and not between others. |
| 269 |
Where whitespace is optional, and where it should not be, |
| 270 |
depends on which characters are adjacent to each other. |
| 271 |
This kind of character-level information is not convenient to represent |
| 272 |
at the structural (G1) level. |
| 273 |
|
| 274 |
It is certainly possible to write whitespace-aware |
| 275 |
BNF for the fragment of the C language |
| 276 |
above. |
| 277 |
And it is certainly possible to extend it to include more and |
| 278 |
more of the declaration syntax. |
| 279 |
But before you've extended the BNF very much, |
| 280 |
you will notice it is becoming a lot harder to write. |
| 281 |
You will also notice that, as quickly as it is becoming hard to |
| 282 |
write, it is even more quickly becoming "write-only" -- |
| 283 |
impossible to read. |
| 284 |
In making your BNF |
| 285 |
whitespace-aware, you are more than doubling its size. |
| 286 |
And you are burying |
| 287 |
what intuition sees as the structure of the language |
| 288 |
under a deep pile of special cases. |
| 289 |
|
| 290 |
Long before you finish, I expect you will realize |
| 291 |
that the "unified" approach is simply not workable. |
| 292 |
The authors of the C language |
| 293 |
relegated lexical issues to their own brief section, |
| 294 |
and ignored them in |
| 295 |
most of their language description. |
| 296 |
This was clearly the only practical approach. |
| 297 |
|
| 298 |
=head1 Interweaving the two levels |
| 299 |
|
| 300 |
The scanless interface |
| 301 |
interweaves the "split" and "unified" approaches |
| 302 |
and, I hope, preserves the best features of each. |
| 303 |
Here is full syntax of |
| 304 |
the example whitespace-and-digit language, |
| 305 |
described using Marpa::R2's scanless interface: |
| 306 |
|
| 307 |
=for Marpa::R2::Display |
| 308 |
name: Scanless concept example |
| 309 |
partial: 1 |
| 310 |
normalize-whitespace: 1 |
| 311 |
|
| 312 |
:start ::= <number sequence> |
| 313 |
<number sequence> ::= <number>+ action => add_sequence |
| 314 |
number ~ digit+ |
| 315 |
digit ~ [0-9] |
| 316 |
:discard ~ whitespace |
| 317 |
whitespace ~ [\s]+ |
| 318 |
|
| 319 |
=for Marpa::R2::Display::End |
| 320 |
|
| 321 |
=head2 A new operator |
| 322 |
|
| 323 |
In this example, three of the scanless interface's extensions |
| 324 |
to the Stuifzand interface are used. |
| 325 |
First, the tilde ("C<~>") is used to separate LHS and RHS of rules at the lexical |
| 326 |
(L0) level. |
| 327 |
Rules whose LHS and RHS are separated by the traditional BNF operator ("C<::=>") |
| 328 |
are at the structural (G1) level. |
| 329 |
|
| 330 |
The programmer must decide when to use the "C<~>" operator |
| 331 |
and when to use the "C<::=>" operator, |
| 332 |
but the choice will usually be easy: |
| 333 |
If you want Marpa to "do what I mean" with whitespace, you use the |
| 334 |
"C<::=>" operator. |
| 335 |
If you want Marpa to do exactly what you say on a character-by-character basis, |
| 336 |
then you use the "C<~>" operator. |
| 337 |
|
| 338 |
=head2 Character classes |
| 339 |
|
| 340 |
Perl character classes are now allowed on the RHS of prioritized and quantified |
| 341 |
rules. |
| 342 |
The example shows character classes only in L0 rules, |
| 343 |
but character classes can also be used in G1 rules. |
| 344 |
When a character class is used |
| 345 |
in a G1 rule, it still must be implemented at |
| 346 |
the L0 level. |
| 347 |
Marpa knows this and "does what you mean." |
| 348 |
|
| 349 |
=head2 Discard pseudo-rule |
| 350 |
|
| 351 |
A new type of rule is introduced: |
| 352 |
a "discard" pseudo-rule. |
| 353 |
A discard pseudo-rule has a C<:discard> pseudo-symbol on its LHS |
| 354 |
and one symbol name on its RHS. |
| 355 |
It indicates that, when the RHS symbol is recognized, |
| 356 |
it should not be passed on as usual to the structural (G1) level. |
| 357 |
Instead, the lexical (L0) level will simply "discard" what it has |
| 358 |
found. |
| 359 |
In the example, whitespace is discarded. |
| 360 |
|
| 361 |
=head1 Lexemes |
| 362 |
|
| 363 |
Tokens at the boundary between L0 and G1 have special |
| 364 |
significance. |
| 365 |
The top-level undiscarded symbols in L0, |
| 366 |
which will be called "L0 lexemes", |
| 367 |
go on to become the terminals in G1. |
| 368 |
G1's terminals are called "G1 lexemes". |
| 369 |
To find the "L0 lexemes", |
| 370 |
Marpa looks for symbols which are on |
| 371 |
the LHS of a L0 rule, but not on the RHS of any L0 rule. |
| 372 |
To find the "G1 lexemes", |
| 373 |
Marpa looks for symbols on the RHS of at least one G1 rule, |
| 374 |
but not on the LHS of any G1 rule. |
| 375 |
|
| 376 |
L0 and G1 should agree on what is a lexeme and what is not. |
| 377 |
If they do not, |
| 378 |
the programmer receives a fatal message which describes the |
| 379 |
problem and the symbols involved. |
| 380 |
So in practice I will usually simply refer to "lexemes". |
| 381 |
|
| 382 |
=head1 Longest acceptable tokens match |
| 383 |
|
| 384 |
If you specify |
| 385 |
"C<< latm => 1 >>" |
| 386 |
as the default, |
| 387 |
which you almost always should, |
| 388 |
the L0 grammar looks for tokens on a |
| 389 |
longest acceptable tokens match (LATM) basis. |
| 390 |
Tokens which the structural grammar would reject |
| 391 |
are thrown away. |
| 392 |
So are tokens in discard pseudo-rules. |
| 393 |
The rest are passed on to the G1 grammar. |
| 394 |
|
| 395 |
Note that the match is longest TOKENS. |
| 396 |
Several tokens may have the same length, |
| 397 |
so several tokens may be "longest". |
| 398 |
When that happens, Marpa |
| 399 |
uses the full set of longest tokens |
| 400 |
in looking for possible parses. |
| 401 |
For more about LATM and its alternative, LTM, |
| 402 |
see L<the detailed description of the C<latm> |
| 403 |
adverb|Marpa::R2::Scanless::DSL/"latm">. |
| 404 |
|
| 405 |
=head1 Semantics |
| 406 |
|
| 407 |
The value of a L0 rule is always the string it matches, |
| 408 |
and the value of a lexeme from the G1 point of view is the |
| 409 |
same as its value from the L0 point of view. |
| 410 |
This means that it makes no sense to specify semantic |
| 411 |
actions for L0 rules, and that is not allowed. |
| 412 |
|
| 413 |
With the exception of lexeme values, |
| 414 |
the semantics of the G1 grammar are exactly the |
| 415 |
same as for ordinary grammars. |
| 416 |
Actions may be specified for G1 rules and will |
| 417 |
behave as described in |
| 418 |
L<Marpa::R2::Semantics>. |
| 419 |
|
| 420 |
=head1 Implementation |
| 421 |
|
| 422 |
The scannerless interface uses two co-operating Marpa grammars, |
| 423 |
an approach pioneered by Andrew Rodland. |
| 424 |
There are separate Marpa grammars for the L0 and G1 levels, |
| 425 |
as well as separate parsers. |
| 426 |
The details of their interaction are hidden from the user. |
| 427 |
Typically, the L0 parser finds tokens and passes them up to the |
| 428 |
G1 parser. |
| 429 |
|
| 430 |
The interface described in |
| 431 |
this document is surprisingly implementation-agnostic. |
| 432 |
The author developed the basics of this |
| 433 |
interface while trying an implementation approach, |
| 434 |
that used a single Marpa grammar, |
| 435 |
before changing to the dual grammar implementation. |
| 436 |
|
| 437 |
=head1 Copyright and License |
| 438 |
|
| 439 |
=for Marpa::R2::Display |
| 440 |
ignore: 1 |
| 441 |
|
| 442 |
Copyright 2022 Jeffrey Kegler |
| 443 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 444 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 445 |
General Public License as published by the Free Software Foundation, |
| 446 |
either version 3 of the License, or (at your option) any later version. |
| 447 |
|
| 448 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 449 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 450 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 451 |
Lesser General Public License for more details. |
| 452 |
|
| 453 |
You should have received a copy of the GNU Lesser |
| 454 |
General Public License along with Marpa::R2. If not, see |
| 455 |
http://www.gnu.org/licenses/. |
| 456 |
|
| 457 |
=for Marpa::R2::Display::End |
| 458 |
|
| 459 |
=cut |
| 460 |
|
| 461 |
# Local Variables: |
| 462 |
# mode: cperl |
| 463 |
# cperl-indent-level: 4 |
| 464 |
# fill-column: 100 |
| 465 |
# End: |
| 466 |
# vim: expandtab shiftwidth=4: |
| 467 |
|