Debian Codegraph @

pod/Deprecated/NAIF.pod

raw
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::Deprecated::NAIF - Marpa named argument interface (NAIF)
19
20 =head1 THE NAIF INTERFACE IS DEPRECATED
21
22 This document describes the NAIF interface,
23 which is deprecated.
24 PLEASE DO NOT USE IT FOR NEW DEVELOPMENT.
25
26 =head1 Synopsis
27
28 =for Marpa::R2::Display
29 name: Engine Synopsis Unambiguous Parse
30 normalize-whitespace: 1
31
32 use Marpa::R2;
33
34 my $grammar = Marpa::R2::Grammar->new(
35 { start => 'Expression',
36 actions => 'My_Actions',
37 default_action => 'first_arg',
38 rules => [
39 { lhs => 'Expression', rhs => [qw/Term/] },
40 { lhs => 'Term', rhs => [qw/Factor/] },
41 { lhs => 'Factor', rhs => [qw/Number/] },
42 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
43 { lhs => 'Factor',
44 rhs => [qw/Factor Multiply Factor/],
45 action => 'do_multiply'
46 },
47 ],
48 }
49 );
50
51 $grammar->precompute();
52
53 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
54
55 $recce->read( 'Number', 42 );
56 $recce->read('Multiply');
57 $recce->read( 'Number', 1 );
58 $recce->read('Add');
59 $recce->read( 'Number', 7 );
60
61 sub My_Actions::do_add {
62 my ( undef, $t1, undef, $t2 ) = @_;
63 return $t1 + $t2;
64 }
65
66 sub My_Actions::do_multiply {
67 my ( undef, $t1, undef, $t2 ) = @_;
68 return $t1 * $t2;
69 }
70
71 sub My_Actions::first_arg { shift; return shift; }
72
73 my $value_ref = $recce->value;
74 my $value = $value_ref ? ${$value_ref} : 'No Parse';
75
76 =for Marpa::R2::Display::End
77
78 =head1 About this document
79
80 This document contains a top-level overview of,
81 and tutorial for,
82 the named argument interface (NAIF) for the Marpa parse engine.
83 If you are a new to Marpa, you want to start with
84 L<the tutorial for the
85 the Scanless interface (SLIF)|Marpa::R2>
86 instead.
87
88 The NAIF is a middle level interface.
89 It is more low level than
90 L<the Scanless interface
91 (SLIF)|Marpa::R2::Scanless>,
92 which uses a domain-specific language.
93 But it is higher level,
94 and provides more features, than
95 L<the thin
96 interface|Marpa::R2::Advanced::Thin>,
97 which provides direct access to the underlying
98 Libmarpa C library.
99
100 The two examples in this document
101 show the typical flows of NAIF Marpa method calls.
102 This document will use these examples
103 to describe the basic features of Marpa
104 in semi-tutorial fashion.
105 More advanced features, and full reference details of all features,
106 can be found in L<the other Marpa API documents|/"Other documents">.
107
108 =head2 The three phases
109
110 A parser needs to:
111
112 =over
113
114 =item * Accept a grammar.
115
116 =item * Read input.
117
118 =item * Return values from the parses,
119 according to a semantics.
120
121 =back
122
123 In Marpa these three tasks
124 are, for the most part, distinct phases.
125 Grammars are
126 L<C<Marpa::R2::Grammar>|Marpa::R2::Deprecated::NAIF::Grammar> objects.
127 The reading of input and the evaluation of the parse
128 according to the semantics is performed by
129 L<C<Marpa::R2::Recognizer>|Marpa::R2::Deprecated::NAIF::Recognizer> objects.
130
131 =head1 Example 1: a simple calculator
132
133 The synopsis shows the code for a very simple calculator.
134 It handles only
135 addition and multiplication of integers.
136 This section explains, line by line, how it works.
137
138 =head2 Marpa::R2::Grammar::new
139
140 =for Marpa::R2::Display
141 name: Engine Synopsis Unambiguous Parse
142 partial: 1
143 normalize-whitespace: 1
144
145 my $grammar = Marpa::R2::Grammar->new(
146 { start => 'Expression',
147 actions => 'My_Actions',
148 default_action => 'first_arg',
149 rules => [
150 { lhs => 'Expression', rhs => [qw/Term/] },
151 { lhs => 'Term', rhs => [qw/Factor/] },
152 { lhs => 'Factor', rhs => [qw/Number/] },
153 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
154 { lhs => 'Factor',
155 rhs => [qw/Factor Multiply Factor/],
156 action => 'do_multiply'
157 },
158 ],
159 }
160 );
161
162 =for Marpa::R2::Display::End
163
164 Marpa grammars are
165 L<C<Marpa::R2::Grammar>|Marpa::R2::Deprecated::NAIF::Grammar> objects.
166 They are created
167 with the
168 L<Marpa::R2::Grammar::new|Marpa::R2::Deprecated::NAIF::Grammar/"Constructor">
169 constructor.
170 The arguments to
171 L<Marpa::R2::Grammar::new|Marpa::R2::Deprecated::NAIF::Grammar/"Constructor">
172 are references to
173 hashes of named arguments.
174 In the key/value pairs of these hashes,
175 the
176 hash key
177 is the
178 name of the argument,
179 and the
180 hash value
181 is the
182 value of the named argument.
183
184 =head3 The start named argument
185
186 =for Marpa::R2::Display
187 name: Engine Synopsis Unambiguous Parse
188 partial: 1
189 normalize-whitespace: 1
190
191 start => 'Expression',
192
193 =for Marpa::R2::Display::End
194
195 The C<start> named argument is required.
196 Its value is
197 a string containing the name of the grammar's start symbol.
198
199 =head3 Named arguments for the semantics
200
201 =for Marpa::R2::Display
202 name: Engine Synopsis Unambiguous Parse
203 partial: 1
204 normalize-whitespace: 1
205
206 actions => 'My_Actions',
207 default_action => 'first_arg',
208
209 =for Marpa::R2::Display::End
210
211 The C<actions> and C<default_action> named arguments specify
212 semantics.
213 Their argument values are strings,
214 which acquire their semantics during evaluation.
215
216 Evaluation will be described later.
217 Peeking ahead,
218 C<actions> provides the name
219 of a Perl package where Marpa will look for
220 its B<actions>.
221 The C<default_action> named argument
222 will be interpreted
223 as an B<action name> in that package.
224 This action name will resolve to an action --
225 a Perl closure that implements semantics.
226 The action specified by C<default_action>
227 is used as the action for rules
228 with no action of their own.
229
230 =head3 The rules named argument
231
232 =for Marpa::R2::Display
233 name: Engine Synopsis Unambiguous Parse
234 partial: 1
235 normalize-whitespace: 1
236
237 rules => [
238 { lhs => 'Expression', rhs => [qw/Term/] },
239 { lhs => 'Term', rhs => [qw/Factor/] },
240 { lhs => 'Factor', rhs => [qw/Number/] },
241 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
242 { lhs => 'Factor',
243 rhs => [qw/Factor Multiply Factor/],
244 action => 'do_multiply'
245 },
246 ],
247
248 =for Marpa::R2::Display::End
249
250 The value of the
251 C<rules> named argument is a reference to an array of
252 B<rule descriptors>.
253 In this example, all the rule descriptors are in the "long" form --
254 they are references to hashes of B<rule properties>.
255 In each key/value pair of a rule descriptor hash, the key is
256 the name of a rule property, and the hash value is the value of
257 that rule property.
258
259 =head4 The lhs property
260
261 The value of the C<lhs> rule property must be a string containing
262 the name of the rule's left hand side symbol.
263 Every Marpa rule must have a left hand side symbol.
264
265 =head4 The rhs property
266
267 The value of the C<rhs> property is a reference to
268 an array of strings containing
269 names of the rule's right hand symbols,
270 in order.
271 This array may be zero length, in which case
272 this is an B<empty rule> --
273 a rule with no symbols on the right hand side.
274 There are no empty rules in this example.
275
276 =head4 The action property
277
278 The value of the C<action> rule property is a string.
279 Peeking ahead, each C<action> property string
280 will be interpreted
281 as an action name.
282 This action name will be resolved
283 to a Perl closure that
284 implements
285 the rule's semantics.
286
287 =head2 Marpa::R2::Grammar::precompute
288
289 =for Marpa::R2::Display
290 name: Engine Synopsis Unambiguous Parse
291 partial: 1
292 normalize-whitespace: 1
293
294 $grammar->precompute();
295
296 =for Marpa::R2::Display::End
297
298 Before a Marpa grammar object can be used by a Marpa recognizer,
299 it must be B<precomputed>.
300 Precomputation compiles data structures that the recognizer will need.
301
302 =head2 Marpa::R2::Recognizer::new
303
304 =for Marpa::R2::Display
305 name: Engine Synopsis Unambiguous Parse
306 partial: 1
307 normalize-whitespace: 1
308
309 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
310
311 =for Marpa::R2::Display::End
312
313 C<Marpa::R2::Deprecated::NAIF::Recognizer::new> creates a new recognizer.
314 Its arguments are references to hashes of named arguments.
315 In this example the only named argument is
316 the required argument: "C<grammar>".
317 The value of the
318 C<grammar>
319 named argument must be a precomputed Marpa
320 grammar.
321
322 =head2 Marpa::R2::Recognizer::read
323
324 =for Marpa::R2::Display
325 name: Engine Synopsis Unambiguous Parse
326 partial: 1
327 normalize-whitespace: 1
328
329 $recce->read( 'Number', 42 );
330 $recce->read('Multiply');
331 $recce->read( 'Number', 1 );
332 $recce->read('Add');
333 $recce->read( 'Number', 7 );
334
335 =for Marpa::R2::Display::End
336
337 The C<Marpa::R2::Deprecated::NAIF::Recognizer::read> method takes two arguments,
338 a B<token name> and a B<token value>.
339 The token name must be the name
340 of a valid terminal symbol in the grammar.
341 By default symbols are valid as terminal symbols,
342 if and only if they do NOT occur on the LHS of
343 any rule.
344
345 The B<token value>
346 must be a Perl scalar, but otherwise its form
347 and semantics
348 are entirely up to the application.
349 If the token value is never used,
350 it can be omitted.
351 In the calculator example, the values of the
352 "C<Add>"
353 and "C<Multiply>" tokens are never used,
354 and are allowed to default to an undefined value.
355
356 =head2 Marpa::R2::Recognizer::value
357
358 =for Marpa::R2::Display
359 name: Engine Synopsis Unambiguous Parse
360 partial: 1
361 normalize-whitespace: 1
362
363 my $value_ref = $recce->value;
364 my $value = $value_ref ? ${$value_ref} : 'No Parse';
365
366 =for Marpa::R2::Display::End
367
368 The C<Marpa::R2::Deprecated::NAIF::Recognizer::value> method returns
369 a reference to the parse result's value,
370 if there was a parse result.
371 If there was no parse result,
372 C<Marpa::R2::Deprecated::NAIF::Recognizer::value>
373 returns
374 C<undef>.
375
376 =head2 Resolving the semantics
377
378 The first thing C<Marpa::R2::Deprecated::NAIF::Recognizer::value> needs to do is
379 to resolve the semantics.
380 B<Resolving the semantics> means
381 mapping the action names into actions.
382 B<Actions> are Perl
383 closures which directly implement semantics.
384 In this example,
385 the C<actions> named argument is specified.
386 C<actions> is a Perl package name.
387 Marpa will look for actions
388 in that package.
389
390 =for Marpa::R2::Display
391 name: Engine Synopsis Unambiguous Parse
392 partial: 1
393 normalize-whitespace: 1
394
395 actions => 'My_Actions',
396
397 =for Marpa::R2::Display::End
398
399 =for Marpa::R2::Display
400 name: Engine Synopsis Unambiguous Parse
401 partial: 1
402 normalize-whitespace: 1
403 flatten: 1
404
405 { lhs => 'Factor', rhs => [qw/Factor Multiply Factor/], action => 'do_multiply' },
406
407 =for Marpa::R2::Display::End
408
409 For example, the C<action> property for the above rule
410 is "C<do_multiply>" and the C<actions> named argument to the grammar
411 was "C<My_Actions>".
412 So Marpa looks for a closure whose fully qualified name is C<My_Actions::do_multiply>,
413 which it finds:
414
415 =for Marpa::R2::Display
416 name: Engine Synopsis Unambiguous Parse
417 partial: 1
418 normalize-whitespace: 1
419
420 sub My_Actions::do_multiply {
421 my ( undef, $t1, undef, $t2 ) = @_;
422 return $t1 * $t2;
423 }
424
425 =for Marpa::R2::Display::End
426
427 Rules do not always have C<action> properties.
428 That is the case with these rules in this example:
429
430 =for Marpa::R2::Display
431 name: Engine Synopsis Unambiguous Parse
432 partial: 1
433 normalize-whitespace: 1
434
435 { lhs => 'Expression', rhs => [qw/Term/] },
436 { lhs => 'Term', rhs => [qw/Factor/] },
437 { lhs => 'Factor', rhs => [qw/Number/] },
438
439 =for Marpa::R2::Display::End
440
441 The rules in the above display have no action names.
442 When a rule has no action name,
443 Marpa will fall back to trying to use
444 the default action,
445 as described next.
446
447 =for Marpa::R2::Display
448 name: Engine Synopsis Unambiguous Parse
449 partial: 1
450 normalize-whitespace: 1
451
452 default_action => 'first_arg',
453
454 =for Marpa::R2::Display::End
455
456 The C<default_action> named argument is resolved in the same way
457 as are the C<action> properties of the
458 rules.
459 In this example,
460 default_action is specified as "C<first_arg>"
461 and resolves to
462 C<My_Actions::first_arg>.
463
464 =head2 Actions
465
466 =for Marpa::R2::Display
467 name: Engine Synopsis Unambiguous Parse
468 partial: 1
469 normalize-whitespace: 1
470
471 sub My_Actions::first_arg { shift; return shift; }
472
473 =for Marpa::R2::Display::End
474
475 =for Marpa::R2::Display
476 name: Engine Synopsis Unambiguous Parse
477 partial: 1
478 normalize-whitespace: 1
479
480 sub My_Actions::do_add {
481 my ( undef, $t1, undef, $t2 ) = @_;
482 return $t1 + $t2;
483 }
484
485 =for Marpa::R2::Display::End
486
487 Value actions are Perl closures used as callbacks.
488 Value actions are called when nodes in a parse tree are evaluated.
489 A value action receives one or more arguments.
490 The first argument to a value action is always a per-parse-tree
491 object, which the callbacks can use as a scratchpad.
492 In these examples, the per-parse-tree object is not used.
493
494 For a non-empty rule,
495 the second and any subsequent arguments to the callback are
496 the values,
497 in lexical order,
498 of the symbols on the right hand side of
499 the rule.
500 If the action is for an empty rule,
501 the per-parse-tree object will be its only argument.
502
503 Every value action is expected to return a value.
504 With one exception,
505 this value is passed up to a parent node
506 as an argument.
507 The exception is the value for the start rule.
508 The return value for the start rule becomes
509 the parse result.
510
511 Rules with no action specified for them take their
512 semantics from the C<default_action> named argument.
513 If there is no default action for a grammar,
514 rules with no action specified for them
515 return a Perl C<undef>.
516
517 =head1 Example 2: an ambiguous parse
518
519 This is the same calculator as before,
520 rewritten to be ambiguous.
521 Rather than give multiplication precedence over
522 addition,
523 the rewritten calculator allows any order of operations.
524 In this example,
525 the actions (C<My_Actions::do_add>, etc.)
526 and the C<@tokens> array remain
527 the same as before.
528
529 Eliminating precedence makes the grammar shorter,
530 but it also means there can be multiple parse trees,
531 and that the different parse trees can have different parse results.
532 In this application we decide, for each input,
533 to return every one of the parse results.
534
535 =for Marpa::R2::Display
536 name: Engine Synopsis Ambiguous Parse
537 normalize-whitespace: 1
538
539 use Marpa::R2;
540
541 my $ambiguous_grammar = Marpa::R2::Grammar->new(
542 { start => 'E',
543 actions => 'My_Actions',
544 rules => [
545 [ 'E', [qw/E Add E/], 'do_add' ],
546 [ 'E', [qw/E Multiply E/], 'do_multiply' ],
547 [ 'E', [qw/Number/], ],
548 ],
549 default_action => 'first_arg',
550 }
551 );
552
553 $ambiguous_grammar->precompute();
554
555 my $ambiguous_recce =
556 Marpa::R2::Recognizer->new( { grammar => $ambiguous_grammar } );
557
558 $ambiguous_recce->read( 'Number', 42 );
559 $ambiguous_recce->read('Multiply');
560 $ambiguous_recce->read( 'Number', 1 );
561 $ambiguous_recce->read('Add');
562 $ambiguous_recce->read( 'Number', 7 );
563
564 my @values = ();
565 while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
566 push @values, ${$ambiguous_value_ref};
567 }
568
569 =for Marpa::R2::Display::End
570
571 =head2 Short form rule descriptors
572
573 =for Marpa::R2::Display
574 name: Engine Synopsis Ambiguous Parse
575 partial: 1
576 normalize-whitespace: 1
577
578 rules => [
579 [ 'E', [qw/E Add E/], 'do_add' ],
580 [ 'E', [qw/E Multiply E/], 'do_multiply' ],
581 [ 'E', [qw/Number/], ],
582 ],
583
584 =for Marpa::R2::Display::End
585
586 The rule descriptors in the
587 ambiguous example demonstrate the "short" or array form of rule
588 descriptors.
589 Array form rule descriptors are references to arrays.
590 Here the elements are, in order,
591 the C<lhs> property,
592 the C<rhs> property,
593 and the C<action> property.
594
595 =head2 Marpa::R2::Recognizer::value
596
597 =for Marpa::R2::Display
598 name: Engine Synopsis Ambiguous Parse
599 partial: 1
600 normalize-whitespace: 1
601
602 my @values = ();
603 while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
604 push @values, ${$ambiguous_value_ref};
605 }
606
607 =for Marpa::R2::Display::End
608
609 When called more than once,
610 the C<Marpa::R2::Deprecated::NAIF::Recognizer::value> method iterates through the parse results.
611 For each call,
612 it returns a reference to the parse result.
613 At the end of the iteration, after
614 all parse results have been returned,
615 C<Marpa::R2::Deprecated::NAIF::Recognizer::value> returns C<undef>.
616 If there were no parse results,
617 C<Marpa::R2::Deprecated::NAIF::Recognizer::value> returns C<undef> the first
618 time that it is called.
619
620 =head1 Errors and exceptions
621
622 As a general rule,
623 methods in the Marpa NAIF API do not return errors.
624 When there are errors,
625 Marpa NAIF API methods throw an exception.
626
627 =head1 Inheritance
628
629 Classes in the Marpa API are not designed to
630 be inherited.
631
632 =head1 The Marpa:: namespace
633
634 The C<Marpa::> top-level namespace is reserved.
635 For extensions to Marpa,
636 one appropriate place is the C<MarpaX::> namespace.
637 This practice helps avoid namespace collisions,
638 and follows a CPAN standard, as exemplified by
639 the
640 C<DBIx::>
641 C<LWPx::>
642 and
643 C<MooseX::>
644 which are for extensions of, respectively,
645 DBI, LWP and Moose.
646
647 =head1 Other documents
648
649 This document gives a semi-tutorial overview of the entire Marpa NAIF API.
650 For full details on Marpa's grammar objects and their methods,
651 see the
652 L<Marpa::R2::Deprecated::NAIF::Grammar> document.
653 For full details on Marpa's recognizer objects and their methods,
654 see the
655 L<Marpa::R2::Deprecated::NAIF::Recognizer> document.
656
657 L<Marpa::R2::Vocabulary> is intended as a quick refresher in
658 parsing terminology,
659 emphasizing how the standard terms are used
660 in the Marpa context.
661 the NAIF's standard semantics are fully described in the
662 L<Marpa::R2::Deprecated::NAIF::Semantics> document.
663 Techniques for tracing and for debugging your Marpa grammars
664 are described in the
665 L<Marpa::R2::Deprecated::NAIF::Tracing> document and the
666 L<Marpa::R2::Deprecated::NAIF::Progress> document.
667 For those with a theoretical bent,
668 my sources, and other useful references, are described in
669 L<Marpa::R2::Advanced::Bibliography>.
670
671 =head1 Support
672
673 L<Marpa::R2> comes without warranty.
674 Support is provided
675 on a volunteer basis
676 through the standard mechanisms for CPAN modules.
677 L<The Support document|Marpa::R2::Support> has details.
678
679 =head1 Copyright and License
680
681 =for Marpa::R2::Display
682 ignore: 1
683
684 Copyright 2022 Jeffrey Kegler
685 This file is part of Marpa::R2. Marpa::R2 is free software: you can
686 redistribute it and/or modify it under the terms of the GNU Lesser
687 General Public License as published by the Free Software Foundation,
688 either version 3 of the License, or (at your option) any later version.
689
690 Marpa::R2 is distributed in the hope that it will be useful,
691 but WITHOUT ANY WARRANTY; without even the implied warranty of
692 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
693 Lesser General Public License for more details.
694
695 You should have received a copy of the GNU Lesser
696 General Public License along with Marpa::R2. If not, see
697 http://www.gnu.org/licenses/.
698
699 =for Marpa::R2::Display::End
700
701 =cut
702
703 # Local Variables:
704 # mode: cperl
705 # cperl-indent-level: 4
706 # fill-column: 100
707 # End:
708 # vim: expandtab shiftwidth=4:
709