Debian Codegraph @

t/progress1.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
17 # Note: ah2.t and bocage.t folded into this test
18
19 # the example grammar in Aycock/Horspool "Practical Earley Parsing",
20 # _The Computer Journal_, Vol. 45, No. 6, pp. 620-630,
21 # in its "NNF" form
22
23 use 5.010001;
24 use strict;
25 use warnings;
26
27 use Test::More tests => 12;
28 use POSIX qw(setlocale LC_ALL);
29
30 POSIX::setlocale(LC_ALL, "C");
31
32 use lib 'inc';
33 use Marpa::R2::Test;
34 use Marpa::R2;
35
36 ## no critic (Subroutines::RequireArgUnpacking)
37
38 sub default_action {
39 my (undef, $v) = @_;
40 my $v_count = scalar @{$v};
41 return q{} if $v_count <= 0;
42 return $v->[0] if $v_count == 1;
43 return '(' . ( join q{;}, @{$v}) . ')';
44 }
45
46 ## use critic
47
48 my $dsl = <<'END_OF_DSL';
49 :default ::= action => main::default_action
50 :start ::= S
51 S ::= A A A A A A A
52 A ::=
53 A ::= 'a'
54 END_OF_DSL
55
56 my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl });
57
58 GRAMMAR_TESTS_FOLDED_FROM_ah2_t: {
59
60 Marpa::R2::Test::is( $grammar->show_rules, <<'EOS', 'Aycock/Horspool Rules' );
61 G1 R0 S ::= A A A A A A A
62 G1 R1 A ::=
63 G1 R2 A ::= 'a'
64 G1 R3 :start ::= S
65 EOS
66
67 Marpa::R2::Test::is( $grammar->show_symbols,
68 <<'EOS', 'Aycock/Horspool Symbols' );
69 G1 S0 :start -- Internal G1 start symbol
70 G1 S1 'a' -- Internal lexical symbol for "'a'"
71 G1 S2 S
72 G1 S3 A
73 EOS
74
75 Marpa::R2::Test::is( $grammar->show_irls,
76 <<'EOS', 'Aycock/Horspool IRLs' );
77 0: S -> A S[R0:1]
78 1: S -> A A[] A[] A[] A[] A[] A[]
79 2: S -> A[] S[R0:1]
80 3: S[R0:1] -> A S[R0:2]
81 4: S[R0:1] -> A A[] A[] A[] A[] A[]
82 5: S[R0:1] -> A[] S[R0:2]
83 6: S[R0:2] -> A S[R0:3]
84 7: S[R0:2] -> A A[] A[] A[] A[]
85 8: S[R0:2] -> A[] S[R0:3]
86 9: S[R0:3] -> A S[R0:4]
87 10: S[R0:3] -> A A[] A[] A[]
88 11: S[R0:3] -> A[] S[R0:4]
89 12: S[R0:4] -> A S[R0:5]
90 13: S[R0:4] -> A A[] A[]
91 14: S[R0:4] -> A[] S[R0:5]
92 15: S[R0:5] -> A A
93 16: S[R0:5] -> A A[]
94 17: S[R0:5] -> A[] A
95 18: A -> [Lex-0]
96 19: [:start] -> S
97 20: [:start]['] -> [:start]
98 EOS
99
100 }
101
102 Marpa::R2::Test::is( $grammar->show_isys,
103 <<'EOS', 'Aycock/Horspool ISYs' );
104 0: [:start]
105 1: [:start][], nulling
106 2: [Lex-0]
107 3: S
108 4: S[], nulling
109 5: A
110 6: A[], nulling
111 7: S[R0:1]
112 8: S[R0:2]
113 9: S[R0:3]
114 10: S[R0:4]
115 11: S[R0:5]
116 12: [:start][']
117 EOS
118
119 my ($S_sym) = grep { $grammar->symbol_name($_) eq 'S' } $grammar->symbol_ids();
120 my ($target_rule) = grep { ($grammar->rule_expand($_))[0] eq $S_sym } $grammar->rule_ids();
121 my $target_rule_length = -1 + scalar (() = $grammar->rule_expand($target_rule));
122
123 my $recce = Marpa::R2::Scanless::R->new( { grammar => $grammar });
124 my $input_length = 7;
125 my $input = ('a' x $input_length);
126 $recce->read( \$input );
127
128 sub earley_set_display {
129 my ($earley_set) = @_;
130 my @target_items =
131 grep { $_->[0] eq $target_rule } @{ $recce->progress($earley_set) };
132 my @data = ();
133 for my $target_item (@target_items) {
134 my ( $rule_id, $dot, $origin ) = @{$target_item};
135 # rule length is constant in this example
136 my $cooked_dot = $dot < 0 ? $target_rule_length : $dot;
137 my $desc .=
138 "S:$dot " . '@'
139 . "$origin-$earley_set "
140 . $grammar->show_dotted_rule( $rule_id, $cooked_dot );
141 my @datum = ( $cooked_dot, $origin, $rule_id, $dot, $origin, $desc );
142 push @data, \@datum;
143 }
144 my @sorted = map { $_->[-1] } sort { $a->[0] <=> $b->[0] || $a->[1] <=> $b->[1] } @data;
145 return join "\n", "=== Earley Set $earley_set ===", @sorted, '';
146 }
147
148 Marpa::R2::Test::is( earley_set_display(0), <<'EOS', 'Earley Set 0' );
149 === Earley Set 0 ===
150 S:0 @0-0 S -> . A A A A A A A
151 S:1 @0-0 S -> A . A A A A A A
152 S:2 @0-0 S -> A A . A A A A A
153 S:3 @0-0 S -> A A A . A A A A
154 S:4 @0-0 S -> A A A A . A A A
155 S:5 @0-0 S -> A A A A A . A A
156 S:6 @0-0 S -> A A A A A A . A
157 EOS
158
159 Marpa::R2::Test::is( earley_set_display(1), <<'EOS', 'Earley Set 1' );
160 === Earley Set 1 ===
161 S:1 @0-1 S -> A . A A A A A A
162 S:2 @0-1 S -> A A . A A A A A
163 S:3 @0-1 S -> A A A . A A A A
164 S:4 @0-1 S -> A A A A . A A A
165 S:5 @0-1 S -> A A A A A . A A
166 S:6 @0-1 S -> A A A A A A . A
167 S:-1 @0-1 S -> A A A A A A A .
168 EOS
169
170 Marpa::R2::Test::is( earley_set_display(2), <<'EOS', 'Earley Set 2' );
171 === Earley Set 2 ===
172 S:2 @0-2 S -> A A . A A A A A
173 S:3 @0-2 S -> A A A . A A A A
174 S:4 @0-2 S -> A A A A . A A A
175 S:5 @0-2 S -> A A A A A . A A
176 S:6 @0-2 S -> A A A A A A . A
177 S:-1 @0-2 S -> A A A A A A A .
178 EOS
179
180 Marpa::R2::Test::is( earley_set_display(3), <<'EOS', 'Earley Set 3' );
181 === Earley Set 3 ===
182 S:3 @0-3 S -> A A A . A A A A
183 S:4 @0-3 S -> A A A A . A A A
184 S:5 @0-3 S -> A A A A A . A A
185 S:6 @0-3 S -> A A A A A A . A
186 S:-1 @0-3 S -> A A A A A A A .
187 EOS
188
189 Marpa::R2::Test::is( earley_set_display(4), <<'EOS', 'Earley Set 4' );
190 === Earley Set 4 ===
191 S:4 @0-4 S -> A A A A . A A A
192 S:5 @0-4 S -> A A A A A . A A
193 S:6 @0-4 S -> A A A A A A . A
194 S:-1 @0-4 S -> A A A A A A A .
195 EOS
196
197 Marpa::R2::Test::is( earley_set_display(5), <<'EOS', 'Earley Set 5' );
198 === Earley Set 5 ===
199 S:5 @0-5 S -> A A A A A . A A
200 S:6 @0-5 S -> A A A A A A . A
201 S:-1 @0-5 S -> A A A A A A A .
202 EOS
203
204 Marpa::R2::Test::is( earley_set_display(6), <<'EOS', 'Earley Set 6' );
205 === Earley Set 6 ===
206 S:6 @0-6 S -> A A A A A A . A
207 S:-1 @0-6 S -> A A A A A A A .
208 EOS
209
210 Marpa::R2::Test::is( earley_set_display(7), <<'EOS', 'Earley Set 7' );
211 === Earley Set 7 ===
212 S:-1 @0-7 S -> A A A A A A A .
213 EOS
214
215 # vim: expandtab shiftwidth=4: