Debian Codegraph @

t/sl_ah2.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
17 # the example grammar in Aycock/Horspool "Practical Earley Parsing",
18 # _The Computer Journal_, Vol. 45, No. 6, pp. 620-630,
19 # in its "NNF" form
20
21 use 5.010001;
22 use strict;
23 use warnings;
24
25 use Test::More tests => 26;
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 return $_[0] if $v_count == 1;
37 return '(' . ( join q{;}, @_ ) . ')';
38 } ## end sub default_action
39
40 ## use critic
41
42 my $dsl = <<'END_OF_DSL';
43 :default ::= action => main::default_action
44 :start ::= S
45 S ::= A A A A
46 A ::=
47 A ::= 'a'
48 END_OF_DSL
49
50 my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl });
51 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar });
52 my $input_length = 4;
53 my $input = ('a' x $input_length);
54 $recce->read( \$input );
55
56 my @expected = map {
57 +{ map { ( $_ => 1 ) } @{$_} }
58 }
59 [q{}],
60 [qw( (a;;;) (;a;;) (;;a;) (;;;a) )],
61 [qw( (a;a;;) (a;;a;) (a;;;a) (;a;a;) (;a;;a) (;;a;a) )],
62 [qw( (a;a;a;) (a;a;;a) (a;;a;a) (;a;a;a) )],
63 ['(a;a;a;a)'];
64
65 $recce->set( { max_parses => 20 } );
66
67 my @ambiguity_expected;
68 $ambiguity_expected[0] = 'No ambiguity';
69
70 $ambiguity_expected[1] = <<'END_OF_AMBIGUITY_DESC';
71 Length of symbol "A" at line 1, column 1 is ambiguous
72 Choice 1, length=1, ends at line 1, column 1
73 Choice 1: a
74 Choice 2 is zero length
75 END_OF_AMBIGUITY_DESC
76
77 $ambiguity_expected[2] = <<'END_OF_AMBIGUITY_DESC';
78 Length of symbol "A" at line 1, column 1 is ambiguous
79 Choice 1 is zero length
80 Choice 2, length=1, ends at line 1, column 1
81 Choice 2: a
82 END_OF_AMBIGUITY_DESC
83
84 $ambiguity_expected[3] = <<'END_OF_AMBIGUITY_DESC';
85 Length of symbol "A" at line 1, column 1 is ambiguous
86 Choice 1 is zero length
87 Choice 2, length=1, ends at line 1, column 1
88 Choice 2: a
89 Length of symbol "A" at line 1, column 2 is ambiguous
90 Choice 1, length=1, ends at line 1, column 2
91 Choice 1: a
92 Choice 2 is zero length
93 END_OF_AMBIGUITY_DESC
94
95 $ambiguity_expected[4] = 'No ambiguity';
96
97 for my $i ( 0 .. $input_length ) {
98
99 $recce->series_restart( { end => $i } );
100 my $expected = $expected[$i];
101
102 # Marpa::R2::Display
103 # name: Scanless ambiguity_metric() synopsis
104
105 my $ambiguity_metric = $recce->ambiguity_metric();
106
107 # Marpa::R2::Display::End
108
109 $ambiguity_metric = 2 if $ambiguity_metric > 2; # cap at 2 -- higher numbers not defined
110 my $expected_metric = (scalar keys %{$expected} > 1 ? 2 : 1);
111 Test::More::is($ambiguity_metric, $expected_metric, "Ambiguity check for length $i");
112
113 while ( my $value_ref = $recce->value() ) {
114
115 my $value = $value_ref ? ${$value_ref} : 'No parse';
116 if ( defined $expected->{$value} ) {
117 delete $expected->{$value};
118 Test::More::pass(qq{Expected result for length=$i, "$value"});
119 }
120 else {
121 Test::More::fail(qq{Unexpected result for length=$i, "$value"});
122 }
123 } ## end while ( my $value_ref = $recce->value() )
124
125 for my $value ( keys %{$expected} ) {
126 Test::More::fail(qq{Missing result for length=$i, "$value"});
127 }
128
129 my $ambiguity_desc = 'No ambiguity';
130 if ($ambiguity_metric > 1) {
131
132 $recce->series_restart( { end => $i } );
133 my $asf = Marpa::R2::ASF->new( { slr => $recce } );
134 die 'No ASF' if not defined $asf;
135 my $ambiguities = Marpa::R2::Internal::ASF::ambiguities($asf);
136
137 # Only report the first two
138 my @ambiguities = grep {defined} @{$ambiguities}[ 0 .. 1 ];
139
140 $ambiguity_desc =
141 Marpa::R2::Internal::ASF::ambiguities_show( $asf, \@ambiguities );
142 }
143
144 Marpa::R2::Test::is($ambiguity_desc, $ambiguity_expected[$i], "Ambiguity description for length $i");
145
146 } ## end for my $i ( 0 .. $input_length )
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: