| 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::Vocabulary - Standard parsing terms as used within Marpa |
| 19 |
|
| 20 |
=head1 Description |
| 21 |
|
| 22 |
The definitions in this document are of standard parsing |
| 23 |
terms as they are used |
| 24 |
in the Marpa context. |
| 25 |
I put B<defining uses> of terms in boldface, for easy skimming. |
| 26 |
Where a narrow |
| 27 |
or specialized sense of the term is the one that applies |
| 28 |
within Marpa, that is the only definition given. |
| 29 |
Marpa also sometimes uses a standard term |
| 30 |
with a definition which is slightly different from |
| 31 |
the standard one. |
| 32 |
("Ambiguous grammar" is one example, and "grammar" itself is |
| 33 |
another.) |
| 34 |
When this is the case, it is explicitly pointed out. |
| 35 |
|
| 36 |
A reader totally new to parsing will find this document too |
| 37 |
terse to act as a textbook or tutorial. |
| 38 |
They will need to look elsewhere first. |
| 39 |
As an introduction, I recommend |
| 40 |
L<Mark Jason Dominus's |
| 41 |
excellent chapter on parsing in the Perl context|Marpa::R2::Advanced::Bibliography/"Dominus 2005">. |
| 42 |
It's available on-line. |
| 43 |
L<Wikipedia|Marpa::R2::Advanced::Bibliography/"Wikipedia"> is also an excellent place to start. |
| 44 |
|
| 45 |
=head2 Basic |
| 46 |
|
| 47 |
A B<grammar> is a set of rules, |
| 48 |
associated with a set of symbols. |
| 49 |
Of the symbols of the language, |
| 50 |
one is distinguished as the B<start symbol>, |
| 51 |
and a subset are designated as B<terminal symbols>. |
| 52 |
|
| 53 |
A B<symbol string>, or simply B<string> where the meaning is clear, |
| 54 |
is a sequence of the symbols of a grammar. |
| 55 |
A symbol string is also called a B<sentential form>. |
| 56 |
The B<length> of a string is the number of symbols in it. |
| 57 |
|
| 58 |
Any symbol which is allowed to occur |
| 59 |
in the symbol string input is called a B<terminal> symbol. |
| 60 |
If the symbols in a symbol string are all terminals, |
| 61 |
that symbol string is also called a B<sentence>. |
| 62 |
|
| 63 |
A B<language> is a set of sentences. |
| 64 |
A grammar defines a B<language>, |
| 65 |
as will be described later. |
| 66 |
|
| 67 |
It is important to note that the term language, as it is |
| 68 |
used in parsing theory, means something very different |
| 69 |
from what it means in ordinary use. |
| 70 |
The meaning of the strings is an essential part |
| 71 |
of the ordinary idea of what a language is. |
| 72 |
In ordinary use, the word "language" means |
| 73 |
far more than a set of sentences. |
| 74 |
In parsing terminology, meaning (or B<semantics> as it is |
| 75 |
called) is a separate issue. |
| 76 |
For parsing theory a |
| 77 |
language is exactly a set of strings -- |
| 78 |
that and nothing more. |
| 79 |
|
| 80 |
The Marpa definition of a grammar differs slightly from the various |
| 81 |
standard ones. Standard definitions usually sharply distinguish terminal symbols |
| 82 |
from non-terminals. Marpa does not. |
| 83 |
|
| 84 |
=head2 Stages of parsing |
| 85 |
|
| 86 |
A B<recognizer> is a program that determines whether its B<input> |
| 87 |
is in the language of a grammar and a start symbol. |
| 88 |
A B<parser> is a program which finds the structure of that input. |
| 89 |
|
| 90 |
The term B<parsing> is used in a strict and a loose sense. |
| 91 |
B<Parsing in the loose sense> is all phases of finding a grammar's structure, |
| 92 |
including a separate recognition phase if the parser has one. (Marpa does.) |
| 93 |
If a parser has phases, |
| 94 |
B<parsing in the strict sense> refers specifically to the phase that finds the structure of the input. |
| 95 |
When the Marpa documents use the term B<parsing> in its strict sense, they will |
| 96 |
speak explicitly of "parsing in the strict sense". |
| 97 |
Otherwise, B<parsing> will mean parsing in the loose sense. |
| 98 |
|
| 99 |
Parsers often use a |
| 100 |
B<lexical analyzer> to convert B<raw input>, |
| 101 |
usually B<input text>, |
| 102 |
into a B<token stream>, |
| 103 |
which is a series of B<tokens>. |
| 104 |
Each token represents a terminal symbol of the grammar and has a B<value>. |
| 105 |
A lexical analyzer is often called a B<lexer> or a B<scanner>, |
| 106 |
and B<lexical analysis> is often called B<lexing> or B<scanning>. |
| 107 |
|
| 108 |
The sequence of symbols represented by the sequence of tokens |
| 109 |
becomes the B<input> |
| 110 |
seen by the recognizer. |
| 111 |
The input must be a sentence -- |
| 112 |
that is, it must be a sequence of terminals. |
| 113 |
|
| 114 |
By default, Marpa uses the token stream model of input. |
| 115 |
Marpa also allows alternative input models. |
| 116 |
These are new to Marpa, so that their terminology is |
| 117 |
of necessity non-standard. |
| 118 |
The terminology needed for alternative input models |
| 119 |
is explained in L<the document that introduces |
| 120 |
them|Marpa::R2::Advanced::Models>. |
| 121 |
|
| 122 |
=head2 Rules |
| 123 |
|
| 124 |
A standard way of describing rules is Backus-Naur Form, or B<BNF>. |
| 125 |
A rule of a grammar is also often called a B<production>. |
| 126 |
In one common way of writing BNF, a production looks like this: |
| 127 |
|
| 128 |
=for Marpa::R2::Display: |
| 129 |
ignore: 1 |
| 130 |
|
| 131 |
Expression ::= Term Factor |
| 132 |
|
| 133 |
=for Marpa::R2::Display::End |
| 134 |
|
| 135 |
In the production above, C<Expression>, C<Term> and C<Factor> are symbols. |
| 136 |
A production consists of a B<left hand side> and a B<right hand side>. |
| 137 |
In a B<context-free grammar>, |
| 138 |
like those Marpa parses, |
| 139 |
the left hand side of a production |
| 140 |
is always a symbol string of length 1. |
| 141 |
The right hand side of a production is a symbol string of zero or more symbols. |
| 142 |
In the example, C<Expression> is the left hand side, and |
| 143 |
C<Term> and C<Factor> are right hand side symbols. |
| 144 |
|
| 145 |
Left hand side and right hand side are often abbreviated as B<RHS> and B<LHS>. |
| 146 |
If the RHS of a production has no symbols, |
| 147 |
the production is called an B<empty production> |
| 148 |
or an B<empty rule>. |
| 149 |
|
| 150 |
=head2 Derivations |
| 151 |
|
| 152 |
A B<step> of a derivation, or B<derivation step>, is a change made to a symbol string |
| 153 |
by applying one of the productions from the grammar. |
| 154 |
The production must be one of those with a LHS that occurs in the symbol string. |
| 155 |
The result of the derivation step is another symbol string, |
| 156 |
one in which every occurence of the LHS symbol from the production is |
| 157 |
replaced by the RHS of the production. |
| 158 |
For example, if C<A>, C<B>, C<C>, C<D>, and C<X> are symbols, |
| 159 |
and |
| 160 |
|
| 161 |
=for Marpa::R2::Display: |
| 162 |
ignore: 1 |
| 163 |
|
| 164 |
X ::= B C |
| 165 |
|
| 166 |
=for Marpa::R2::Display::End |
| 167 |
|
| 168 |
is a production, then |
| 169 |
|
| 170 |
=for Marpa::R2::Display: |
| 171 |
ignore: 1 |
| 172 |
|
| 173 |
A X D -> A B C D |
| 174 |
|
| 175 |
=for Marpa::R2::Display::End |
| 176 |
|
| 177 |
is a derivation step, with "C<A X D>" as its beginning and "C<A B C D>" as its end or result. |
| 178 |
We say that the symbol string "C<A X D>" |
| 179 |
B<derives> the symbol string |
| 180 |
"C<A B C D>". |
| 181 |
|
| 182 |
A B<derivation> is a sequence of derivation steps. |
| 183 |
The B<length> of a derivation is its length in steps. |
| 184 |
|
| 185 |
=over |
| 186 |
|
| 187 |
=item * |
| 188 |
|
| 189 |
We say that |
| 190 |
a first symbol string B<directly |
| 191 |
derives> a second symbol string if and only if there is a derivation of length 1 from the first symbol |
| 192 |
string to the second symbol string. |
| 193 |
|
| 194 |
=item * |
| 195 |
|
| 196 |
Every symbol string is said to derive itself in a derivation |
| 197 |
of length 0. A zero length derivation is a B<trivial derivation>. |
| 198 |
|
| 199 |
=item * |
| 200 |
|
| 201 |
A derivation which is not trivial |
| 202 |
(that is, |
| 203 |
a derivation which has one or more steps) |
| 204 |
is a B<non-trivial> derivation. |
| 205 |
|
| 206 |
=item * |
| 207 |
|
| 208 |
A non-trivial derivation of a symbol string from itself |
| 209 |
is called a B<cycle>. |
| 210 |
Grammars which contain cycles are traditionally considered useless, |
| 211 |
but Marpa will parse with such grammars. |
| 212 |
|
| 213 |
=item * |
| 214 |
|
| 215 |
If a derivation is not trivial or direct, that is, if it has more than one step, |
| 216 |
then it is an B<indirect> derivation. |
| 217 |
|
| 218 |
=back |
| 219 |
|
| 220 |
Technically, a symbol C<X> and a string |
| 221 |
that consists of only that symbol |
| 222 |
are two different things. |
| 223 |
But we often say "the symbol C<X>" |
| 224 |
as shorthand for "the string |
| 225 |
of length 1 |
| 226 |
whose only symbol is C<X>". |
| 227 |
For example, if the string containing only the |
| 228 |
symbol C<X> derives a string C<Y>, |
| 229 |
we will usually say simply that "C<X> derives C<Y>". |
| 230 |
|
| 231 |
Wherever symbol or string C<X> derives C<Y>, |
| 232 |
we may also say C<X> B<produces> C<Y>. |
| 233 |
Derivations are often described as symbol matches. |
| 234 |
Wherever symbol or string C<X> derives C<Y>, |
| 235 |
we may also say that C<Y> B<matches> C<X> |
| 236 |
or that C<X> B<matches> C<Y>. |
| 237 |
It is particularly common to say that |
| 238 |
C<X> matches C<Y> when C<X> or C<Y> is a sentence. |
| 239 |
|
| 240 |
The parse of an input by a grammar is B<successful> |
| 241 |
if and only if, |
| 242 |
according to the grammar, |
| 243 |
the start symbol produces the input sentence. |
| 244 |
The set of all input sentences that |
| 245 |
a grammar |
| 246 |
will successfully parse is |
| 247 |
the B<language> of |
| 248 |
the grammar. |
| 249 |
|
| 250 |
=head2 Nulling |
| 251 |
|
| 252 |
The zero length symbol string is called the B<empty string>. |
| 253 |
The empty string can be considered to be a sentence, in which |
| 254 |
case it is the B<empty sentence>. |
| 255 |
A string of one or more symbols is B<non-empty>. |
| 256 |
A derivation which produces the empty string is a B<null derivation>. |
| 257 |
A derivation from the start symbol which produces the empty string |
| 258 |
is a B<null parse>. |
| 259 |
|
| 260 |
If in a particular grammar, a symbol has a null derivation, |
| 261 |
it is a B<nullable symbol>. |
| 262 |
If, in a particular grammar, |
| 263 |
the only sentence produced by a symbol is the empty sentence, |
| 264 |
it is a B<nulling symbol>. |
| 265 |
All nulling symbols are nullable symbols. |
| 266 |
|
| 267 |
If a symbol is not nullable, it is B<non-nullable>. |
| 268 |
If a symbol is not nulling, it is B<non-nulling>. |
| 269 |
In any instance where a symbol produces the empty string, |
| 270 |
it is said to be B<nulled>, |
| 271 |
or to be a B<null symbol>. |
| 272 |
|
| 273 |
=head2 Useless rules |
| 274 |
|
| 275 |
If any derivation from the start symbol uses a rule, |
| 276 |
that rule is called B<reachable> or B<accessible>. |
| 277 |
A rule that is not accessible |
| 278 |
is called B<unreachable> or B<inaccessible>. |
| 279 |
If any derivation which results in a sentence uses a rule, |
| 280 |
that rule is said to be B<productive>. |
| 281 |
A rule that is not productive is called B<unproductive>. |
| 282 |
For example, a rule is unproductive unless every symbol on |
| 283 |
its RHS either is a terminal or is the LHS of some other rule. |
| 284 |
A rule which is inaccessible or unproductive is called a |
| 285 |
B<useless> rule. |
| 286 |
Marpa can handle grammars with useless rules. |
| 287 |
|
| 288 |
A symbol is B<reachable> or B<accessible> if it appears in a reachable production. |
| 289 |
If a symbol is not reachable, it is B<unreachable> or B<inaccessible>. |
| 290 |
A symbol is B<productive> if it appears on the LHS of a productive rule, |
| 291 |
or if it is a nullable symbol. |
| 292 |
If a symbol is not productive, it is B<unproductive>. |
| 293 |
A symbol which is inaccessible or unproductive is called a |
| 294 |
B<useless> symbol. |
| 295 |
Marpa can handle grammars with useless symbols. |
| 296 |
|
| 297 |
=head2 Recursion and cycles |
| 298 |
|
| 299 |
If any symbol in the grammar non-trivially produces a symbol string containing itself, |
| 300 |
the grammar is said to be B<recursive>. |
| 301 |
If any symbol non-trivially produces a symbol string with itself on the left, |
| 302 |
the grammar is said to be B<left-recursive>. |
| 303 |
If any symbol non-trivially produces a symbol string with itself on the right, |
| 304 |
the grammar is said to be B<right-recursive>. |
| 305 |
Marpa can handle all recursive grammars, |
| 306 |
including |
| 307 |
grammars which are left-recursive, |
| 308 |
grammars which are right-recursive, |
| 309 |
and grammars |
| 310 |
which contain both left- and right-recursion. |
| 311 |
|
| 312 |
A B<cycle> is a non-trivial derivation |
| 313 |
of a string of symbols from itself. |
| 314 |
If it is not possible for any derivation using a grammar |
| 315 |
to contain a cycle, |
| 316 |
then that grammar is said to be B<cycle-free>. |
| 317 |
Traditionally, a grammar is |
| 318 |
considered useless |
| 319 |
if it is not cycle-free. |
| 320 |
|
| 321 |
The traditional deprecation of cycles is well-founded. |
| 322 |
A cycle is |
| 323 |
the parsing equivalent of an infinite loop. |
| 324 |
Once a cycle appears, it can be |
| 325 |
repeated over and over again. |
| 326 |
Even a very short input sentence can have an |
| 327 |
infinite number of parses when the grammar is not |
| 328 |
cycle-free. |
| 329 |
|
| 330 |
For that reason, |
| 331 |
a grammar which contains a cycle is also |
| 332 |
called B<infinitely ambiguous>. |
| 333 |
Marpa can parse with grammars which are not |
| 334 |
cycle-free, |
| 335 |
and will |
| 336 |
even parse inputs that cause cycles. |
| 337 |
When a parse is infinitely ambiguous, Marpa |
| 338 |
limits cycles to a single loop, |
| 339 |
so that only a finite number of parses is returned. |
| 340 |
|
| 341 |
=head2 Parse structure |
| 342 |
|
| 343 |
The structure of a parse can be represented as a series of derivation steps from |
| 344 |
the start symbol to the input. |
| 345 |
Another way to represent structure is as a B<parse tree>. |
| 346 |
Every symbol used in the parse is |
| 347 |
represented by a B<node> of the parse tree. |
| 348 |
Wherever a production is used in the parse, |
| 349 |
its LHS is represented by a B<parent node> |
| 350 |
and the RHS symbols are represented by B<child nodes>. |
| 351 |
The start symbol is the B<root> of the tree. |
| 352 |
The node at the root of the tree is called the B<start node>. |
| 353 |
|
| 354 |
Traditionally, |
| 355 |
grammars divide all symbols sharply into terminals |
| 356 |
and non-terminals. |
| 357 |
A terminal symbol must ALWAYS be used as a terminal. |
| 358 |
A non-terminal symbol can NEVER be used as a terminal. |
| 359 |
|
| 360 |
Marpa's use of terminals is non-traditional, |
| 361 |
and its terminology is different accordingly. |
| 362 |
As in the traditional approach, |
| 363 |
Marpa's non-terminals can never be used as terminals. |
| 364 |
But Marpa terminals can be used anywhere, |
| 365 |
even in places where |
| 366 |
the traditional approach requires a |
| 367 |
a non-terminal symbol. |
| 368 |
In particular, a Marpa terminal can be the LHS |
| 369 |
of a rule. |
| 370 |
|
| 371 |
Traditionally, and in Marpa as well, |
| 372 |
every node is either a |
| 373 |
B<inner node> or a |
| 374 |
B<leaf node>. |
| 375 |
In Marpa, |
| 376 |
B<leaf nodes> are of |
| 377 |
two kinds: |
| 378 |
|
| 379 |
=over |
| 380 |
|
| 381 |
=item * |
| 382 |
|
| 383 |
Nodes for nulled symbols. |
| 384 |
A node for a nulled symbol is called |
| 385 |
a B<nulled node>. |
| 386 |
|
| 387 |
=item * |
| 388 |
|
| 389 |
Nodes for symbols being used as terminals. |
| 390 |
|
| 391 |
=back |
| 392 |
|
| 393 |
=head2 Ambiguity |
| 394 |
|
| 395 |
Marpa allows ambiguous grammars. |
| 396 |
Traditionally |
| 397 |
we say that a parse is B<ambiguous> if, |
| 398 |
for a given grammar and a given input, |
| 399 |
more than one derivation tree is possible. |
| 400 |
However, Marpa allows ambiguous input tokens, |
| 401 |
which the traditional definition does not take |
| 402 |
into account. |
| 403 |
If Marpa used |
| 404 |
the traditional definition, |
| 405 |
all grammars would be |
| 406 |
ambiguous except those grammars which allowed only |
| 407 |
the null parse. |
| 408 |
|
| 409 |
It is easiest if the Marpa definition and the |
| 410 |
traditional definition were extensionally |
| 411 |
equivalent --- |
| 412 |
that is, if Marpa's set of ambiguous grammars |
| 413 |
was exactly the same as the set of traditionally |
| 414 |
ambiguous grammars. |
| 415 |
This can be accomplished by using a slightly |
| 416 |
altered definition. |
| 417 |
In the Marpa context, a grammar |
| 418 |
is B<ambiguous> if and only if, |
| 419 |
for some UNAMBIGUOUS stream of input tokens, |
| 420 |
that grammar produces |
| 421 |
more than one parse tree. |
| 422 |
|
| 423 |
=head2 Semantics |
| 424 |
|
| 425 |
In real life, the structure of a parse is usually a means to an end. |
| 426 |
Grammars usually have a B<semantics> associated with them, |
| 427 |
and what the user actually wants is the B<value> of the parse |
| 428 |
according to the semantics. |
| 429 |
|
| 430 |
The tree representation is especially useful when evaluating a parse. |
| 431 |
In the traditional method of evaluating a parse tree, |
| 432 |
every node which represents a terminal symbol |
| 433 |
has a value associated with it on input. |
| 434 |
Non-null inner nodes |
| 435 |
take their semantics from the production whose LHS they represent. |
| 436 |
Nulled nodes are dealt with as special cases. |
| 437 |
|
| 438 |
The semantics for a production |
| 439 |
describe how to calculate the value of the node which represents the LHS |
| 440 |
(the parent node) |
| 441 |
from the values of zero or more of the nodes which represent the RHS symbols |
| 442 |
(child nodes). |
| 443 |
Values are computed recursively, bottom-up. |
| 444 |
The value of a parse is the value of its start symbol. |
| 445 |
|
| 446 |
=head1 Copyright and License |
| 447 |
|
| 448 |
=for Marpa::R2::Display |
| 449 |
ignore: 1 |
| 450 |
|
| 451 |
Copyright 2022 Jeffrey Kegler |
| 452 |
This file is part of Marpa::R2. Marpa::R2 is free software: you can |
| 453 |
redistribute it and/or modify it under the terms of the GNU Lesser |
| 454 |
General Public License as published by the Free Software Foundation, |
| 455 |
either version 3 of the License, or (at your option) any later version. |
| 456 |
|
| 457 |
Marpa::R2 is distributed in the hope that it will be useful, |
| 458 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 459 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 460 |
Lesser General Public License for more details. |
| 461 |
|
| 462 |
You should have received a copy of the GNU Lesser |
| 463 |
General Public License along with Marpa::R2. If not, see |
| 464 |
http://www.gnu.org/licenses/. |
| 465 |
|
| 466 |
=for Marpa::R2::Display::End |
| 467 |
|
| 468 |
=cut |
| 469 |
|