File: NAIF.pod

package info (click to toggle)
libmarpa-r2-perl 12.000000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,660 kB
  • sloc: perl: 42,628; ansic: 23,387; sh: 4,363; makefile: 157
file content (709 lines) | stat: -rw-r--r-- 19,764 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
# Copyright 2022 Jeffrey Kegler
# This file is part of Marpa::R2.  Marpa::R2 is free software: you can
# redistribute it and/or modify it under the terms of the GNU Lesser
# General Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Marpa::R2 is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser
# General Public License along with Marpa::R2.  If not, see
# http://www.gnu.org/licenses/.

=head1 NAME

Marpa::R2::Deprecated::NAIF - Marpa named argument interface (NAIF)

=head1 THE NAIF INTERFACE IS DEPRECATED

This document describes the NAIF interface,
which is deprecated.
PLEASE DO NOT USE IT FOR NEW DEVELOPMENT.

=head1 Synopsis

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
normalize-whitespace: 1

    use Marpa::R2;

    my $grammar = Marpa::R2::Grammar->new(
        {   start   => 'Expression',
            actions => 'My_Actions',
            default_action => 'first_arg',
            rules   => [
                { lhs => 'Expression', rhs => [qw/Term/] },
                { lhs => 'Term', rhs => [qw/Factor/] },
                { lhs => 'Factor', rhs => [qw/Number/] },
                { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
                {   lhs    => 'Factor',
                    rhs    => [qw/Factor Multiply Factor/],
                    action => 'do_multiply'
                },
            ],
        }
    );

    $grammar->precompute();

    my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );

    $recce->read( 'Number', 42 );
    $recce->read('Multiply');
    $recce->read( 'Number', 1 );
    $recce->read('Add');
    $recce->read( 'Number', 7 );

    sub My_Actions::do_add {
        my ( undef, $t1, undef, $t2 ) = @_;
        return $t1 + $t2;
    }

    sub My_Actions::do_multiply {
        my ( undef, $t1, undef, $t2 ) = @_;
        return $t1 * $t2;
    }

    sub My_Actions::first_arg { shift; return shift; }

    my $value_ref = $recce->value;
    my $value = $value_ref ? ${$value_ref} : 'No Parse';

=for Marpa::R2::Display::End

=head1 About this document

This document contains a top-level overview of,
and tutorial for,
the named argument inteface (NAIF) for the Marpa parse engine.
If you are a new to Marpa, you want to start with
L<the tutorial for the
the Scanless interface (SLIF)|Marpa::R2>
instead.

The NAIF is a middle level interface.
It is more low level than
L<the Scanless interface
(SLIF)|Marpa::R2::Scanless>,
which uses a domain-specific language.
But it is higher level,
and provides more features, than
L<the thin
interface|Marpa::R2::Advanced::Thin>,
which provides direct access to the underlying
Libmarpa C library.

The two examples in this document
show the typical flows of NAIF Marpa method calls.
This document will use these examples
to describe the basic features of Marpa
in semi-tutorial fashion.
More advanced features, and full reference details of all features,
can be found in L<the other Marpa API documents|/"Other documents">.

=head2 The three phases

A parser needs to:

=over

=item * Accept a grammar.

=item * Read input.

=item * Return values from the parses,
according to a semantics.

=back

In Marpa these three tasks
are, for the most part, distinct phases.
Grammars are
L<C<Marpa::R2::Grammar>|Marpa::R2::Deprecated::NAIF::Grammar> objects.
The reading of input and the evaluation of the parse
according to the semantics is performed by
L<C<Marpa::R2::Recognizer>|Marpa::R2::Deprecated::NAIF::Recognizer> objects.

=head1 Example 1: a simple calculator

The synopsis shows the code for a very simple calculator.
It handles only
addition and multiplication of integers.
This section explains, line by line, how it works.

=head2 Marpa::R2::Grammar::new

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    my $grammar = Marpa::R2::Grammar->new(
        {   start   => 'Expression',
            actions => 'My_Actions',
            default_action => 'first_arg',
            rules   => [
                { lhs => 'Expression', rhs => [qw/Term/] },
                { lhs => 'Term', rhs => [qw/Factor/] },
                { lhs => 'Factor', rhs => [qw/Number/] },
                { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
                {   lhs    => 'Factor',
                    rhs    => [qw/Factor Multiply Factor/],
                    action => 'do_multiply'
                },
            ],
        }
    );

=for Marpa::R2::Display::End

Marpa grammars are
L<C<Marpa::R2::Grammar>|Marpa::R2::Deprecated::NAIF::Grammar> objects.
They are created
with the
L<Marpa::R2::Grammar::new|Marpa::R2::Deprecated::NAIF::Grammar/"Constructor">
constructor.
The arguments to
L<Marpa::R2::Grammar::new|Marpa::R2::Deprecated::NAIF::Grammar/"Constructor">
are references to
hashes of named arguments.
In the key/value pairs of these hashes,
the
hash key
is the
name of the argument,
and the
hash value
is the
value of the named argument.

=head3 The start named argument

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    start => 'Expression',

=for Marpa::R2::Display::End

The C<start> named argument is required.
Its value is
a string containing the name of the grammar's start symbol.

=head3 Named arguments for the semantics

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

            actions => 'My_Actions',
            default_action => 'first_arg',

=for Marpa::R2::Display::End

The C<actions> and C<default_action> named arguments specify
semantics.
Their argument values are strings,
which acquire their semantics during evaluation.

Evaluation will be described later.
Peeking ahead,
C<actions> provides the name
of a Perl package where Marpa will look for
its B<actions>.
The C<default_action> named argument
will be interpreted
as an B<action name> in that package.
This action name will resolve to an action --
a Perl closure that implements semantics.
The action specified by C<default_action>
is used as the action for rules
with no action of their own.

=head3 The rules named argument

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    rules => [
        { lhs => 'Expression', rhs => [qw/Term/] },
        { lhs => 'Term',       rhs => [qw/Factor/] },
        { lhs => 'Factor',     rhs => [qw/Number/] },
        { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
        {   lhs    => 'Factor',
            rhs    => [qw/Factor Multiply Factor/],
            action => 'do_multiply'
        },
    ],

=for Marpa::R2::Display::End

The value of the
C<rules> named argument is a reference to an array of
B<rule descriptors>.
In this example, all the rule descriptors are in the "long" form --
they are references to hashes of B<rule properties>.
In each key/value pair of a rule descriptor hash, the key is
the name of a rule property, and the hash value is the value of
that rule property.

=head4 The lhs property

The value of the C<lhs> rule property must be a string containing
the name of the rule's left hand side symbol.
Every Marpa rule must have a left hand side symbol.

=head4 The rhs property

The value of the C<rhs> property is a reference to
an array of strings containing
names of the rule's right hand symbols,
in order.
This array may be zero length, in which case
this is an B<empty rule> --
a rule with no symbols on the right hand side.
There are no empty rules in this example.

=head4 The action property

The value of the C<action> rule property is a string.
Peeking ahead, each C<action> property string
will be interpreted
as an action name.
This action name will be resolved
to a Perl closure that
implements
the rule's semantics.

=head2 Marpa::R2::Grammar::precompute

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    $grammar->precompute();

=for Marpa::R2::Display::End

Before a Marpa grammar object can be used by a Marpa recognizer,
it must be B<precomputed>.
Precomputation compiles data structures that the recognizer will need.

=head2 Marpa::R2::Recognizer::new

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );

=for Marpa::R2::Display::End

C<Marpa::R2::Deprecated::NAIF::Recognizer::new> creates a new recognizer.
Its arguments are references to hashes of named arguments.
In this example the only named argument is
the required argument: "C<grammar>".
The value of the
C<grammar>
named argument must be a precomputed Marpa
grammar.

=head2 Marpa::R2::Recognizer::read

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    $recce->read( 'Number', 42 );
    $recce->read('Multiply');
    $recce->read( 'Number', 1 );
    $recce->read('Add');
    $recce->read( 'Number', 7 );

=for Marpa::R2::Display::End

The C<Marpa::R2::Deprecated::NAIF::Recognizer::read> method takes two arguments,
a B<token name> and a B<token value>.
The token name must be the name
of a valid terminal symbol in the grammar.
By default symbols are valid as terminal symbols,
if and only if they do NOT occur on the LHS of
any rule.

The B<token value>
must be a Perl scalar, but otherwise its form
and semantics
are entirely up to the application.
If the token value is never used,
it can be omitted.
In the calculator example, the values of the
"C<Add>"
and "C<Multiply>" tokens are never used,
and are allowed to default to an undefined value.

=head2 Marpa::R2::Recognizer::value

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    my $value_ref = $recce->value;
    my $value = $value_ref ? ${$value_ref} : 'No Parse';

=for Marpa::R2::Display::End

The C<Marpa::R2::Deprecated::NAIF::Recognizer::value> method returns
a reference to the parse result's value,
if there was a parse result.
If there was no parse result,
C<Marpa::R2::Deprecated::NAIF::Recognizer::value>
returns
C<undef>.

=head2 Resolving the semantics

The first thing C<Marpa::R2::Deprecated::NAIF::Recognizer::value> needs to do is
to resolve the semantics.
B<Resolving the semantics> means
mapping the action names into actions.
B<Actions> are Perl
closures which directly implement semantics.
In this example,
the C<actions> named argument is specified.
C<actions> is a Perl package name.
Marpa will look for actions
in that package.

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    actions => 'My_Actions',

=for Marpa::R2::Display::End

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1
flatten: 1

    { lhs => 'Factor', rhs => [qw/Factor Multiply Factor/], action => 'do_multiply' },

=for Marpa::R2::Display::End

For example, the C<action> property for the above rule
is "C<do_multiply>" and the C<actions> named argument to the grammar
was "C<My_Actions>".
So Marpa looks for a closure whose fully qualified name is C<My_Actions::do_multiply>,
which it finds:

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    sub My_Actions::do_multiply {
        my ( undef, $t1, undef, $t2 ) = @_;
        return $t1 * $t2;
    }

=for Marpa::R2::Display::End

Rules do not always have C<action> properties.
That is the case with these rules in this example:

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    { lhs => 'Expression', rhs => [qw/Term/] },
    { lhs => 'Term', rhs => [qw/Factor/] },
    { lhs => 'Factor', rhs => [qw/Number/] },

=for Marpa::R2::Display::End

The rules in the above display have no action names.
When a rule has no action name,
Marpa will fall back to trying to use
the default action,
as described next.

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    default_action => 'first_arg',

=for Marpa::R2::Display::End

The C<default_action> named argument is resolved in the same way
as are the C<action> properties of the
rules.
In this example,
default_action is specified as "C<first_arg>"
and resolves to
C<My_Actions::first_arg>.

=head2 Actions

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    sub My_Actions::first_arg { shift; return shift; }

=for Marpa::R2::Display::End

=for Marpa::R2::Display
name: Engine Synopsis Unambiguous Parse
partial: 1
normalize-whitespace: 1

    sub My_Actions::do_add {
        my ( undef, $t1, undef, $t2 ) = @_;
        return $t1 + $t2;
    }

=for Marpa::R2::Display::End

Value actions are Perl closures used as callbacks.
Value actions are called when nodes in a parse tree are evaluated.
A value action receives one or more arguments.
The first argument to a value action is always a per-parse-tree
object, which the callbacks can use as a scratchpad.
In these examples, the per-parse-tree object is not used.

For a non-empty rule,
the second and any subsequent arguments to the callback are
the values,
in lexical order,
of the symbols on the right hand side of
the rule.
If the action is for an empty rule,
the per-parse-tree object will be its only argument.

Every value action is expected to return a value.
With one exception,
this value is passed up to a parent node
as an argument.
The exception is the value for the start rule.
The return value for the start rule becomes
the parse result.

Rules with no action specified for them take their
semantics from the C<default_action> named argument.
If there is no default action for a grammar,
rules with no action specified for them
return a Perl C<undef>.

=head1 Example 2: an ambiguous parse

This is the same calculator as before,
rewritten to be ambiguous.
Rather than give multiplication precedence over
addition,
the rewritten calculator allows any order of operations.
In this example,
the actions (C<My_Actions::do_add>, etc.)
and the C<@tokens> array remain
the same as before.

Eliminating precedence makes the grammar shorter,
but it also means there can be multiple parse trees,
and that the different parse trees can have different parse results.
In this application we decide, for each input,
to return every one of the parse results.

=for Marpa::R2::Display
name: Engine Synopsis Ambiguous Parse
normalize-whitespace: 1

    use Marpa::R2;

    my $ambiguous_grammar = Marpa::R2::Grammar->new(
        {   start   => 'E',
            actions => 'My_Actions',
            rules   => [
                [ 'E', [qw/E Add E/],      'do_add' ],
                [ 'E', [qw/E Multiply E/], 'do_multiply' ],
                [ 'E', [qw/Number/],       ],
            ],
            default_action => 'first_arg',
        }
    );

    $ambiguous_grammar->precompute();

    my $ambiguous_recce =
        Marpa::R2::Recognizer->new( { grammar => $ambiguous_grammar } );

    $ambiguous_recce->read( 'Number', 42 );
    $ambiguous_recce->read('Multiply');
    $ambiguous_recce->read( 'Number', 1 );
    $ambiguous_recce->read('Add');
    $ambiguous_recce->read( 'Number', 7 );

    my @values = ();
    while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
        push @values, ${$ambiguous_value_ref};
    }

=for Marpa::R2::Display::End

=head2 Short form rule descriptors

=for Marpa::R2::Display
name: Engine Synopsis Ambiguous Parse
partial: 1
normalize-whitespace: 1

    rules => [
        [ 'E', [qw/E Add E/],      'do_add' ],
        [ 'E', [qw/E Multiply E/], 'do_multiply' ],
        [ 'E', [qw/Number/], ],
    ],

=for Marpa::R2::Display::End

The rule descriptors in the
ambiguous example demonstrate the "short" or array form of rule
descriptors.
Array form rule descriptors are references to arrays.
Here the elements are, in order,
the C<lhs> property,
the C<rhs> property,
and the C<action> property.

=head2 Marpa::R2::Recognizer::value

=for Marpa::R2::Display
name: Engine Synopsis Ambiguous Parse
partial: 1
normalize-whitespace: 1

    my @values = ();
    while ( defined( my $ambiguous_value_ref = $ambiguous_recce->value() ) ) {
        push @values, ${$ambiguous_value_ref};
    }

=for Marpa::R2::Display::End

When called more than once,
the C<Marpa::R2::Deprecated::NAIF::Recognizer::value> method iterates through the parse results.
For each call,
it returns a reference to the parse result.
At the end of the iteration, after
all parse results have been returned,
C<Marpa::R2::Deprecated::NAIF::Recognizer::value> returns C<undef>.
If there were no parse results,
C<Marpa::R2::Deprecated::NAIF::Recognizer::value> returns C<undef> the first
time that it is called.

