| 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 |
# A variation on |
| 20 |
# the example grammar in Aycock/Horspool "Practical Earley Parsing", |
| 21 |
# _The Computer Journal_, Vol. 45, No. 6, pp. 620-630, |
| 22 |
# Its order of ambiguity generates Pascal's triangle. |
| 23 |
|
| 24 |
use strict; |
| 25 |
use warnings; |
| 26 |
|
| 27 |
use Test::More tests => 6; |
| 28 |
use lib 'inc'; |
| 29 |
use Marpa::R2::Test; |
| 30 |
use Marpa::R2; |
| 31 |
|
| 32 |
my $base_dsl = <<'END_OF_BASE_DSL'; |
| 33 |
:start ::= S |
| 34 |
A ::= 'a' | E |
| 35 |
E ::= # empty |
| 36 |
END_OF_BASE_DSL |
| 37 |
|
| 38 |
sub ah_extended { |
| 39 |
my $n = shift; |
| 40 |
|
| 41 |
my $full_dsl = $base_dsl . join q{ }, 'S', '::=', ( ('A') x $n ); |
| 42 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$full_dsl, } ); |
| 43 |
my $input = 'a' x $n; |
| 44 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 45 |
$recce->read( \$input ); |
| 46 |
|
| 47 |
my @parse_counts = (1); |
| 48 |
for my $loc ( 1 .. $n ) { |
| 49 |
my $parse_number = 0; |
| 50 |
|
| 51 |
$recce->series_restart( { end => $loc } ); |
| 52 |
my $asf = Marpa::R2::ASF->new( { slr => $recce , factoring_max => 1000} ); |
| 53 |
$parse_counts[$loc] = $asf->traverse( |
| 54 |
{}, |
| 55 |
sub { |
| 56 |
my ($glade) = @_; |
| 57 |
my $glade_count = 0; |
| 58 |
do { |
| 59 |
my $rule_count = 1; |
| 60 |
if ( defined $glade->rule_id() ) { |
| 61 |
$rule_count *= $glade->rh_value($_) |
| 62 |
for 0 .. $glade->rh_length() - 1; |
| 63 |
} |
| 64 |
$glade_count += $rule_count; |
| 65 |
$glade->literal(); |
| 66 |
} while defined $glade->next(); |
| 67 |
return $glade_count; |
| 68 |
} |
| 69 |
); |
| 70 |
|
| 71 |
} ## end for my $loc ( 0 .. $n ) |
| 72 |
return join q{ }, @parse_counts; |
| 73 |
} ## end sub ah_extended |
| 74 |
|
| 75 |
# In the NAIF, the zero case was one of my more important tests, |
| 76 |
# but allowing a SLIF whose lexers are never used seems pointless. |
| 77 |
my @answers = ( |
| 78 |
undef, |
| 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 ( ( 1 .. 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: set expandtab shiftwidth=4: |