| 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 |
# The example from p. 166 of Leo's paper. |
| 17 |
|
| 18 |
use 5.010001; |
| 19 |
use strict; |
| 20 |
use warnings; |
| 21 |
|
| 22 |
use Test::More tests => 9; |
| 23 |
|
| 24 |
use lib 'inc'; |
| 25 |
use Marpa::R2::Test; |
| 26 |
use Marpa::R2; |
| 27 |
|
| 28 |
## no critic (Subroutines::RequireArgUnpacking) |
| 29 |
|
| 30 |
sub main::default_action { |
| 31 |
shift; |
| 32 |
return ( join q{}, grep {defined} @_ ); |
| 33 |
} |
| 34 |
|
| 35 |
## use critic |
| 36 |
|
| 37 |
my $grammar = Marpa::R2::Grammar->new( |
| 38 |
{ start => 'S', |
| 39 |
rules => [ [ 'S', [qw/a S/] ], [ 'S', [], ], ], |
| 40 |
terminals => [qw(a)], |
| 41 |
default_action => 'main::default_action', |
| 42 |
} |
| 43 |
); |
| 44 |
|
| 45 |
$grammar->precompute(); |
| 46 |
|
| 47 |
Marpa::R2::Test::is( $grammar->show_symbols(), |
| 48 |
<<'END_OF_STRING', 'Leo166 Symbols' ); |
| 49 |
0: a, terminal |
| 50 |
1: S |
| 51 |
END_OF_STRING |
| 52 |
|
| 53 |
Marpa::R2::Test::is( $grammar->show_rules, |
| 54 |
<<'END_OF_STRING', 'Leo166 Rules' ); |
| 55 |
0: S -> a S |
| 56 |
1: S -> /* empty !used */ |
| 57 |
END_OF_STRING |
| 58 |
|
| 59 |
Marpa::R2::Test::is( $grammar->show_ahms, <<'END_OF_STRING', 'Leo166 AHMs' ); |
| 60 |
AHM 0: postdot = "a" |
| 61 |
S ::= . a S |
| 62 |
AHM 1: postdot = "S" |
| 63 |
S ::= a . S |
| 64 |
AHM 2: completion |
| 65 |
S ::= a S . |
| 66 |
AHM 3: postdot = "a" |
| 67 |
S ::= . a S[] |
| 68 |
AHM 4: completion |
| 69 |
S ::= a S[] . |
| 70 |
AHM 5: postdot = "S" |
| 71 |
S['] ::= . S |
| 72 |
AHM 6: completion |
| 73 |
S['] ::= S . |
| 74 |
END_OF_STRING |
| 75 |
|
| 76 |
my $length = 50; |
| 77 |
|
| 78 |
LEO_FLAG: for my $leo_flag ( 0, 1 ) { |
| 79 |
my $recce = Marpa::R2::Recognizer->new( |
| 80 |
{ grammar => $grammar, leo => $leo_flag } ); |
| 81 |
|
| 82 |
my $i = 0; |
| 83 |
|
| 84 |
# Marpa::R2::Display |
| 85 |
# name: latest_earley_set() Synopsis |
| 86 |
|
| 87 |
my $latest_earley_set = $recce->latest_earley_set(); |
| 88 |
|
| 89 |
# Marpa::R2::Display::End |
| 90 |
|
| 91 |
my $max_size = $recce->earley_set_size($latest_earley_set); |
| 92 |
TOKEN: while ( $i++ < $length ) { |
| 93 |
$recce->read( 'a', 'a' ); |
| 94 |
$latest_earley_set = $recce->latest_earley_set(); |
| 95 |
my $size = $recce->earley_set_size($latest_earley_set); |
| 96 |
$max_size = $size > $max_size ? $size : $max_size; |
| 97 |
} ## end while ( $i++ < $length ) |
| 98 |
|
| 99 |
my $expected_size = $leo_flag ? 6 : $length + 4; |
| 100 |
Marpa::R2::Test::is( $max_size, $expected_size, |
| 101 |
"Leo flag $leo_flag, size" ); |
| 102 |
|
| 103 |
my $value_ref = $recce->value(); |
| 104 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 105 |
Marpa::R2::Test::is( $value, 'a' x $length, 'Leo p166 parse' ); |
| 106 |
} ## end for my $leo_flag ( 0, 1 ) |
| 107 |
|
| 108 |
{ |
| 109 |
open my $trace_fh, q{>}, \( my $trace_output = q{} ); |
| 110 |
my $recce = Marpa::R2::Recognizer->new( |
| 111 |
{ grammar => $grammar, |
| 112 |
leo => 0, |
| 113 |
too_many_earley_items => $length, |
| 114 |
trace_fh => $trace_fh |
| 115 |
} |
| 116 |
); |
| 117 |
for ( 1 .. $length ) { $recce->read( 'a', 'a' ); } |
| 118 |
my $value_ref = $recce->value(); |
| 119 |
close $trace_fh; |
| 120 |
my $warning_found = |
| 121 |
( $trace_output |
| 122 |
=~ m/Earley[ ]item[ ]count[ ()\d]*exceeds[ ]warning[ ]threshold/xms |
| 123 |
); |
| 124 |
if ($warning_found) { |
| 125 |
Test::More::pass('Warns at earley item threshold'); |
| 126 |
} |
| 127 |
else { |
| 128 |
Marpa::R2::Test::is( $trace_output, q{}, 'Leo p166 parse' ); |
| 129 |
} |
| 130 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 131 |
Marpa::R2::Test::is( $value, 'a' x $length, 'Leo p166 parse' ); |
| 132 |
} |
| 133 |
|
| 134 |
1; # In case used as "do" file |
| 135 |
|
| 136 |
# Local Variables: |
| 137 |
# mode: cperl |
| 138 |
# cperl-indent-level: 4 |
| 139 |
# fill-column: 100 |
| 140 |
# End: |
| 141 |
# vim: expandtab shiftwidth=4: |