| 1 |
#!/usr/bin/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 |
# Test of deletion of duplicate parses. |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use Test::More tests => 5; |
| 24 |
|
| 25 |
use lib 'inc'; |
| 26 |
use Marpa::R2::Test; |
| 27 |
use Marpa::R2; |
| 28 |
|
| 29 |
## no critic (Subroutines::RequireArgUnpacking) |
| 30 |
|
| 31 |
sub default_action { |
| 32 |
shift; |
| 33 |
my $v_count = scalar @_; |
| 34 |
return q{-} if $v_count <= 0; |
| 35 |
my @vals = map { $_ // q{-} } @_; |
| 36 |
return $vals[0] if $v_count == 1; |
| 37 |
return '(' . join( q{;}, @vals ) . ')'; |
| 38 |
} ## end sub default_action |
| 39 |
|
| 40 |
## use critic |
| 41 |
|
| 42 |
my $grammar = Marpa::R2::Grammar->new( |
| 43 |
{ start => 'S', |
| 44 |
|
| 45 |
rules => [ |
| 46 |
[ 'S', [qw/p p p n/], ], |
| 47 |
[ 'p', ['a'], ], |
| 48 |
[ 'p', [], ], |
| 49 |
[ 'n', ['a'], ], |
| 50 |
], |
| 51 |
terminals => ['a'], |
| 52 |
default_action => 'main::default_action', |
| 53 |
|
| 54 |
} |
| 55 |
); |
| 56 |
|
| 57 |
$grammar->precompute(); |
| 58 |
|
| 59 |
Marpa::R2::Test::is( $grammar->show_rules, |
| 60 |
<<'END_OF_STRING', 'duplicate parse 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', 'duplicate parse AHMs' ); |
| 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 |
use constant SPACE => 0x60; |
| 118 |
|
| 119 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } ); |
| 120 |
my $input_length = 3; |
| 121 |
for my $input_ix ( 1 .. $input_length ) { |
| 122 |
$recce->read( 'a', chr( SPACE + $input_ix ) ); |
| 123 |
} |
| 124 |
|
| 125 |
# Set max at 10 just in case there's an infinite loop. |
| 126 |
# This is for debugging, after all |
| 127 |
$recce->set( { max_parses => 10 } ); |
| 128 |
|
| 129 |
my %expected = map { ( $_ => 1 ) } qw( (-;a;b;c) (a;-;b;c) (a;b;-;c) ); |
| 130 |
|
| 131 |
while ( my $value_ref = $recce->value() ) { |
| 132 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 133 |
if ( defined $expected{$value} ) { |
| 134 |
delete $expected{$value}; |
| 135 |
Test::More::pass("Expected value: $value"); |
| 136 |
} |
| 137 |
else { |
| 138 |
Test::More::fail("Unexpected value: $value"); |
| 139 |
} |
| 140 |
} ## end while ( my $value_ref = $recce->value() ) |
| 141 |
|
| 142 |
1; # In case used as "do" file |
| 143 |
|
| 144 |
# Local Variables: |
| 145 |
# mode: cperl |
| 146 |
# cperl-indent-level: 4 |
| 147 |
# fill-column: 100 |
| 148 |
# End: |
| 149 |
# vim: expandtab shiftwidth=4: |