=head1 Errors and exceptions

As a general rule,
methods in the Marpa NAIF API do not return errors.
When there are errors,
Marpa NAIF API methods throw an exception.

=head1 Inheritance

Classes in the Marpa API are not designed to
be inherited.

=head1 The Marpa:: namespace

The C<Marpa::> top-level namespace is reserved.
For extensions to Marpa,
one appropriate place is the C<MarpaX::> namespace.
This practice helps avoid namespace collisions,
and follows a CPAN standard, as exemplified by
the
C<DBIx::>
C<LWPx::>
and
C<MooseX::>
which are for extensions of, respectively,
DBI, LWP and Moose.

=head1 Other documents

This document gives a semi-tutorial overview of the entire Marpa NAIF API.
For full details on Marpa's grammar objects and their methods,
see the
L<Marpa::R2::Deprecated::NAIF::Grammar> document.
For full details on Marpa's recognizer objects and their methods,
see the
L<Marpa::R2::Deprecated::NAIF::Recognizer> document.

L<Marpa::R2::Vocabulary> is intended as a quick refresher in
parsing terminology,
emphasizing how the standard terms are used
in the Marpa context.
the NAIF's standard semantics are fully described in the
L<Marpa::R2::Deprecated::NAIF::Semantics> document.
Techniques for tracing and for debugging your Marpa grammars
are described in the
L<Marpa::R2::Deprecated::NAIF::Tracing> document and the
L<Marpa::R2::Deprecated::NAIF::Progress> document.
For those with a theoretical bent,
my sources, and other useful references, are described in
L<Marpa::R2::Advanced::Bibliography>.

=head1 Support

L<Marpa::R2> comes without warranty.
Support is provided
on a volunteer basis
through the standard mechanisms for CPAN modules.
L<The Support document|Marpa::R2::Support> has details.

=head1 Copyright and License

=for Marpa::R2::Display
ignore: 1

  Copyright 2022 Jeffrey Kegler
  This file is part of Marpa::R2.  Marpa::R2 is free software: you can
  redistribute it and/or modify it under the terms of the GNU Lesser
  General Public License as published by the Free Software Foundation,
  either version 3 of the License, or (at your option) any later version.

  Marpa::R2 is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser
  General Public License along with Marpa::R2.  If not, see
  http://www.gnu.org/licenses/.

=for Marpa::R2::Display::End

=cut

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4: