Debian Codegraph @

t/sl_ambig.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 # Tests of ambiguity detection in the target grammar
18 # (as opposed to the SLIF DSL itself).
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
24 use Test::More tests => 11;
25 use English qw( -no_match_vars );
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29 use Data::Dumper;
30
31 our $DEBUG = 0;
32
33 my $source = \(<<'END_OF_SOURCE');
34 :default ::= action => ::array
35 pair ::= duple | item item
36 duple ::= item item
37 item ::= Hesperus | Phosphorus
38 Hesperus ::= 'a'
39 Phosphorus ::= 'a'
40 END_OF_SOURCE
41
42 my $input = 'aa';
43 my $expected_value = 'Application grammar is ambiguous';
44 my $expected_result = <<'END_OF_MESSAGE';
45 Ambiguous symch at Glade=2, Symbol=<pair>:
46 The ambiguity is from line 1, column 1 to line 1, column 2
47 Text is: aa
48 There are 2 symches
49 Symch 0 is a rule: pair ::= duple
50 Symch 1 is a rule: pair ::= item item
51 END_OF_MESSAGE
52 my $test_name = 'Symch ambiguity';
53
54 my $grammar = Marpa::R2::Scanless::G->new( { source => $source } );
55 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } );
56 my $is_ambiguous_parse = 1;
57
58 my ( $actual_value, $actual_result );
59 PROCESSING: {
60
61 if ( not defined eval { $recce->read( \$input ); 1 } ) {
62 say $EVAL_ERROR if $DEBUG;
63 my $abbreviated_error = $EVAL_ERROR;
64 chomp $abbreviated_error;
65 $abbreviated_error =~ s/\n.*//xms;
66 $actual_value = 'No parse';
67 $actual_result = $abbreviated_error;
68 $is_ambiguous_parse = 0;
69 last PROCESSING;
70 } ## end if ( not defined eval { $recce->read( \$input ); 1 })
71
72 # Marpa::R2::Display
73 # name: ASF ambiguity reporting
74
75 if ( $recce->ambiguity_metric() > 1 ) {
76 my $asf = Marpa::R2::ASF->new( { slr => $recce } );
77 die 'No ASF' if not defined $asf;
78 my $ambiguities = Marpa::R2::Internal::ASF::ambiguities($asf);
79
80 # Only report the first two
81 my @ambiguities = grep {defined} @{$ambiguities}[ 0 .. 1 ];
82
83 $actual_value = 'Application grammar is ambiguous';
84 $actual_result =
85 Marpa::R2::Internal::ASF::ambiguities_show( $asf, \@ambiguities );
86 last PROCESSING;
87 } ## end if ( $recce->ambiguity_metric() > 1 )
88
89 # Marpa::R2::Display::End
90
91 $is_ambiguous_parse = 0;
92
93 my $value_ref = $recce->value();
94 if ( not defined $value_ref ) {
95 $actual_value = 'No parse';
96 $actual_result = 'Input read to end but no parse';
97 last PROCESSING;
98 }
99 $actual_value = ${$value_ref};
100 $actual_result = 'Parse OK';
101 last PROCESSING;
102
103 } ## end PROCESSING:
104
105 Test::More::is(
106 Data::Dumper::Dumper( \$actual_value ),
107 Data::Dumper::Dumper( \$expected_value ),
108 qq{Value of $test_name}
109 );
110 Test::More::is( $actual_result, $expected_result, qq{Result of $test_name} );
111
112 if ( !$is_ambiguous_parse ) {
113 Test::More::fail(qq{glade_span() start});
114 Test::More::fail(qq{glade_span() length});
115 }
116 else {
117 $recce->series_restart();
118 my $asf = Marpa::R2::ASF->new( { slr => $recce } );
119 my $glade_id = $asf->peak;
120
121 # Marpa::R2::Display
122 # name: glade_span() example
123
124 my ( $glade_start, $glade_length ) = $asf->glade_span($glade_id);
125
126 # Marpa::R2::Display::End
127
128 Test::More::is( $glade_start, 0, qq{glade_span() start} );
129 Test::More::is( $glade_length, 2, qq{glade_span() length} );
130
131 } ## end else [ if ( !$is_ambiguous_parse ) ]
132
133 # Tests of ambiguity_metric() anb ambiguous() across all ranking methods
134 $source = \(<<'END_OF_SOURCE');
135 lexeme default = latm => 1
136
137 top ::= unchoice rank => 1
138 top ::= choice
139 unchoice ::= choice1
140 choice ::= choice1 | choice2
141 choice1 ::= A1 B1
142 choice2 ::= A2 B2
143 A1 ~ 'a'
144 A2 ~ 'a'
145 B1 ~ 'b'
146 B2 ~ 'b'
147
148 :discard ~ ws
149 ws ~ [\s]+
150 END_OF_SOURCE
151
152 $grammar = Marpa::R2::Scanless::G->new({ source => $source });
153
154 $input = q{a b};
155
156 for my $ranking_method ('none', 'rule', 'high_rule_only'){
157
158 $recce = Marpa::R2::Scanless::R->new({
159 grammar => $grammar,
160 ranking_method => $ranking_method,
161 } );
162
163 $recce->read(\$input);
164
165 if ($ranking_method eq 'high_rule_only'){
166 # count parses and test that there is only one
167 my $parse_count = 0;
168 while (defined $recce->value()) { ++$parse_count }
169 Test::More::is( $parse_count, 1, "$ranking_method ranking, single parse" );
170 # reset recognizer and test ambiguity methods
171 $recce->series_restart();
172 Test::More::is( $recce->ambiguous(), '', "$ranking_method ranking, single parse, ambiguous status is empty" );
173 Test::More::is( $recce->ambiguity_metric(), 1, "$ranking_method ranking, single parse, ambiguity metric is 1" );
174 }
175 else{
176 Test::More::isnt( $recce->ambiguous(), '', "$ranking_method ranking, many parses, ambiguous status isn't empty" );
177 Test::More::ok( $recce->ambiguity_metric() > 1, "$ranking_method ranking, many parses, ambiguity metric > 1" );
178 }
179 } ## end for my $ranking_method ...
180
181 # vim: expandtab shiftwidth=4: