| 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 |
# Two rules which start with nullables, and cycle. |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
use Test::More tests => 9; |
| 23 |
|
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
use Marpa::R2; |
| 27 |
|
| 28 |
## no critic (Subroutines::RequireArgUnpacking) |
| 29 |
|
| 30 |
sub default_action { |
| 31 |
shift; |
| 32 |
my $v_count = scalar @_; |
| 33 |
return q{-} if $v_count <= 0; |
| 34 |
my @vals = map { $_ // q{-} } @_; |
| 35 |
return $_[0] if scalar @vals == 1; |
| 36 |
return '(' . join( q{;}, @vals ) . ')'; |
| 37 |
} ## end sub default_action |
| 38 |
|
| 39 |
## use critic |
| 40 |
|
| 41 |
my $grammar = Marpa::R2::Grammar->new( |
| 42 |
{ start => 'S', |
| 43 |
|
| 44 |
rules => [ |
| 45 |
[ 'S', [qw/p p p n/], ], |
| 46 |
[ 'p', ['t'], ], |
| 47 |
[ 'p', [], ], |
| 48 |
[ 'n', ['t'], ], |
| 49 |
[ 'n', ['r2'], ], |
| 50 |
[ 'r2', [qw/a b c d e x/], ], |
| 51 |
[ 'a', [] ], |
| 52 |
[ 'b', [] ], |
| 53 |
[ 'c', [] ], |
| 54 |
[ 'd', [] ], |
| 55 |
[ 'e', [] ], |
| 56 |
[ 'a', ['t'] ], |
| 57 |
[ 'b', ['t'] ], |
| 58 |
[ 'c', ['t'] ], |
| 59 |
[ 'd', ['t'] ], |
| 60 |
[ 'e', ['t'] ], |
| 61 |
[ 'x', ['t'], ], |
| 62 |
], |
| 63 |
terminals => ['t'], |
| 64 |
default_action => 'main::default_action', |
| 65 |
} |
| 66 |
); |
| 67 |
|
| 68 |
$grammar->precompute(); |
| 69 |
|
| 70 |
# The count of results without an r2 production, the count |
| 71 |
# is C(n-1,3), when n>=4, 0 otherwise. |
| 72 |
# The count of results with an r2 productions is C(n-1,8). |
| 73 |
# Total results is the sum of the results with an |
| 74 |
# r2 production and those without. |
| 75 |
my @expected_count; |
| 76 |
$expected_count[1] = 2; # 1 w/o r2; 1 with an r2 |
| 77 |
$expected_count[2] = 11; # 3 w/o r2; 8 with an r2 |
| 78 |
$expected_count[3] = 31; # 3 w/o r2; 28 with an r2 |
| 79 |
$expected_count[4] = 57; # 1 w/o r2; 56 with an r2 |
| 80 |
$expected_count[5] = 70; # 0 w/o r2; 70 with an r2 |
| 81 |
$expected_count[6] = 56; # 0 w/o r2; 70 with an r2 |
| 82 |
$expected_count[7] = 28; # 0 w/o r2; 28 with an r2 |
| 83 |
$expected_count[8] = 8; # 0 w/o r2; 8 with an r2 |
| 84 |
$expected_count[9] = 1; # 0 w/o r2; 1 with an r2 |
| 85 |
|
| 86 |
for my $input_length ( 1 .. 9 ) { |
| 87 |
my $recce = Marpa::R2::Recognizer->new( |
| 88 |
{ grammar => $grammar, max_parses => 100 } ); |
| 89 |
for ( 1 .. $input_length ) { $recce->read( 't', 't' ); } |
| 90 |
my $expected = 1; |
| 91 |
my $parse_count = 0; |
| 92 |
while ( $expected and my $value_ref = $recce->value() ) { |
| 93 |
$expected = 0; |
| 94 |
$parse_count++; |
| 95 |
my $value = ${$value_ref}; |
| 96 |
if ($value =~ m{ |
| 97 |
\A |
| 98 |
[(] |
| 99 |
((t|[-])[;]){3} |
| 100 |
(t|[-]) |
| 101 |
[)] |
| 102 |
\z |
| 103 |
}xms |
| 104 |
) |
| 105 |
{ |
| 106 |
$expected = 1; |
| 107 |
} ## end if ( $value =~ m{ ) (}) |
| 108 |
elsif ( |
| 109 |
$value =~ m{ |
| 110 |
\A |
| 111 |
[(] |
| 112 |
((t|[-])[;]){3} |
| 113 |
[(] |
| 114 |
((t|[-])[;]){5} |
| 115 |
(t|[-]) |
| 116 |
[)] |
| 117 |
[)] |
| 118 |
\z |
| 119 |
}xms |
| 120 |
) |
| 121 |
{ |
| 122 |
$expected = 1; |
| 123 |
} ## end elsif ( $value =~ m{ ) (}) |
| 124 |
$expected &&= $input_length == ( $value =~ tr/t/t/ ); |
| 125 |
if ( not $expected ) { |
| 126 |
Test::More::fail( |
| 127 |
qq{Unexpected value, length=$input_length, "$value"}); |
| 128 |
} |
| 129 |
} ## end while ( $expected and my $value_ref = $recce->value() ) |
| 130 |
if ($expected) { |
| 131 |
my $expected_count = $expected_count[$input_length]; |
| 132 |
if ( $parse_count == $expected_count ) { |
| 133 |
Test::More::pass( |
| 134 |
qq{Good parse count $parse_count; input length=$input_length} |
| 135 |
); |
| 136 |
} |
| 137 |
else { |
| 138 |
Test::More::fail( |
| 139 |
qq{Bad parse count $parse_count, expected $expected_count; input length=$input_length} |
| 140 |
); |
| 141 |
} |
| 142 |
} ## end if ($expected) |
| 143 |
} ## end for my $input_length ( 1 .. 9 ) |
| 144 |
|
| 145 |
1; # In case used as "do" file |
| 146 |
|
| 147 |
# Local Variables: |
| 148 |
# mode: cperl |
| 149 |
# cperl-indent-level: 4 |
| 150 |
# fill-column: 100 |
| 151 |
# End: |
| 152 |
# vim: expandtab shiftwidth=4: |