Debian Codegraph @

pod/Deprecated/NAIF/Grammar.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::Grammar - NAIF grammars
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 partial: 1
31 normalize-whitespace: 1
32
33 my $grammar = Marpa::R2::Grammar->new(
34 { start => 'Expression',
35 actions => 'My_Actions',
36 default_action => 'first_arg',
37 rules => [
38 { lhs => 'Expression', rhs => [qw/Term/] },
39 { lhs => 'Term', rhs => [qw/Factor/] },
40 { lhs => 'Factor', rhs => [qw/Number/] },
41 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
42 { lhs => 'Factor',
43 rhs => [qw/Factor Multiply Factor/],
44 action => 'do_multiply'
45 },
46 ],
47 }
48 );
49
50 $grammar->precompute();
51
52 =for Marpa::R2::Display::End
53
54 =head1 Description
55
56 This document describes grammars for Marpa's
57 named argument interface (NAIF).
58 If you are a beginner,
59 or are not sure which interface you are interested in,
60 or do not know what the NAIF interfaces is,
61 you probably are looking for
62 L<the document on grammars for the SLIF
63 interface|Marpa::R2::Scanless::G>.
64
65 To create a Marpa grammar object,
66 use the C<new> method.
67 Rules and symbols may be specified when the grammar is created.
68
69 To change a Marpa grammar object,
70 use the C<set> method.
71 New rules may be added until a grammar is precomputed.
72
73 A grammar cannot be used for parsing until it is precomputed.
74 To precompute a Marpa grammar object,
75 use the C<precompute> method.
76 After precomputation,
77 no new rules may added and
78 most other changes are forbidden.
79
80 =head2 Symbol names
81
82 Marpa reserves, for its internal use, all symbol names
83 ending with one of these four symbols:
84 the right square bracket ("C<]>"),
85 the right parenthesis ("C<)>"),
86 the right angle bracket ("C<< > >>"),
87 and the right curly bracket ("C<}>").
88 Any other valid Perl string is an acceptable
89 symbol name.
90
91 =head2 Terminal symbols
92
93 Marpa defines a B<terminal> as a symbol which is valid as an
94 input token symbol.
95 By default, the terminals are those symbols who do not
96 appear on the LHS of any rule.
97
98 Marpa will allow any non-nulling symbol to be a terminal,
99 even those which appear on the LHS of one or more rules.
100 To allow (or disallow) use of a symbol as a terminal,
101 the application can use
102 the L<C<terminals> named argument|/"terminals">,
103 and the L<C<terminal> property|/"terminal">.
104 An attempt to use a nulling symbol as a terminal
105 is a fatal error.
106
107 =head2 Sequence rules
108
109 It is very common in a grammar for one symbol to produce a
110 repeating sequence.
111 Marpa allows a shorthand for this:
112 sequence rules.
113 The RHS of a sequence rule will be repeated,
114 as specified by the C<min> rule property.
115 In sequence rules the RHS must always be one symbol
116 in length,
117 and that symbol may not be a nullable symbol.
118
119 A rule is a sequence rule if the C<min> rule property is defined.
120 C<min> can be 0 or 1, and specifies the minimum number of times
121 that the sequence is allowed to repeat.
122 As of this writing,
123 the maximum number of repetitions is always infinite.
124
125 =for Marpa::R2::Display
126 name: Marpa::R2::Grammar min 0 sequence example
127 partial: 1
128 normalize-whitespace: 1
129
130 { lhs => 'sequence', rhs => ['item'], min => 0, action => 'do_sequence' }
131
132 =for Marpa::R2::Display::End
133
134 A C<min> of zero indicates a sequence that repeats zero or more times.
135 This is the equivalent of using the star quantifier
136 ("C<*>") in the
137 standard regular expression notation.
138
139 =for Marpa::R2::Display
140 name: Marpa::R2::Grammar min 1 sequence example
141 partial: 1
142 normalize-whitespace: 1
143
144 { lhs => 'sequence', rhs => ['item'], min => 1, action => 'do_sequence' }
145
146 =for Marpa::R2::Display::End
147
148 A C<min> of one indicates a sequence that repeats one or more times.
149 This is the equivalent of using the plus quantifier
150 ("C<+>") in the
151 standard regular expression notation.
152
153 Sequences can have a separator, specified
154 with the C<separator> rule property.
155 By default, separation is Perl-style:
156 trailing separators are allowed.
157 In "C<proper>" separation,
158 a separator must actually separate
159 two sequence items
160 and therefore is not allowed
161 after the last item of a sequence.
162 If you prefer "C<proper>" separation,
163 you can set
164 the C<proper> rule property.
165
166 =head3 Advantages of sequence rules
167
168 You are never forced to use sequence rules,
169 but it's usually better if you do.
170 When a sequence is written as a sequence rule,
171 Marpa optimizes it.
172
173 When a sequence is written using non-sequence
174 rules, the semantics typically wind up being spread
175 over two or three Perl closures.
176 The semantic action for a sequence rule
177 is a single Perl closure.
178 Putting the semantics into
179 a single Perl closure often results in
180 simpler and more
181 natural code.
182 See L<the section on sequences in the semantics
183 document|Marpa::R2::Deprecated::NAIF::Semantics/"Sequence rule nodes">.
184
185 =head3 Caveats
186
187 Marpa throws an exception if you try to use a nullable symbol
188 as the right hand side of a sequence rule,
189 or as the separator for a sequence rule.
190 The ban on nullables in sequences only applies to sequences
191 when they are written using sequence rules.
192 Nothing prevents you from specifying a sequence of nullables using non-sequence rules.
193 But usually there is no good reason to do this,
194 and sequences of nullables can be highly ambiguous
195 which,
196 for efficiency reasons,
197 makes them
198 a good thing to avoid.
199
200 To keep things simple,
201 the right hand side of a sequence rule must be a single symbol.
202 Of course, applications will often want to repeat sequences of multiple symbols.
203 That is easy to do indirectly:
204
205 =for Marpa::R2::Display
206 name: Marpa::R2::Grammar multipart rhs sequence example
207 partial: 1
208 normalize-whitespace: 1
209
210 { lhs => 'sequence', rhs => [qw(item)], min => 0, action => 'do_sequence' },
211 { lhs => 'item', rhs => [qw(part1 part2)], action => 'do_item' },
212
213 =for Marpa::R2::Display::End
214
215 =head1 Constructor
216
217 =head2 new()
218
219 =for Marpa::R2::Display
220 name: Engine Synopsis Unambiguous Parse
221 partial: 1
222 normalize-whitespace: 1
223
224 my $grammar = Marpa::R2::Grammar->new(
225 { start => 'Expression',
226 actions => 'My_Actions',
227 default_action => 'first_arg',
228 rules => [
229 { lhs => 'Expression', rhs => [qw/Term/] },
230 { lhs => 'Term', rhs => [qw/Factor/] },
231 { lhs => 'Factor', rhs => [qw/Number/] },
232 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
233 { lhs => 'Factor',
234 rhs => [qw/Factor Multiply Factor/],
235 action => 'do_multiply'
236 },
237 ],
238 }
239 );
240
241 =for Marpa::R2::Display::End
242
243 C<Marpa::R2::Deprecated::NAIF::Grammar::new>
244 returns a new Marpa grammar object or throws an exception.
245 The arguments to
246 C<Marpa::R2::Deprecated::NAIF::Grammar::new> are
247 references to hashes of named arguments.
248 In each key/value pair of this hash,
249 the hash key is the argument name
250 and the hash value is the value of
251 the named argument.
252 The available named arguments are described
253 L<below|/"Named arguments">.
254
255 =head1 Mutators
256
257 =head2 precompute()
258
259 =for Marpa::R2::Display
260 name: Engine Synopsis Unambiguous Parse
261 partial: 1
262 normalize-whitespace: 1
263
264 $grammar->precompute();
265
266 =for Marpa::R2::Display::End
267
268 The C<precompute> method
269 compiles data structures that the recognizer will need.
270 It
271 returns the grammar object or throws an exception.
272
273 =head2 set()
274
275 =for Marpa::R2::Display
276 name: Grammar set Synopsis
277 normalize-whitespace: 1
278
279 $grammar->set( { trace_file_handle => $trace_fh } );
280
281 =for Marpa::R2::Display::End
282
283 The arguments to the
284 C<set> method are
285 references to hashes of named arguments.
286 The available named arguments are described
287 L<below|/"Named arguments">.
288 C<set> either returns true or throws an exception.
289
290 =head1 Accessors
291
292 =head2 check_terminal()
293
294 Returns a Perl true when its argument is the name of a terminal symbol.
295 Otherwise, returns a Perl false.
296 Not often needed,
297 but a lexer may find this
298 the most convenient way to determine if a symbol is a terminal.
299
300 =head2 rule()
301
302 =for Marpa::R2::Display
303 name: Action context synopsis
304 normalize-whitespace: 1
305 partial: 1
306
307 my ( $lhs, @rhs ) = $grammar->rule($rule_id);
308
309 =for Marpa::R2::Display::End
310
311 Given a rule ID as its argument,
312 returns an array containing the symbols of the rule.
313 The C<rule()> method
314 returns a Perl false if no rule with that rule ID exists.
315 If the rule ID exists,
316 the rule's LHS symbol is the first symbol in the array,
317 and rest of the array contains the rule's
318 RHS symbols in order.
319 Situations where
320 Rule ID's are encountered include
321 callbacks and
322 use of L<the progress method|Marpa::R2::Deprecated::NAIF::Progress/progress()>.
323
324 =head2 rule_ids()
325
326 =for Marpa::R2::Display
327 name: rule_ids() Synopsis
328 normalize-whitespace: 1
329
330 my @rule_ids = $grammar->rule_ids();
331
332 =for Marpa::R2::Display::End
333
334 Returns an array containing the valid rule IDs.
335 Situations where
336 Rule ID's are encountered include
337 callbacks and
338 use of L<the progress method|Marpa::R2::Deprecated::NAIF::Progress/progress()>.
339
340 =head1 Trace accessors
341
342 =head2 show_problems()
343
344 =for Marpa::R2::Display
345 name: show_problems Synopsis
346 perltidy: '-dcsc -sil=0'
347
348 print $grammar->show_problems()
349 or die "print failed: $ERRNO";
350
351 =for Marpa::R2::Display::End
352
353 Usually the application does not call this method
354 directly.
355 Returns a string describing any serious but non-fatal
356 problems a grammar had in the precomputation phase.
357 A serious problem is one that will prevent parsing.
358 Warnings are not serious problems in this sense.
359 If there were no serious problems, returns a string saying so.
360 This method is not useful before precomputation.
361
362 In Marpa, most serious grammar problems are
363 not immediately thrown as exceptions.
364 This is because there can be a number of serious
365 problems in a grammar, particularly one that is large
366 or in an early draft.
367 If each serious problem
368 caused an immediate exception,
369 the user would have to
370 fix them one at a time
371 -- very tedious.
372
373 The recognizer
374 throws an exception
375 when the user attempts
376 to create a parse from
377 a grammar with serious problems.
378 When that happens,
379 the string returned by C<show_problems> is part of the
380 error message.
381
382 =head2 show_rules()
383
384 =for Marpa::R2::Display
385 name: show_rules Synopsis
386 perltidy: '-dcsc -sil=0'
387
388 print $grammar->show_rules()
389 or die "print failed: $ERRNO";
390
391 =for Marpa::R2::Display::End
392
393 Returns a string listing the rules.
394 Each rule is shown with B<comments> which
395 indicate rule properties.
396 C<show_rules> is useful in debugging grammars.
397
398 Marpa does extensive rewriting of its grammars,
399 and both the original rules and the rewritten rules
400 appear in the C<show_rules> list.
401 When a rule is rewritten, the original rule is
402 often not used.
403 In that case, "C<!used>" will be one of the comments
404 for the original rule.
405 The "C<!used>" comment also marks rules not used
406 for reasons other than rewrites.
407 For example,
408 inaccessible and unproductive rules are also
409 marked "C<!used>".
410
411 The "C<discard_sep"> comment indicates that the rule
412 discards separators
413 This is only
414 relevant in sequence rules.
415 Other comments indicate
416 whether rules
417 were nullable, unproductive, inaccessible, or empty.
418
419 =head2 show_symbols()
420
421 =for Marpa::R2::Display
422 name: show_symbols Synopsis
423 perltidy: '-dcsc -sil=0'
424
425 print $grammar->show_symbols()
426 or die "print failed: $ERRNO";
427
428 =for Marpa::R2::Display::End
429
430 Returns a string listing the symbols, along with comments
431 indicating whether they were
432 terminal, nulling, nullable, unproductive or inaccessible.
433 Useful for debugging grammars.
434
435 =head1 Named arguments
436
437 =head2 action_object
438
439 The C<action_object> named argument
440 specifies a Perl class name to be used
441 in resolving action names to Perl closures.
442 A C<new> constructor must be defined
443 in the C<action_object> package.
444 It will be used to create the per-parse-tree variables.
445 The per-parse-tree variable is passed
446 to rule evaluation closures,
447 as their first argument.
448 Details are in
449 L<the document on semantics|Marpa::R2::Deprecated::NAIF::Semantics>.
450
451 =head2 actions
452
453 =for Marpa::R2::Display
454 name: Engine Synopsis Unambiguous Parse
455 partial: 1
456 normalize-whitespace: 1
457
458 actions => 'My_Actions',
459
460 =for Marpa::R2::Display::End
461
462 The C<actions> named argument specifies
463 the Perl package that Marpa will use when resolving
464 action names to Perl closures.
465 If both an
466 C<actions> named argument and an
467 C<action_object> named argument are specified,
468 the
469 package from the C<actions> named argument
470 is the only one used to resolve action names.
471 The C<actions> package is treated only as a package,
472 and not as a class.
473 Any C<new> constructor in the C<actions> package is ignored.
474 Details are given in
475 L<the document on semantics|Marpa::R2::Deprecated::NAIF::Semantics>.
476
477 =head2 default_action
478
479 =for Marpa::R2::Display
480 name: Engine Synopsis Unambiguous Parse
481 partial: 1
482 normalize-whitespace: 1
483
484 default_action => 'first_arg',
485
486 =for Marpa::R2::Display::End
487
488 The C<default_action> named argument specifies
489 the value action name
490 for rules without an L<C<action> property|/"action">.
491 Details are given in
492 L<the document on semantics|Marpa::R2::Deprecated::NAIF::Semantics>.
493
494 =head2 default_empty_action
495
496 The C<default_empty_action> named argument specifies
497 the action for empty (zero length) rules
498 which have no action specified explicitly.
499 Details are given in
500 L<the document on semantics|Marpa::R2::Deprecated::NAIF::Semantics>.
501
502 =head2 inaccessible_ok
503
504 The value must be a reference to an array of symbol names.
505 By default, Marpa warns if a symbol is inaccessible, but
506 the warning is suppressed for any symbol named in the array.
507 Setting the C<inaccessible_ok> named argument
508 after grammar precomputation is useless,
509 and itself results in a warning.
510
511 Inaccessible symbols are symbols which cannot be derived from
512 the start symbol, and which therefore can never be part of a
513 successful parse.
514 Inaccessible symbols often indicate errors in grammar
515 design.
516 But a user may have plans for these symbols,
517 may wish to keep them as notes,
518 or may simply wish to deal with them later.
519
520 =head2 infinite_action
521
522 Takes as its value a string specifying what Marpa
523 should do if it discovers that
524 its grammar is infinitely
525 ambiguous.
526 The value must be one of
527 "C<fatal>",
528 "C<warn>" or
529 "C<quiet>".
530 A grammar is B<infinitely ambiguous> if there
531 is some input for which it produces
532 an endless number of parses.
533
534 If the value is "C<fatal>",
535 Marpa throws an exception when it encounters
536 an infinitely ambiguous grammar.
537 This is the default and
538 will usually be what the user wants.
539 In most cases,
540 an infinitely ambiguous grammar is simply a mistake.
541
542 "C<quiet>" indicates that
543 the user wants to allow
544 infinitely ambiguous grammars.
545 "C<warn>" indicates that
546 the user wants to allow
547 infinitely ambiguous grammars, but
548 wants a warning message to be printed
549 to the trace file handle.
550
551 =head2 rules
552
553 The value of the
554 C<rules> named argument is a reference to an array of
555 B<rule descriptors>.
556 The C<rules> named argument may be specified multiple times,
557 adding new rules to the grammar each time.
558 New rules may be added until the grammar is precomputed.
559 The format of rule descriptors is L<explained
560 below|/"Rule descriptors">.
561
562 =head2 source
563
564 The value of the
565 C<source> named argument is a reference to string
566 that contains a description of the grammar in BNF format.
567 The format of this string is described in
568 L<the document on the BNF format|Marpa::R2::Deprecated::BNF>.
569 The C<source> named argument may only be specified once,
570 and it cannot be used together with the C<rules>
571 named argument.
572
573 =head2 start
574
575 =for Marpa::R2::Display
576 name: Engine Synopsis Unambiguous Parse
577 partial: 1
578 normalize-whitespace: 1
579
580 start => 'Expression',
581
582 =for Marpa::R2::Display::End
583
584 The value of the C<start> named argument must be a symbol name.
585 It will be used as the start symbol for the grammar.
586 The C<start> named argument is required.
587
588 =head2 symbols
589
590 The value of the C<symbols> named
591 arguments must be a reference to a hash.
592 In each key/value pair of this hash,
593 the hash key is the symbol property name
594 and the hash value is the
595 symbol descriptor.
596 Symbol descriptors are described L<below|/"Symbol descriptors">.
597
598 Note that the value of C<symbols> named argument is a hash,
599 but the value of the C<rules> named argument is an array.
600 This is because symbol names make convenient hash keys.
601 For rules, there is no equally natural choice for a hash key.
602
603 =head2 terminals
604
605 The value of the C<terminals> named argument
606 must be a reference to an array of symbol names.
607 All the symbols in the array
608 will be allowed as terminals.
609 See
610 L<the discussion of terminals above|/"Terminal symbols">.
611
612 =head2 trace_file_handle
613
614 The value is a file handle.
615 Trace output and warning messages
616 go to the trace file handle.
617 By default the trace file handle is C<STDERR>.
618
619 =head2 unproductive_ok
620
621 The value must be a reference to an array of symbol names.
622 By default, Marpa warns if a symbol is unproductive, but
623 the warning is suppressed for any symbol named in the array.
624 Setting the C<unproductive_ok> named argument
625 after grammar precomputation is useless,
626 and itself results in a warning.
627
628 Unproductive symbols are symbols which can never derive
629 a sentence.
630 (A sentence is a string of zero or more terminals.)
631 That means that unproductive symbols can never be part
632 of a successful parse.
633 Unproductive symbols often indicate errors in grammar
634 design.
635 But a user may have plans for these symbols,
636 may wish to keep them as notes,
637 or may simply wish to deal with them later.
638
639 =head2 warnings
640
641 The value is a boolean.
642 Warnings are written to the trace file handle.
643 By default, warnings are on.
644 Usually, an application will want to leave them on.
645 If warnings are turned off,
646 turning them back on
647 after grammar precomputation is useless,
648 and itself results in a warning.
649
650 =head1 Rule descriptors
651
652 =for Marpa::R2::Display
653 name: Engine Synopsis Unambiguous Parse
654 partial: 1
655 normalize-whitespace: 1
656
657 rules => [
658 { lhs => 'Expression', rhs => [qw/Term/] },
659 { lhs => 'Term', rhs => [qw/Factor/] },
660 { lhs => 'Factor', rhs => [qw/Number/] },
661 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
662 { lhs => 'Factor',
663 rhs => [qw/Factor Multiply Factor/],
664 action => 'do_multiply'
665 },
666 ],
667
668 =for Marpa::R2::Display::End
669
670 =head2 Rule descriptors as hashes
671
672 The long form descriptor of a rule is a
673 reference to a hash
674 of B<rule properties>.
675 In each key/value pair of this hash,
676 the hash key is the rule property name
677 and the hash value is the value of
678 that property.
679
680 =head2 action
681
682 The value of the C<action> rule property is a string which
683 specifies the semantics for the rule.
684 For details, see
685 L<the document on semantics|Marpa::R2::Deprecated::NAIF::Semantics>.
686
687 The semantics of nulling symbols are dealt with on a per-symbol
688 basis, rather than a per-rule basis.
689 For this reason the
690 C<action> rule property is useless
691 for empty rules.
692 An exception is thrown if an C<action> property
693 is defined for an empty rule.
694
695 =head2 keep
696
697 Separators in sequence rules
698 are usually not semantically
699 significant.
700 By default,
701 Marpa throws away
702 separators during parse tree traversal and
703 before node evaluation time,
704 so that the semantic actions
705 do not see the separators.
706
707 If the value of the C<keep> rule property
708 is a Perl true, Marpa keeps separators.
709 This allows the semantic actions to examine them.
710 The downside
711 is that the
712 work of distinguishing
713 sequence separators from sequence items
714 is pushed into the semantic actions.
715 For details about the semantics, see
716 L<the document on semantics|Marpa::R2::Deprecated::NAIF::Semantics>.
717
718 =head2 lhs
719
720 The value of the C<lhs> rule property must be a string containing
721 the name of the rule's left hand side symbol.
722 Every Marpa rule must have a left hand side symbol.
723
724 =head2 min
725
726 C<min> must be 0, 1, or undefined.
727 If C<min> is 0 or 1,
728 the rule is a B<sequence rule>.
729 If C<min> is undefined, the rule is an ordinary B<BNF rule>.
730
731 Only one symbol,
732 called the B<sequence item>,
733 is allowed on the right hand side of a sequence rule.
734 The sequence item
735 may not be a nullable symbol.
736 The input will be required to match
737 the sequence item
738 at least C<min> times
739 and will be allowed to match
740 the sequence item
741 an unlimited number of times.
742
743 =head2 null_ranking
744
745 C<null_ranking> is ignored unless
746 the recognizer's C<ranking_method> named argument
747 is set to something other than its default.
748 The C<null_ranking> named argument allows the application
749 to control the order in which rules with nullable symbols
750 are returned by the C<value> method.
751 Such rules can match the same input in several ways
752 depending on which symbols are nulled.
753 These different ways of nulling symbols in a rule
754 are called its null variants.
755
756 If C<null_ranking> is undefined,
757 the order of the null variants will be arbitrary.
758 This is the default,
759 and is acceptable to most applications.
760 For details on using the
761 C<null_ranking> named argument,
762 see L<the document on parse order|Marpa::R2::Deprecated::NAIF::Semantics::Order>.
763
764 =head2 proper
765
766 By default, sequence rules with separators allow trailing
767 separators,
768 Perl-style.
769 If the C<proper> rule property is a Perl true,
770 "C<proper>" separation is enforced.
771 In proper separation,
772 separation must actually separate sequence items,
773 and trailing separators are not allowed.
774
775 =head2 rank
776
777 C<rank> is ignored unless
778 the recognizer's C<ranking_method> named argument
779 is set to something other than its default.
780 The range allowed for C<rank> is implementation-defined,
781 but numbers in the range
782 between -134,217,727 and 134,217,727
783 will always be allowed.
784 C<rank> is 0 by default.
785 For details on using the
786 C<rank> named argument,
787 see L<the document on parse order|Marpa::R2::Deprecated::NAIF::Semantics::Order>.
788
789 =head2 rhs
790
791 The value of the C<rhs> property is a reference to
792 an array of strings containing
793 the names of the rule's right hand symbols,
794 in order.
795 This array may be zero length, in which case
796 this is an B<empty rule> --
797 a rule with no symbols on the right hand side.
798 A rule is also empty if
799 there is no C<rhs> specifier in its descriptor.
800
801 =head2 separator
802
803 Any sequence rule may have a C<separator> defined.
804 The value must be a symbol name.
805 By default, Marpa allows trailing separators.
806 This is the usual style in Perl.
807 The separator must not be a nullable symbol.
808
809 =head2 Rule descriptors as arrays
810
811 =for Marpa::R2::Display
812 name: Engine Synopsis Ambiguous Parse
813 partial: 1
814 normalize-whitespace: 1
815
816 rules => [
817 [ 'E', [qw/E Add E/], 'do_add' ],
818 [ 'E', [qw/E Multiply E/], 'do_multiply' ],
819 [ 'E', [qw/Number/], ],
820 ],
821
822 =for Marpa::R2::Display::End
823
824 Rule descriptors may be given in "short form" --
825 as a reference to an array.
826 The elements of the array,
827 in order,
828 are
829 the C<lhs> property,
830 the C<rhs> property,
831 and the C<action> property.
832 The last two are optional.
833 Omission of an optional
834 property in a short form descriptor
835 has the same effect
836 that omitting the same optional property would have
837 in the long form.
838
839 =head2 Duplicate rules
840
841 Marpa throws an exception if a duplicate rule is added.
842 Two BNF rules are considered duplicates if
843
844 =over
845
846 =item * Both rules have the same left hand symbol.
847
848 =item * Both rules have the same right hand symbols in the same order.
849
850 =back
851
852 Sequence rules are even more restricted.
853 The LHS of a sequence rule
854 may not be the LHS of another sequence rule.
855 The LHS of a sequence rule
856 also may not be the LHS of any BNF rule.
857
858 This restriction on the LHS of sequence rules is
859 intended to make the definition of duplicate rules
860 intuitive and their detection easy.
861 It does not limit the expressiveness of Marpa grammars,
862 because it is very easy to work around.
863 One workaround to create an
864 intermediate rule of length one,
865 whose RHS is the sequence LHS symbol.
866 The LHS of the intermediate rule can then be used,
867 without restriction, as the LHS of other rules.
868
869 =head1 Symbol descriptors
870
871 =for Marpa::R2::Display
872 name: Symbol descriptor example
873 normalize-whitespace: 1
874
875 symbols => {
876 MinusMinus => { terminal => 1 },
877 Minus => { terminal => 1 },
878 Number => { terminal => 1 },
879 },
880
881 =for Marpa::R2::Display::End
882
883 A symbol descriptor is a hash.
884 In the key/value pairs of this hash,
885 the hash key is the symbol property name
886 and the hash value is the value of
887 that property.
888 The available symbol properties are as follows:
889
890 =head2 terminal
891
892 A boolean.
893 If true, it allows the symbol to be used as a terminal.
894 If false, it disallows use of
895 the symbol as a terminal.
896 For details, see
897 L<the section on terminals|/"Terminal symbols">.
898
899 =head1 Copyright and License
900
901 =for Marpa::R2::Display
902 ignore: 1
903
904 Copyright 2022 Jeffrey Kegler
905 This file is part of Marpa::R2. Marpa::R2 is free software: you can
906 redistribute it and/or modify it under the terms of the GNU Lesser
907 General Public License as published by the Free Software Foundation,
908 either version 3 of the License, or (at your option) any later version.
909
910 Marpa::R2 is distributed in the hope that it will be useful,
911 but WITHOUT ANY WARRANTY; without even the implied warranty of
912 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
913 Lesser General Public License for more details.
914
915 You should have received a copy of the GNU Lesser
916 General Public License along with Marpa::R2. If not, see
917 http://www.gnu.org/licenses/.
918
919 =for Marpa::R2::Display::End
920
921 =cut
922
923 # Local Variables:
924 # mode: cperl
925 # cperl-indent-level: 4
926 # fill-column: 100
927 # End:
928 # vim: expandtab shiftwidth=4: