| 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 => 10; |
| 28 |
use lib 'inc'; |
| 29 |
use Marpa::R2::Test; |
| 30 |
use Marpa::R2; |
| 31 |
|
| 32 |
my $n = 10; |
| 33 |
|
| 34 |
my $dsl = <<'=== END_OF_DSL ==='; |
| 35 |
:default ::= action => [name,values] |
| 36 |
lexeme default = latm => 1 |
| 37 |
|
| 38 |
sequence ::= A action => main::one |
| 39 |
| A sequence action => main::add |
| 40 |
A ::= 'a' action => main::one |
| 41 |
=== END_OF_DSL === |
| 42 |
|
| 43 |
sub one { return 1 } |
| 44 |
sub add { my (undef, $left, $right) = @_; |
| 45 |
# say STDERR join " ", "args:", @_; |
| 46 |
return $left+$right } |
| 47 |
|
| 48 |
my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } ); |
| 49 |
my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar } ); |
| 50 |
my $input = 'a' x $n; |
| 51 |
$recce->read( \$input, 0, 0 ); |
| 52 |
|
| 53 |
my @parse_counts = (1); |
| 54 |
for my $loc ( 1 .. $n ) { |
| 55 |
my $parse_number = 0; |
| 56 |
|
| 57 |
$recce->series_restart(); |
| 58 |
$recce->resume( undef, 1 ); |
| 59 |
die "No parse" if not my $value_ref = $recce->value(); |
| 60 |
local $Data::Dumper::Deepcopy = 1; |
| 61 |
# say STDERR Data::Dumper::Dumper($value_ref); |
| 62 |
my $actual = ${$value_ref}; |
| 63 |
Marpa::R2::Test::is( $actual, $loc, |
| 64 |
"Count $loc of incremental read" ); |
| 65 |
|
| 66 |
} ## end for my $loc ( 1 .. $n ) |
| 67 |
|
| 68 |
1; # In case used as "do" file |
| 69 |
|
| 70 |
# Local Variables: |
| 71 |
# mode: cperl |
| 72 |
# cperl-indent-level: 4 |
| 73 |
# fill-column: 100 |
| 74 |
# End: |
| 75 |
# vim: set expandtab shiftwidth=4: |