| 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 |
# This uses an ambiguous grammar to implement a binary |
| 18 |
# counter. A very expensive way to do it, but a |
| 19 |
# good test of the ranking logic. |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
|
| 25 |
use Test::More tests => 32; |
| 26 |
use English qw( -no_match_vars ); |
| 27 |
use lib 'inc'; |
| 28 |
use Marpa::R2::Test; |
| 29 |
use Marpa::R2; |
| 30 |
|
| 31 |
sub zero { return '0' } |
| 32 |
sub one { return '1' } |
| 33 |
|
| 34 |
sub start_rule_action { |
| 35 |
shift; |
| 36 |
return join q{}, @_; |
| 37 |
} |
| 38 |
|
| 39 |
## use critic |
| 40 |
|
| 41 |
sub gen_grammar { |
| 42 |
my ($is_count_up) = @_; |
| 43 |
my $grammar = Marpa::R2::Grammar->new( |
| 44 |
{ start => 'S', |
| 45 |
rules => [ |
| 46 |
{ lhs => 'S', |
| 47 |
rhs => [qw/digit digit digit digit/], |
| 48 |
action => 'main::start_rule_action' |
| 49 |
}, |
| 50 |
{ lhs => 'digit', |
| 51 |
rhs => ['zero'], |
| 52 |
rank => $is_count_up ? 1 : 0, |
| 53 |
action => 'main::zero' |
| 54 |
}, |
| 55 |
{ lhs => 'digit', |
| 56 |
rhs => ['one'], |
| 57 |
rank => $is_count_up ? 0 : 1, |
| 58 |
action => 'main::one' |
| 59 |
}, |
| 60 |
{ lhs => 'one', |
| 61 |
rhs => ['t'], |
| 62 |
}, |
| 63 |
{ lhs => 'zero', |
| 64 |
rhs => ['t'], |
| 65 |
}, |
| 66 |
], |
| 67 |
terminals => [qw(t)], |
| 68 |
} |
| 69 |
); |
| 70 |
return $grammar->precompute(); |
| 71 |
} ## end sub gen_grammar |
| 72 |
|
| 73 |
my @counting_up = |
| 74 |
qw{ 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 }; |
| 75 |
my @counting_down = reverse @counting_up; |
| 76 |
|
| 77 |
for my $is_count_up ( 1, 0 ) { |
| 78 |
my $count = $is_count_up ? ( \@counting_up ) : ( \@counting_down ); |
| 79 |
my $direction_desc = $is_count_up ? 'up' : 'down'; |
| 80 |
my $recce = Marpa::R2::Recognizer->new( |
| 81 |
{ grammar => gen_grammar($is_count_up), ranking_method => 'rule' } ); |
| 82 |
|
| 83 |
my $input_length = 4; |
| 84 |
for ( 1 .. $input_length ) { $recce->read('t'); } |
| 85 |
|
| 86 |
my $i = 0; |
| 87 |
while ( my $result = $recce->value() ) { |
| 88 |
my $got = ${$result}; |
| 89 |
my $expected = reverse $count->[$i]; |
| 90 |
Test::More::is( $got, $expected, "counting $direction_desc $i" ); |
| 91 |
$i++; |
| 92 |
} ## end while ( my $result = $recce->value() ) |
| 93 |
} ## end for my $is_count_up ( 1, 0 ) |
| 94 |
|
| 95 |
1; # In case used as "do" file |
| 96 |
|
| 97 |
# Local Variables: |
| 98 |
# mode: cperl |
| 99 |
# cperl-indent-level: 4 |
| 100 |
# fill-column: 100 |
| 101 |
# End: |
| 102 |
# vim: expandtab shiftwidth=4: |