Debian Codegraph @

t/leo3.t

raw
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 # augmented to test Leo prediction items.
18 #
19
20 use 5.010001;
21 use strict;
22 use warnings;
23
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 => 'S',
41 rules => [
42 [ 'S', [qw/a A/] ],
43 [ 'A', [qw/B/] ],
44 [ 'B', [qw/C/] ],
45 [ 'C', [qw/S/] ],
46 [ 'S', [], ],
47 ],
48 terminals => [qw(a)],
49 default_action => 'main::default_action',
50 }
51 );
52
53 $grammar->precompute();
54
55 Marpa::R2::Test::is( $grammar->show_symbols(),
56 <<'END_OF_STRING', 'Leo166 Symbols' );
57 0: a, terminal
58 1: S
59 2: A
60 3: B
61 4: C
62 END_OF_STRING
63
64 Marpa::R2::Test::is( $grammar->show_rules,
65 <<'END_OF_STRING', 'Leo166 Rules' );
66 0: S -> a A
67 1: A -> B
68 2: B -> C
69 3: C -> S
70 4: S -> /* empty !used */
71 END_OF_STRING
72
73 Marpa::R2::Test::is( $grammar->show_ahms, <<'END_OF_STRING', 'Leo166 AHMs' );
74 AHM 0: postdot = "a"
75 S ::= . a A
76 AHM 1: postdot = "A"
77 S ::= a . A
78 AHM 2: completion
79 S ::= a A .
80 AHM 3: postdot = "a"
81 S ::= . a A[]
82 AHM 4: completion
83 S ::= a A[] .
84 AHM 5: postdot = "B"
85 A ::= . B
86 AHM 6: completion
87 A ::= B .
88 AHM 7: postdot = "C"
89 B ::= . C
90 AHM 8: completion
91 B ::= C .
92 AHM 9: postdot = "S"
93 C ::= . S
94 AHM 10: completion
95 C ::= S .
96 AHM 11: postdot = "S"
97 S['] ::= . S
98 AHM 12: completion
99 S['] ::= S .
100 END_OF_STRING
101
102 my $length = 20;
103
104 LEO_FLAG: for my $leo_flag ( 0, 1 ) {
105 my $recce = Marpa::R2::Recognizer->new(
106 { grammar => $grammar, leo => $leo_flag } );
107
108 my $i = 0;
109 my $latest_earley_set = $recce->latest_earley_set();
110 my $max_size = $recce->earley_set_size($latest_earley_set);
111 TOKEN: while ( $i++ < $length ) {
112 $recce->read( 'a', 'a' );
113 $latest_earley_set = $recce->latest_earley_set();
114 my $size = $recce->earley_set_size($latest_earley_set);
115
116 $max_size = $size > $max_size ? $size : $max_size;
117 } ## end while ( $i++ < $length )
118
119 # Note that the length formula only works
120 # beginning with Earley set c, for some small
121 # constant c
122 my $expected_size = $leo_flag ? 9 : ( $length - 1 ) * 4 + 8;
123 Marpa::R2::Test::is( $max_size, $expected_size,
124 "Leo flag $leo_flag, size $max_size" );
125
126 my $value_ref = $recce->value();
127 my $value = $value_ref ? ${$value_ref} : 'No parse';
128 Marpa::R2::Test::is( $value, 'a' x $length, 'Leo p166 parse' );
129
130 } ## end for my $leo_flag ( 0, 1 )
131
132 1; # In case used as "do" file
133
134 # Local Variables:
135 # mode: cperl
136 # cperl-indent-level: 4
137 # fill-column: 100
138 # End:
139 # vim: expandtab shiftwidth=4: