File: G.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 (670 lines) | stat: -rw-r--r-- 20,038 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
# 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::Scanless::G - Scanless interface grammars

=head1 Synopsis

=for Marpa::R2::Display
name: Scanless grammar synopsis
partial: 1
normalize-whitespace: 1

    my $grammar = Marpa::R2::Scanless::G->new(
        {
            source          => \(<<'END_OF_SOURCE'),
    :default ::= action => do_first_arg
    :start ::= Script
    Script ::= Expression+ separator => comma action => do_script
    comma ~ [,]
    Expression ::=
        Number
        | '(' Expression ')' action => do_parens assoc => group
       || Expression '**' Expression action => do_pow assoc => right
       || Expression '*' Expression action => do_multiply
        | Expression '/' Expression action => do_divide
       || Expression '+' Expression action => do_add
        | Expression '-' Expression action => do_subtract
    Number ~ [\d]+

    :discard ~ whitespace
    whitespace ~ [\s]+
    # allow comments
    :discard ~ <hash comment>
    <hash comment> ~ <terminated hash comment> | <unterminated
       final hash comment>
    <terminated hash comment> ~ '#' <hash comment body> <vertical space char>
    <unterminated final hash comment> ~ '#' <hash comment body>
    <hash comment body> ~ <hash comment char>*
    <vertical space char> ~ [\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
    <hash comment char> ~ [^\x{A}\x{B}\x{C}\x{D}\x{2028}\x{2029}]
    END_OF_SOURCE
        }
    );

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

=head1 About this document

This page is the reference for the grammar objects
of Marpa's Scanless interface.

=head1 Constructor

The C<new()> method is the constructor for Scanless grammars.
An example of its use is L<above|/"Synopsis">.
The C<new()> constructor accepts a hash of named arguments.
The following named arguments are allowed:

=head2 bless_package

Specifies the name of a Perl package.
The package is used
for blessing node values into a Perl class,
in conjunction with the
L<C<bless> adverb|Marpa::R2::Scanless::DSL/"bless">.
C<bless_package> should not be confused with the 
L<SLIF's
C<semantics_package> recognizer setting|Marpa::R2::Scanless::R/"semantics_package">.
The two are not closely related.

=head2 source

The value of the C<source> named argument must be a reference
to a string which contains a description of the grammar.
The string's format is a domain-specific language,
described L<in its own
document|Marpa::R2::Scanless::DSL>.

=head2 trace_file_handle

The value is a file handle.
Trace output and warning messages
go to the trace file handle.
By default the trace file handle is C<STDERR>.

=head2 Discouraged named arguments

=head3 action_object

Use of this argument is discouraged
in favor of L<the C<semantics_package> named argument of the SLIF
recognizer|Marpa::R2::Scanless::R/"semantics_package">.
Like the C<semantics_package> named argument, it sets the semantic
package.
Unlike the C<semantics_package> named argument, it is a fatal error if used
together with an explicit per-parse argument of the SLIF recognizer's C<value()> method.
It is also a fatal error to try to use the C<semantics_package>
and C<action_object> arguments together.

=head3 default_action

Use of this argument is deprecated in favor of using the
L<C<action> adverb|Marpa::R2::Scanless::DSL/"action">
in a
L<default pseudo-rule|Marpa::R2::Scanless::DSL/"Default pseudo-rule">.
Specifies the C<default_action> named argument that
will be used for the G1 grammar.
For details of on possible default action values and how
they are used, see
L<C<action> adverb|Marpa::R2::Scanless::DSL/"action">.

=head1 Mutators

=head2 parse()

=for Marpa::R2::Display
name: Landing page synopsis
normalize-whitespace: 1 
partial: 1

    my $grammar   = Marpa::R2::Scanless::G->new( { source => \$dsl } );
    my $input     = '42 * 1 + 7';
    my $value_ref = $grammar->parse( \$input, 'My_Actions' );

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

This very-high level method is a "one shot"
way of producing a parse value from a grammar and an input stream.
The features this method provides
are those most often wanted in
the "first cut" of a parser.

As the parser grows,
users are likely to find their application has
outgrown this method.
It is recommended, rather than spend a lot of time
exploring ways to adapt this method to expanding needs,
that users be quick to abandon it
in favor of the lower level calls.
As an example of how to make this transition,
the tutorial in L<Marpa::R2> is reimplemented
using low-level calls in L<Marpa::R2::Tutorial2>.

The C<parse()> method takes one or more arguments.
The first argument, which is required, is a ref to an input string.
Optionally, the second argument may be a string specifying the package name
for the semantics.
The remaining arguments
(including the second argument if it exists, but is not a string)
must be references to hashes of named arguments.
These hash references will be
passed, as is,
to the constructor for the recognizer.

This method returns a reference to the only parse value, if there is
exactly one parse value.
If there is no parse, or if the parse is ambiguous,
C<parse()> throws an exception.

=head2 set()

=for Marpa::R2::Display
name: SLIF grammar set() synopsis
normalize-whitespace: 1

    $grammar->set( { trace_file_handle => $trace_fh } );

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

This method allows the named arguments to be changed after an SLIF
grammar is created.
Currently, the only argument that may be changed in L<C<trace_file_handle>|/"trace_file_handle">.

=head1 Accessors

=head2 rule_expand()

=for Marpa::R2::Display
name: SLG rule_expand() synopsis

    my ($lhs_id, @rhs_ids) = $grammar->rule_expand($rule_id);
    $text .= "Rule #$rule_id: $lhs_id ::= " . (join q{ }, @rhs_ids) . "\n";

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

=for Marpa::R2::Display
name: SLG rule_expand() 2 args synopsis

    my ($lhs_id, @rhs_ids) = $grammar->rule_expand($rule_id, 'L0');
    $text .= "L0 Rule #$rule_id: $lhs_id ::= " . (join q{ }, @rhs_ids) . "\n";

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

"Expands" a rule ID into symbol ID's.
An array of symbol ID's is returned.
The ID of the LHS symbol is the first element,
and the remaining elements are the ID's of the RHS symbols,
in order.
Returns an empty array if the rule does not exist.

The first argument is the ID of the rule to be "expanded".
The second, optional, argument is the name of a subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.

=head2 rule_ids()

=for Marpa::R2::Display
name: SLG rule_ids() synopsis
normalize-whitespace: 1

    do_something($_) for $grammar->rule_ids();

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

=for Marpa::R2::Display
name: SLG rule_ids() 2 arg synopsis
normalize-whitespace: 1

    do_something($_) for $grammar->rule_ids('L0');

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

Returns a list of the rule ID's as an array.
Takes one, optional, argument: the name of a subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.

=head2 rule_name()

=for Marpa::R2::Display
name: $grammar->rule_name() example

    push @rule_names, $grammar->rule_name($_) for $grammar->rule_ids();

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

Given a rule ID, returns the rule name.
A rule name is as defined by
L<the C<name> adverb|Marpa::R2::Scanless::DSL/"name">.
If no rule name was defined, the rule name is the name of
the LHS symbol.

=head2 rule_show()

=for Marpa::R2::Display
name: SLG rule_show() synopsis

    my $rule_description = $grammar->rule_show($rule_id);

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

=for Marpa::R2::Display
name: SLG rule_show() 2 args synopsis

    my $rule_description = $grammar->rule_show($rule_id, 'L0');

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

For a rule ID,
returns a string describing that rule in a form which is useful for tracing and debugging,
but subject to change.
Returns a Perl undef if the rule does not exist.

The first argument is the ID of the rule to be displayed.
The second, optional, argument is the name of a subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.

=head2 start_symbol_id()

=for Marpa::R2::Display
name: $grammar->start_symbol_id() example

    my $start_id = $grammar->start_symbol_id();

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

Returns the ID of the start symbol.
Note that there is no method to return the ID of the start
rule, because there may be no unique start rule.

=head2 symbol_description()

=for Marpa::R2::Display
name: SLG symbol_description() synopsis

    my $description = $grammar->symbol_description($symbol_id)
        // '[No description]';
    $text .= "symbol number: $symbol_id  description $description\n";

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

=for Marpa::R2::Display
name: SLG symbol_description() 2 arg synopsis

    my $description = $grammar->symbol_description( $symbol_id, 'L0' )
        // '[No description]';
    $text .= "L0 symbol number: $symbol_id  description $description\n";

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

Given a symbol ID, returns a description of the symbol.
The description may not be defined.
Currently internal symbols tend to have descriptions,
while symbols explicitly specified by the user in the DSL are treated as self-explanatory.
The description is intended for humans to read, and is subject to change.

The first argument is the symbol ID.
A second, optional, argument is the subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.
Returns a Perl C<undef> if the symbol does not exist,
or if it has no description.

=head2 symbol_display_form()

=for Marpa::R2::Display
name: SLG symbol_display_form() synopsis

    my $display_form = $grammar->symbol_display_form($symbol_id);
    $text
        .= "symbol number: $symbol_id  name in display form: $display_form\n";

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

=for Marpa::R2::Display
name: SLG symbol_display_form() 2 arg synopsis

    my $display_form = $grammar->symbol_display_form( $symbol_id, 'L0' );
    $text
        .= "L0 symbol number: $symbol_id  name in display form: $display_form\n";

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

Given a symbol ID, returns the "display form" of the symbol.
This is the symbol in a form thought most suitable for display in messages, etc.
The display form is always defined.
The display form of a symbol is not useable as a name -- it is not necessarily unique,
and is subject to change.

The first argument is the symbol ID.
A second, optional, argument is the subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.
Returns a Perl C<undef> if the symbol does not exist.

=head2 symbol_dsl_form()

=for Marpa::R2::Display
name: SLG symbol_dsl_form() synopsis

    my $dsl_form = $grammar->symbol_dsl_form($symbol_id)
        // '[No name in DSL form]';
    $text .= "symbol number: $symbol_id  DSL form: $dsl_form\n";

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

=for Marpa::R2::Display
name: SLG symbol_dsl_form() 2 arg synopsis

    my $dsl_form = $grammar->symbol_dsl_form( $symbol_id, 'L0' )
        // '[No name in DSL form]';
    $text .= "L0 symbol number: $symbol_id  DSL form: $dsl_form\n";

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

Given a symbol ID, returns the "DSL form" of the symbol.
This is the name of the symbol in a form similar
to the way it is specified by the user in the DSL.
If the symbol has an explicit name,
the symbol's DSL form is the same as its explicit name.
If the symbol does not have an explicit name,
the method may return a Perl C<undef>,
or it may return a DSL name invented by Marpa
and intended to be suggestive.
The DSL form of a symbol is not intended for use as a symbol name
-- it is not necessarily unique,
is not always defined,
and it is subject to change.

The first argument is the symbol ID.
A second, optional, argument is the subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.
Returns a Perl C<undef> if the symbol does not exist,
or if it has no DSL form.

=head2 symbol_ids()

=for Marpa::R2::Display
name: SLG symbol_ids() synopsis
normalize-whitespace: 1

    do_something($_) for $grammar->symbol_ids();

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

=for Marpa::R2::Display
name: SLG symbol_ids() 2 arg synopsis
normalize-whitespace: 1

    do_something($_) for $grammar->symbol_ids('L0');

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

Returns a list of the symbol ID's as an array.
Takes one, optional, argument: the name of a subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.

=head2 symbol_name()

=for Marpa::R2::Display
name: SLG symbol_name() synopsis

    my $name = $grammar->symbol_name($symbol_id);
    $text .= "symbol number: $symbol_id  name: $name\n";

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

=for Marpa::R2::Display
name: SLG symbol_name() 2 arg synopsis

    my $name = $grammar->symbol_name( $symbol_id, 'L0' );
    $text .= "L0 symbol number: $symbol_id  name: $name\n";

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

Given a symbol ID, returns the name of the symbol.
For every symbol ID, this method's return value will be defined
and will be unique to that symbol ID,
so that it is suitable for use as a symbol name.
If a symbol has an explicit name, the return value will be
the symbol's explicit name.
If there is no explicit name, it will be an internal name.
Internal names are subject to change.

The first argument is the symbol ID.
A second, optional, argument is the subgrammar.
Currently there are L0 and G1 subgrammars.
The default subgrammar is G1.
Returns a Perl C<undef> if the symbol does not exist.

=head1 Trace methods

=head2 show_rules()

=for Marpa::R2::Display
name: SLG show_rules() synopsis
partial: 1
normalize-whitespace: 1

    my $show_rules_output = $grammar->show_rules();

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

=for Marpa::R2::Display
name: SLG show_rules() synopsis with 2 args
partial: 1
normalize-whitespace: 1

    $show_rules_output .= $grammar->show_rules(3, 'L0');

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

The C<show_rules()> method returns a descripton of
the rules for a subgrammar, by default G1.
It is useful for understanding the rules as they
appear in trace and debugging outputs.
To allow for improvements in Marpa::R2,
the output of C<show_rules()> is subject to change.

The first optional argument can be a numeric verbosity level.
The default verbosity is 1, which is adequate for
most purposes.
A verbosity of 2 prints additional information useful
for those new to SLIF tracing and debugging.
A verbosity of 3 prints additional information for
experts.

The second, optional, argument is the name of a subgrammar.
Currently there are L0 and G1 subgrammars.

=head2 show_symbols()

=for Marpa::R2::Display
name: SLIF show_symbols() synopsis
partial: 1
normalize-whitespace: 1

    $show_symbols_output .= $grammar->show_symbols(3);

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

=for Marpa::R2::Display
name: SLIF show_symbols() synopsis
partial: 1
normalize-whitespace: 1

    $show_symbols_output .= $grammar->show_symbols(3, 'L0');

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

The C<show_symbols()> method returns a descripton of
the symbols for a subgrammar,
by default G1.
It is useful for understanding the symbols as they
appear in trace and debugging outputs.
To allow for improvements in Marpa::R2,
the output of C<show_symbols()> is subject to change.

The first argument can be a numeric verbosity level.
The default verbosity is 1, which is adequate for
most purposes.
A verbosity of 2 prints additional information useful
for those new to SLIF tracing and debugging.
A verbosity of 3 prints additional information for
experts.

The second, optional, argument is the name of a subgrammar.
Currently there are L0 and G1 subgrammars.

=head1 Discouraged methods

Discouraged methods are those that
continue to be supported, but whose use is discouraged for one
reason or another.

=head2 g0_rule()

=for Marpa::R2::Display
name: Scanless g0_rule() synopsis
normalize-whitespace: 1

    my @g0_rule_ids = $grammar->g0_rule_ids();
    for my $g0_rule_id (@g0_rule_ids) {
        $g0_rules_description .= "$g0_rule_id "
            . ( join q{ }, map {"<$_>"} $grammar->g0_rule($g0_rule_id) ) . "\n";
    }

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

Please prefer L<"rule_expand()">, together with
L<"symbol_name()"> or
L<"symbol_display_form()">.
Given a L0 rule ID as its argument,
returns an array containing the
names of the symbols of that rule.
The C<g0_rule()> method
returns a Perl false if no L0 rule with that rule ID exists.
If the L0 rule ID exists,
C<g0_rule()> returns a list of one or more symbol names.
The first symbol name will be that of
the rule's LHS symbol.
The rest of the list will be the names of the rule's
RHS symbols, in order.

=head2 g0_rule_ids()

=for Marpa::R2::Display
name: Scanless g0_rule() synopsis
normalize-whitespace: 1

    my @g0_rule_ids = $grammar->g0_rule_ids();
    for my $g0_rule_id (@g0_rule_ids) {
        $g0_rules_description .= "$g0_rule_id "
            . ( join q{ }, map {"<$_>"} $grammar->g0_rule($g0_rule_id) ) . "\n";
    }

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

Please prefer L<"rule_expand()">.
Returns a list of the L0 rule ID's.

=head2 g1_rule_ids()

=for Marpa::R2::Display
name: Scanless rule() synopsis
normalize-whitespace: 1

    my @g1_rule_ids = $grammar->g1_rule_ids();
    for my $g1_rule_id (@g1_rule_ids) {
        $g1_rules_description .= "$g1_rule_id "
            . ( join q{ }, map {"<$_>"} $grammar->rule($g1_rule_id) ) . "\n";
    }

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

Please prefer L<"rule_expand()">.
Returns a list of the G1 rule ID's.

=head2 rule()

=for Marpa::R2::Display
name: Scanless rule() synopsis

    my @g1_rule_ids = $grammar->g1_rule_ids();
    for my $g1_rule_id (@g1_rule_ids) {
        $g1_rules_description .= "$g1_rule_id "
            . ( join q{ }, map {"<$_>"} $grammar->rule($g1_rule_id) ) . "\n";
    }

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

Please prefer L<"rule_expand()">, together with
L<"symbol_name()"> or
L<"symbol_display_form()">.
Given a G1 rule ID as its argument,
returns an array containing the
names of the symbols of that rule.
The C<rule()> method
returns a Perl false if no G1 rule with that rule ID exists.
If the rule ID exists,
C<rule()> returns a list of one or more symbol names.
The first symbol name will be that of
the rule's LHS symbol.
The rest of the list will be the names of the rule's
RHS symbols, in order.
The SLIF's C<rule()> method is useful in
combination with
the SLIF's
of L<the progress method|Marpa::R2::Scanless::R/progress()>,
whose output identifies rules by rule ID.

=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: