| 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 |
# the example grammar in Aycock/Horspool "Practical Earley Parsing", |
| 18 |
# _The Computer Journal_, Vol. 45, No. 6, pp. 620-630, |
| 19 |
# in its "NNF" form |
| 20 |
|
| 21 |
use 5.010001; |
| 22 |
use strict; |
| 23 |
use warnings; |
| 24 |
|
| 25 |
use Test::More tests => 10; |
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
use Marpa::R2; |
| 29 |
|
| 30 |
## no critic (Subroutines::RequireArgUnpacking) |
| 31 |
|
| 32 |
sub default_action { |
| 33 |
shift; |
| 34 |
my $v_count = scalar @_; |
| 35 |
return q{} if $v_count <= 0; |
| 36 |
return $_[0] if $v_count == 1; |
| 37 |
return '(' . join( q{;}, @_ ) . ')'; |
| 38 |
} ## end sub default_action |
| 39 |
|
| 40 |
## use critic |
| 41 |
|
| 42 |
sub gen_grammar { |
| 43 |
my ($null_ranking) = @_; |
| 44 |
my $grammar = Marpa::R2::Grammar->new( |
| 45 |
{ start => 'S', |
| 46 |
rules => [ |
| 47 |
{ lhs => 'S', |
| 48 |
rhs => [qw/A A A A/], |
| 49 |
null_ranking => $null_ranking |
| 50 |
}, |
| 51 |
[ 'A', [qw/a/] ], |
| 52 |
['A'], |
| 53 |
], |
| 54 |
default_action => 'main::default_action', |
| 55 |
} |
| 56 |
); |
| 57 |
$grammar->set( { terminals => ['a'], } ); |
| 58 |
$grammar->precompute(); |
| 59 |
return $grammar; |
| 60 |
} ## end sub gen_grammar |
| 61 |
|
| 62 |
my @maximal = ( q{}, qw[(a;;;) (a;a;;) (a;a;a;) (a;a;a;a)] ); |
| 63 |
my @minimal = ( q{}, qw[(;;;a) (;;a;a) (;a;a;a) (a;a;a;a)] ); |
| 64 |
|
| 65 |
for my $maximal ( 0, 1 ) { |
| 66 |
my $grammar = gen_grammar( $maximal ? 'low' : 'high' ); |
| 67 |
my $recce = Marpa::R2::Recognizer->new( |
| 68 |
{ grammar => $grammar, ranking_method => 'high_rule_only' } ); |
| 69 |
|
| 70 |
my $input_length = 4; |
| 71 |
for ( 1 .. $input_length ) { $recce->read( 'a', 'a' ); } |
| 72 |
|
| 73 |
for my $i ( 0 .. $input_length ) { |
| 74 |
my $expected = $maximal ? \@maximal : \@minimal; |
| 75 |
my $name = $maximal ? 'maximal' : 'minimal'; |
| 76 |
$recce->reset_evaluation(); |
| 77 |
$recce->set( { end => $i, } ); |
| 78 |
my $result = $recce->value(); |
| 79 |
Test::More::is( ${$result}, $expected->[$i], |
| 80 |
"$name parse, length=$i" ); |
| 81 |
|
| 82 |
} ## end for my $i ( 0 .. $input_length ) |
| 83 |
} ## end for my $maximal ( 0, 1 ) |
| 84 |
|
| 85 |
1; # In case used as "do" file |
| 86 |
|
| 87 |
# Local Variables: |
| 88 |
# mode: cperl |
| 89 |
# cperl-indent-level: 4 |
| 90 |
# fill-column: 100 |
| 91 |
# End: |
| 92 |
# vim: expandtab shiftwidth=4: |