| 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 |
# Test of Leo logic for unit rule. |
| 18 |
|
| 19 |
use 5.010001; |
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
|
| 23 |
use List::Util; |
| 24 |
use Test::More tests => 7; |
| 25 |
|
| 26 |
use lib 'inc'; |
| 27 |
use Marpa::R2::Test; |
| 28 |
use Marpa::R2; |
| 29 |
|
| 30 |
## no critic (Subroutines::RequireArgUnpacking) |
| 31 |
|
| 32 |
sub main::default_action { |
| 33 |
shift; |
| 34 |
return ( join q{}, grep {defined} @_ ); |
| 35 |
} |
| 36 |
|
| 37 |
## use critic |
| 38 |
|
| 39 |
my $grammar = Marpa::R2::Grammar->new( |
| 40 |
{ start => 'A', |
| 41 |
rules => [ |
| 42 |
[ 'A', [qw/a B/] ], |
| 43 |
[ 'B', [qw/C/] ], |
| 44 |
[ 'C', [qw/c A/] ], |
| 45 |
[ 'C', [qw/c/] ], |
| 46 |
], |
| 47 |
terminals => [qw(a c)], |
| 48 |
default_action => 'main::default_action', |
| 49 |
} |
| 50 |
); |
| 51 |
|
| 52 |
$grammar->precompute(); |
| 53 |
|
| 54 |
Marpa::R2::Test::is( $grammar->show_symbols(), |
| 55 |
<<'END_OF_STRING', 'Leo166 Symbols' ); |
| 56 |
0: a, terminal |
| 57 |
1: c, terminal |
| 58 |
2: A |
| 59 |
3: B |
| 60 |
4: C |
| 61 |
END_OF_STRING |
| 62 |
|
| 63 |
Marpa::R2::Test::is( $grammar->show_rules, |
| 64 |
<<'END_OF_STRING', 'Leo166 Rules' ); |
| 65 |
0: A -> a B |
| 66 |
1: B -> C |
| 67 |
2: C -> c A |
| 68 |
3: C -> c |
| 69 |
END_OF_STRING |
| 70 |
|
| 71 |
|
| 72 |
Marpa::R2::Test::is( $grammar->show_ahms, <<'END_OF_STRING', 'Leo166 AHMs' ); |
| 73 |
AHM 0: postdot = "a" |
| 74 |
A ::= . a B |
| 75 |
AHM 1: postdot = "B" |
| 76 |
A ::= a . B |
| 77 |
AHM 2: completion |
| 78 |
A ::= a B . |
| 79 |
AHM 3: postdot = "C" |
| 80 |
B ::= . C |
| 81 |
AHM 4: completion |
| 82 |
B ::= C . |
| 83 |
AHM 5: postdot = "c" |
| 84 |
C ::= . c A |
| 85 |
AHM 6: postdot = "A" |
| 86 |
C ::= c . A |
| 87 |
AHM 7: completion |
| 88 |
C ::= c A . |
| 89 |
AHM 8: postdot = "c" |
| 90 |
C ::= . c |
| 91 |
AHM 9: completion |
| 92 |
C ::= c . |
| 93 |
AHM 10: postdot = "A" |
| 94 |
A['] ::= . A |
| 95 |
AHM 11: completion |
| 96 |
A['] ::= A . |
| 97 |
END_OF_STRING |
| 98 |
|
| 99 |
my $input = 'acacac'; |
| 100 |
my $length_of_input = length $input; |
| 101 |
|
| 102 |
LEO_FLAG: for my $leo_flag ( 0, 1 ) { |
| 103 |
my $recce = Marpa::R2::Recognizer->new( |
| 104 |
{ grammar => $grammar, leo => $leo_flag } ); |
| 105 |
|
| 106 |
my $i = 0; |
| 107 |
my $latest_earley_set = $recce->latest_earley_set(); |
| 108 |
my @sizes = ($recce->earley_set_size($latest_earley_set)); |
| 109 |
TOKEN: for ( my $i = 0; $i < $length_of_input; $i++ ) { |
| 110 |
my $token_name = substr( $input, $i, 1 ); |
| 111 |
|
| 112 |
# token name and value are the same |
| 113 |
$recce->read( $token_name, $token_name ); |
| 114 |
$latest_earley_set = $recce->latest_earley_set(); |
| 115 |
push @sizes, $recce->earley_set_size($latest_earley_set); |
| 116 |
|
| 117 |
} ## end TOKEN: for ( my $i = 0; $i < $length_of_input; $i++ ) |
| 118 |
|
| 119 |
my $max_size = List::Util::max(@sizes); |
| 120 |
my $expected_size = $leo_flag ? 5 : ( $length_of_input / 2 ) * 3 + 3; |
| 121 |
Marpa::R2::Test::is( $max_size, $expected_size, |
| 122 |
"Leo flag $leo_flag, size was $max_size but $expected_size was expected" ); |
| 123 |
|
| 124 |
my $value_ref = $recce->value(); |
| 125 |
my $value = $value_ref ? ${$value_ref} : 'No parse'; |
| 126 |
Marpa::R2::Test::is( $value, 'acacac', 'Leo unit rule parse' ); |
| 127 |
|
| 128 |
} ## end LEO_FLAG: for my $leo_flag ( 0, 1 ) |
| 129 |
|
| 130 |
# vim: expandtab shiftwidth=4: |