| 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 => 3; |
| 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 '(' . join( q{;}, @vals ) . ')'; |
| 36 |
} ## end sub default_action |
| 37 |
|
| 38 |
sub rule_na { |
| 39 |
shift; |
| 40 |
return 'na(' . ( join q{;}, map { $_ // q{-} } @_ ) . ')'; |
| 41 |
} |
| 42 |
|
| 43 |
sub rule_Snf { |
| 44 |
shift; |
| 45 |
return 'Snf(' . ( join q{;}, ( map { $_ // q{-} } @_ ) ) . ')'; |
| 46 |
} |
| 47 |
|
| 48 |
sub rule_fa { |
| 49 |
shift; |
| 50 |
return 'fa(' . ( join q{;}, ( map { $_ // q{-} } @_ ) ) . ')'; |
| 51 |
} |
| 52 |
|
| 53 |
sub rule_fS { |
| 54 |
shift; |
| 55 |
return 'fS(' . ( join q{;}, ( map { $_ // q{-} } @_ ) ) . ')'; |
| 56 |
} |
| 57 |
|
| 58 |
## use critic |
| 59 |
|
| 60 |
my $grammar = Marpa::R2::Grammar->new( |
| 61 |
{ start => 'S', |
| 62 |
infinite_action => 'quiet', |
| 63 |
rules => [ |
| 64 |
{ lhs => 'S', rhs => [qw/n f/], action => 'main::rule_Snf' }, |
| 65 |
{ lhs => 'n', rhs => ['a'], action => 'main::rule_na' }, |
| 66 |
{ lhs => 'n', rhs => [] }, |
| 67 |
{ lhs => 'f', rhs => ['a'], action => 'main::rule_fa' }, |
| 68 |
{ lhs => 'f', rhs => [] }, |
| 69 |
{ lhs => 'f', rhs => ['S'], action => 'main::rule_fS' }, |
| 70 |
], |
| 71 |
terminals => [qw(a)], |
| 72 |
default_action => 'main::default_action', |
| 73 |
} |
| 74 |
); |
| 75 |
|
| 76 |
$grammar->precompute(); |
| 77 |
|
| 78 |
my @expected2 = qw{ |
| 79 |
Snf(-;fS(Snf(na(A);fS(Snf(-;fa(A)))))) |
| 80 |
Snf(-;fS(Snf(na(A);fS(Snf(na(A);-))))) |
| 81 |
Snf(-;fS(Snf(na(A);fa(A)))) |
| 82 |
Snf(na(A);fS(Snf(-;fa(A)))) |
| 83 |
Snf(na(A);fS(Snf(na(A);-))) |
| 84 |
Snf(na(A);fa(A)) |
| 85 |
}; |
| 86 |
|
| 87 |
my @expected3 = qw{ |
| 88 |
Snf(-;fS(Snf(na(A);fS(Snf(na(A);fS(Snf(-;fa(A)))))))) |
| 89 |
Snf(-;fS(Snf(na(A);fS(Snf(na(A);fS(Snf(na(A);-))))))) |
| 90 |
Snf(-;fS(Snf(na(A);fS(Snf(na(A);fa(A)))))) |
| 91 |
Snf(na(A);fS(Snf(na(A);fS(Snf(-;fa(A)))))) |
| 92 |
Snf(na(A);fS(Snf(na(A);fS(Snf(na(A);-))))) |
| 93 |
Snf(na(A);fS(Snf(na(A);fa(A)))) |
| 94 |
}; |
| 95 |
|
| 96 |
my @expected = ( |
| 97 |
[q{}], |
| 98 |
[ qw{ |
| 99 |
Snf(-;fa(A)) |
| 100 |
Snf(-;fS(Snf(na(A);-))) |
| 101 |
Snf(na(A);-) |
| 102 |
} |
| 103 |
], |
| 104 |
\@expected2, |
| 105 |
\@expected3, |
| 106 |
); |
| 107 |
|
| 108 |
for my $input_length ( 1 .. 3 ) { |
| 109 |
my $recce = Marpa::R2::Recognizer->new( |
| 110 |
{ grammar => $grammar, max_parses => 99 } ); |
| 111 |
for my $token_ix ( 1 .. $input_length ) { |
| 112 |
$recce->read( 'a', 'A' ); |
| 113 |
} |
| 114 |
my $expected = $expected[$input_length]; |
| 115 |
my @values = (); |
| 116 |
while ( my $value_ref = $recce->value() ) { |
| 117 |
push @values, ${$value_ref}; |
| 118 |
} |
| 119 |
my $values = join "\n", sort @values; |
| 120 |
my $expected_values = join "\n", sort @{$expected}; |
| 121 |
|
| 122 |
# die if $values ne $expected_values; |
| 123 |
Marpa::R2::Test::is( $values, $expected_values, |
| 124 |
"value for input length $input_length" ); |
| 125 |
} ## end for my $input_length ( 1 .. 3 ) |
| 126 |
|
| 127 |
1; # In case used as "do" file |
| 128 |
|
| 129 |
# Local Variables: |
| 130 |
# mode: cperl |
| 131 |
# cperl-indent-level: 4 |
| 132 |
# fill-column: 100 |
| 133 |
# End: |
| 134 |
# vim: expandtab shiftwidth=4: |