Debian Codegraph @

t/final_nonnullable.t

raw
1 #!perl
2 # Copyright 2022 Jeffrey Kegler
3 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
4 # redistribute it and/or modify it under the terms of the GNU Lesser
5 # General Public License as published by the Free Software Foundation,
6 # either version 3 of the License, or (at your option) any later version.
7 #
8 # Marpa::R2 is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser
14 # General Public License along with Marpa::R2. If not, see
15 # http://www.gnu.org/licenses/.
16 # Catch the case of a final non-nulling symbol at the end of a rule
17 # which has more than 2 proper nullables
18 # This is to test an untested branch of the CHAF logic.
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 10;
25
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29
30 ## no critic (Subroutines::RequireArgUnpacking)
31
32 sub default_action {
33 shift;
34 my $v_count = scalar @_;
35 return q{-} if $v_count <= 0;
36 my @vals = map { $_ // q{-} } @_;
37 return $vals[0] if $v_count == 1;
38 return '(' . join( q{;}, @vals ) . ')';
39 } ## end sub default_action
40
41 ## use critic
42
43 my $grammar = Marpa::R2::Grammar->new(
44 { start => 'S',
45
46 rules => [
47 [ 'S', [qw/p p p n/], ],
48 [ 'p', ['a'], ],
49 [ 'p', [], ],
50 [ 'n', ['a'], ],
51 ],
52 terminals => ['a'],
53 default_action => 'main::default_action',
54 }
55 );
56
57 $grammar->precompute();
58
59 Marpa::R2::Test::is( $grammar->show_rules,
60 <<'END_OF_STRING', 'final nonnulling Rules' );
61 0: S -> p p p n
62 1: p -> a
63 2: p -> /* empty !used */
64 3: n -> a
65 END_OF_STRING
66
67 Marpa::R2::Test::is( $grammar->show_ahms,
68 <<'END_OF_STRING', 'final nonnulling AHFA' );
69 AHM 0: postdot = "p"
70 S ::= . p p S[R0:2]
71 AHM 1: postdot = "p"
72 S ::= p . p S[R0:2]
73 AHM 2: postdot = "S[R0:2]"
74 S ::= p p . S[R0:2]
75 AHM 3: completion
76 S ::= p p S[R0:2] .
77 AHM 4: postdot = "p"
78 S ::= . p p[] S[R0:2]
79 AHM 5: postdot = "S[R0:2]"
80 S ::= p p[] . S[R0:2]
81 AHM 6: completion
82 S ::= p p[] S[R0:2] .
83 AHM 7: postdot = "p"
84 S ::= p[] . p S[R0:2]
85 AHM 8: postdot = "S[R0:2]"
86 S ::= p[] p . S[R0:2]
87 AHM 9: completion
88 S ::= p[] p S[R0:2] .
89 AHM 10: postdot = "S[R0:2]"
90 S ::= p[] p[] . S[R0:2]
91 AHM 11: completion
92 S ::= p[] p[] S[R0:2] .
93 AHM 12: postdot = "p"
94 S[R0:2] ::= . p n
95 AHM 13: postdot = "n"
96 S[R0:2] ::= p . n
97 AHM 14: completion
98 S[R0:2] ::= p n .
99 AHM 15: postdot = "n"
100 S[R0:2] ::= p[] . n
101 AHM 16: completion
102 S[R0:2] ::= p[] n .
103 AHM 17: postdot = "a"
104 p ::= . a
105 AHM 18: completion
106 p ::= a .
107 AHM 19: postdot = "a"
108 n ::= . a
109 AHM 20: completion
110 n ::= a .
111 AHM 21: postdot = "S"
112 S['] ::= . S
113 AHM 22: completion
114 S['] ::= S .
115 END_OF_STRING
116
117 my @expected = map {
118 +{ map { ( $_ => 1 ) } @{$_} }
119 }
120 [q{}],
121 [qw( (-;-;-;a) )],
122 [qw( (a;-;-;a) (-;-;a;a) (-;a;-;a) )],
123 [qw( (a;a;-;a) (-;a;a;a) (a;-;a;a))],
124 [qw( (a;a;a;a) )];
125
126 for my $input_length ( 1 .. 4 ) {
127
128 # Set max at 10 just in case there's an infinite loop.
129 # This is for debugging, after all
130 my $recce = Marpa::R2::Recognizer->new(
131 { grammar => $grammar, max_parses => 10 } );
132 for ( 1 .. $input_length ) {
133 $recce->read( 'a', 'a' );
134 }
135 while ( my $value_ref = $recce->value() ) {
136 my $value = $value_ref ? ${$value_ref} : 'No parse';
137 my $expected = $expected[$input_length];
138 if ( defined $expected->{$value} ) {
139 delete $expected->{$value};
140 Test::More::pass(qq{Expected value: "$value"});
141 }
142 else {
143 Test::More::fail(qq{Unexpected value: "$value"});
144 }
145 } ## end while ( my $value_ref = $recce->value() )
146 } ## end for my $input_length ( 1 .. 4 )
147
148 1; # In case used as "do" file
149
150 # Local Variables:
151 # mode: cperl
152 # cperl-indent-level: 4
153 # fill-column: 100
154 # End:
155 # vim: expandtab shiftwidth=4: