| 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 |
use 5.010001; |
| 18 |
|
| 19 |
# variations on |
| 20 |
# the example grammar in Aycock/Horspool "Practical Earley Parsing", |
| 21 |
# _The Computer Journal_, Vol. 45, No. 6, pp. 620-630, |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
|
| 26 |
use Test::More tests => 7; |
| 27 |
use lib 'inc'; |
| 28 |
use Marpa::R2::Test; |
| 29 |
use Marpa::R2; |
| 30 |
|
| 31 |
sub ah_extended { |
| 32 |
my $n = shift; |
| 33 |
|
| 34 |
my $g = Marpa::R2::Grammar->new( |
| 35 |
{ start => 'S', |
| 36 |
|
| 37 |
rules => [ |
| 38 |
[ 'S', [ ('A') x $n ] ], |
| 39 |
[ 'A', [qw/a/] ], |
| 40 |
[ 'A', [qw/E/] ], |
| 41 |
['E'], |
| 42 |
], |
| 43 |
terminals => ['a'], |
| 44 |
|
| 45 |
# no warnings for $n equals zero |
| 46 |
warnings => ( $n ? 1 : 0 ), |
| 47 |
} |
| 48 |
); |
| 49 |
$g->precompute(); |
| 50 |
|
| 51 |
my $recce = Marpa::R2::Recognizer->new( { grammar => $g } ); |
| 52 |
|
| 53 |
for my $token_ix ( 1 .. $n ) { |
| 54 |
$recce->read( 'a' ); |
| 55 |
} |
| 56 |
|
| 57 |
my @parse_counts; |
| 58 |
for my $loc ( 0 .. $n ) { |
| 59 |
my $parse_number = 0; |
| 60 |
|
| 61 |
# An arbitrary maximum is put on the number of parses -- this is for |
| 62 |
# debugging, and infinite loops happen. |
| 63 |
|
| 64 |
# Marpa::R2::Display |
| 65 |
# name: reset_evaluation Synopsis |
| 66 |
|
| 67 |
$recce->reset_evaluation(); |
| 68 |
$recce->set( { end => $loc, max_parses => 999, } ); |
| 69 |
|
| 70 |
# Marpa::R2::Display::End |
| 71 |
|
| 72 |
while ( $recce->value() ) { $parse_counts[$loc]++ } |
| 73 |
} ## end for my $loc ( 0 .. $n ) |
| 74 |
return join q{ }, @parse_counts; |
| 75 |
} ## end sub ah_extended |
| 76 |
|
| 77 |
my @answers = ( |
| 78 |
'1', |
| 79 |
'1 1', |
| 80 |
'1 2 1', |
| 81 |
'1 3 3 1', |
| 82 |
'1 4 6 4 1', |
| 83 |
'1 5 10 10 5 1', |
| 84 |
'1 6 15 20 15 6 1', |
| 85 |
'1 7 21 35 35 21 7 1', |
| 86 |
'1 8 28 56 70 56 28 8 1', |
| 87 |
'1 9 36 84 126 126 84 36 9 1', |
| 88 |
'1 10 45 120 210 252 210 120 45 10 1', |
| 89 |
); |
| 90 |
|
| 91 |
## no critic (ValuesAndExpressions::ProhibitMagicNumbers) |
| 92 |
for my $a ( ( 0 .. 5 ), 10 ) { |
| 93 |
## use critic |
| 94 |
|
| 95 |
Marpa::R2::Test::is( ah_extended($a), $answers[$a], |
| 96 |
"Row $a of Pascal's triangle matches parse counts" ); |
| 97 |
|
| 98 |
} ## end for my $a ( ( 0 .. 5 ), 10 ) |
| 99 |
|
| 100 |
1; # In case used as "do" file |
| 101 |
|
| 102 |
# Local Variables: |
| 103 |
# mode: cperl |
| 104 |
# cperl-indent-level: 4 |
| 105 |
# fill-column: 100 |
| 106 |
# End: |
| 107 |
# vim: expandtab shiftwidth=4: |