Debian Codegraph @

t/ah2.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 # the example grammar in Aycock/Horspool "Practical Earley Parsing",
18 # _The Computer Journal_, Vol. 45, No. 6, pp. 620-630,
19 # in its "NNF" form
20
21 use 5.010001;
22 use strict;
23 use warnings;
24
25 use Test::More tests => 25;
26 use lib 'inc';
27 use Marpa::R2::Test;
28 use Marpa::R2;
29
30 ## no critic (Subroutines::RequireArgUnpacking)
31
32 sub default_action {
33 shift;
34 my $v_count = scalar @_;
35 return q{} if $v_count <= 0;
36 return $_[0] if $v_count == 1;
37 return '(' . ( join q{;}, @_ ) . ')';
38 } ## end sub default_action
39
40 ## use critic
41
42 my $grammar = Marpa::R2::Grammar->new(
43 { start => 'S',
44 rules => [
45 [ 'S', [qw/A A A A/] ],
46 [ 'A', [qw/a/] ],
47 [ 'A', [qw/E/] ],
48 ['E'],
49 ],
50 default_action => 'main::default_action',
51 }
52 );
53
54 $grammar->set( { terminals => ['a'], } );
55
56 $grammar->precompute();
57
58 Marpa::R2::Test::is( $grammar->show_rules, <<'EOS', 'Aycock/Horspool Rules' );
59 0: S -> A A A A
60 1: A -> a
61 2: A -> E /* !used */
62 3: E -> /* empty !used */
63 EOS
64
65 Marpa::R2::Test::is( $grammar->show_symbols,
66 <<'EOS', 'Aycock/Horspool Symbols' );
67 0: S
68 1: A
69 2: a, terminal
70 3: E, nulling
71 EOS
72
73 Marpa::R2::Test::is( $grammar->show_isys,
74 <<'EOS', 'Aycock/Horspool ISYs' );
75 0: S
76 1: S[], nulling
77 2: A
78 3: A[], nulling
79 4: a
80 5: E[], nulling
81 6: S[R0:1]
82 7: S[R0:2]
83 8: S[']
84 EOS
85
86 Marpa::R2::Test::is( $grammar->show_irls,
87 <<'EOS', 'Aycock/Horspool IRLs' );
88 0: S -> A S[R0:1]
89 1: S -> A A[] A[] A[]
90 2: S -> A[] S[R0:1]
91 3: S[R0:1] -> A S[R0:2]
92 4: S[R0:1] -> A A[] A[]
93 5: S[R0:1] -> A[] S[R0:2]
94 6: S[R0:2] -> A A
95 7: S[R0:2] -> A A[]
96 8: S[R0:2] -> A[] A
97 9: A -> a
98 10: S['] -> S
99 EOS
100
101 Marpa::R2::Test::is(
102 $grammar->show_nulling_symbols,
103 q{E},
104 'Aycock/Horspool Nulling Symbols'
105 );
106 Marpa::R2::Test::is(
107 $grammar->show_productive_symbols,
108 q{A E S a},
109 'Aycock/Horspool Productive Symbols'
110 );
111 Marpa::R2::Test::is(
112 $grammar->show_accessible_symbols,
113 q{A E S a},
114 'Aycock/Horspool Accessible Symbols'
115 );
116
117 Marpa::R2::Test::is( $grammar->show_ahms(),
118 <<'EOS', 'Aycock/Horspool AHMs' );
119 AHM 0: postdot = "A"
120 S ::= . A S[R0:1]
121 AHM 1: postdot = "S[R0:1]"
122 S ::= A . S[R0:1]
123 AHM 2: completion
124 S ::= A S[R0:1] .
125 AHM 3: postdot = "A"
126 S ::= . A A[] A[] A[]
127 AHM 4: completion
128 S ::= A A[] A[] A[] .
129 AHM 5: postdot = "S[R0:1]"
130 S ::= A[] . S[R0:1]
131 AHM 6: completion
132 S ::= A[] S[R0:1] .
133 AHM 7: postdot = "A"
134 S[R0:1] ::= . A S[R0:2]
135 AHM 8: postdot = "S[R0:2]"
136 S[R0:1] ::= A . S[R0:2]
137 AHM 9: completion
138 S[R0:1] ::= A S[R0:2] .
139 AHM 10: postdot = "A"
140 S[R0:1] ::= . A A[] A[]
141 AHM 11: completion
142 S[R0:1] ::= A A[] A[] .
143 AHM 12: postdot = "S[R0:2]"
144 S[R0:1] ::= A[] . S[R0:2]
145 AHM 13: completion
146 S[R0:1] ::= A[] S[R0:2] .
147 AHM 14: postdot = "A"
148 S[R0:2] ::= . A A
149 AHM 15: postdot = "A"
150 S[R0:2] ::= A . A
151 AHM 16: completion
152 S[R0:2] ::= A A .
153 AHM 17: postdot = "A"
154 S[R0:2] ::= . A A[]
155 AHM 18: completion
156 S[R0:2] ::= A A[] .
157 AHM 19: postdot = "A"
158 S[R0:2] ::= A[] . A
159 AHM 20: completion
160 S[R0:2] ::= A[] A .
161 AHM 21: postdot = "a"
162 A ::= . a
163 AHM 22: completion
164 A ::= a .
165 AHM 23: postdot = "S"
166 S['] ::= . S
167 AHM 24: completion
168 S['] ::= S .
169 EOS
170
171 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
172
173 my $expected_earley_sets = <<'END_OF_SETS';
174 Last Completed: 4; Furthest: 4
175 Earley Set 0
176 ahm23: R10:0@0-0
177 R10:0: S['] ::= . S
178 ahm0: R0:0@0-0
179 R0:0: S ::= . A S[R0:1]
180 ahm3: R1:0@0-0
181 R1:0: S ::= . A A[] A[] A[]
182 ahm5: R2:1@0-0
183 R2:1: S ::= A[] . S[R0:1]
184 ahm7: R3:0@0-0
185 R3:0: S[R0:1] ::= . A S[R0:2]
186 ahm10: R4:0@0-0
187 R4:0: S[R0:1] ::= . A A[] A[]
188 ahm12: R5:1@0-0
189 R5:1: S[R0:1] ::= A[] . S[R0:2]
190 ahm14: R6:0@0-0
191 R6:0: S[R0:2] ::= . A A
192 ahm17: R7:0@0-0
193 R7:0: S[R0:2] ::= . A A[]
194 ahm19: R8:1@0-0
195 R8:1: S[R0:2] ::= A[] . A
196 ahm21: R9:0@0-0
197 R9:0: A ::= . a
198 Earley Set 1
199 ahm22: R9$@0-1
200 R9$: A ::= a .
201 [c=R9:0@0-0; s=a; t=\'a']
202 ahm20: R8$@0-1
203 R8$: S[R0:2] ::= A[] A .
204 [p=R8:1@0-0; c=R9$@0-1]
205 ahm18: R7$@0-1
206 R7$: S[R0:2] ::= A A[] .
207 [p=R7:0@0-0; c=R9$@0-1]
208 ahm15: R6:1@0-1
209 R6:1: S[R0:2] ::= A . A
210 [p=R6:0@0-0; c=R9$@0-1]
211 ahm11: R4$@0-1
212 R4$: S[R0:1] ::= A A[] A[] .
213 [p=R4:0@0-0; c=R9$@0-1]
214 ahm8: R3:1@0-1
215 R3:1: S[R0:1] ::= A . S[R0:2]
216 [p=R3:0@0-0; c=R9$@0-1]
217 ahm4: R1$@0-1
218 R1$: S ::= A A[] A[] A[] .
219 [p=R1:0@0-0; c=R9$@0-1]
220 ahm1: R0:1@0-1
221 R0:1: S ::= A . S[R0:1]
222 [p=R0:0@0-0; c=R9$@0-1]
223 ahm24: R10$@0-1
224 R10$: S['] ::= S .
225 [p=R10:0@0-0; c=R1$@0-1] [p=R10:0@0-0; c=R2$@0-1]
226 ahm6: R2$@0-1
227 R2$: S ::= A[] S[R0:1] .
228 [p=R2:1@0-0; c=R4$@0-1] [p=R2:1@0-0; c=R5$@0-1]
229 ahm13: R5$@0-1
230 R5$: S[R0:1] ::= A[] S[R0:2] .
231 [p=R5:1@0-0; c=R7$@0-1] [p=R5:1@0-0; c=R8$@0-1]
232 ahm21: R9:0@1-1
233 R9:0: A ::= . a
234 ahm14: R6:0@1-1
235 R6:0: S[R0:2] ::= . A A
236 ahm17: R7:0@1-1
237 R7:0: S[R0:2] ::= . A A[]
238 ahm19: R8:1@1-1
239 R8:1: S[R0:2] ::= A[] . A
240 ahm7: R3:0@1-1
241 R3:0: S[R0:1] ::= . A S[R0:2]
242 ahm10: R4:0@1-1
243 R4:0: S[R0:1] ::= . A A[] A[]
244 ahm12: R5:1@1-1
245 R5:1: S[R0:1] ::= A[] . S[R0:2]
246 Earley Set 2
247 ahm22: R9$@1-2
248 R9$: A ::= a .
249 [c=R9:0@1-1; s=a; t=\'a']
250 ahm11: R4$@1-2
251 R4$: S[R0:1] ::= A A[] A[] .
252 [p=R4:0@1-1; c=R9$@1-2]
253 ahm8: R3:1@1-2
254 R3:1: S[R0:1] ::= A . S[R0:2]
255 [p=R3:0@1-1; c=R9$@1-2]
256 ahm20: R8$@1-2
257 R8$: S[R0:2] ::= A[] A .
258 [p=R8:1@1-1; c=R9$@1-2]
259 ahm18: R7$@1-2
260 R7$: S[R0:2] ::= A A[] .
261 [p=R7:0@1-1; c=R9$@1-2]
262 ahm15: R6:1@1-2
263 R6:1: S[R0:2] ::= A . A
264 [p=R6:0@1-1; c=R9$@1-2]
265 ahm16: R6$@0-2
266 R6$: S[R0:2] ::= A A .
267 [p=R6:1@0-1; c=R9$@1-2]
268 ahm13: R5$@0-2
269 R5$: S[R0:1] ::= A[] S[R0:2] .
270 [p=R5:1@0-0; c=R6$@0-2]
271 ahm6: R2$@0-2
272 R2$: S ::= A[] S[R0:1] .
273 [p=R2:1@0-0; c=R3$@0-2] [p=R2:1@0-0; c=R5$@0-2]
274 ahm24: R10$@0-2
275 R10$: S['] ::= S .
276 [p=R10:0@0-0; c=R0$@0-2] [p=R10:0@0-0; c=R2$@0-2]
277 ahm13: R5$@1-2
278 R5$: S[R0:1] ::= A[] S[R0:2] .
279 [p=R5:1@1-1; c=R7$@1-2] [p=R5:1@1-1; c=R8$@1-2]
280 ahm9: R3$@0-2
281 R3$: S[R0:1] ::= A S[R0:2] .
282 [p=R3:1@0-1; c=R7$@1-2] [p=R3:1@0-1; c=R8$@1-2]
283 ahm2: R0$@0-2
284 R0$: S ::= A S[R0:1] .
285 [p=R0:1@0-1; c=R4$@1-2] [p=R0:1@0-1; c=R5$@1-2]
286 ahm14: R6:0@2-2
287 R6:0: S[R0:2] ::= . A A
288 ahm17: R7:0@2-2
289 R7:0: S[R0:2] ::= . A A[]
290 ahm19: R8:1@2-2
291 R8:1: S[R0:2] ::= A[] . A
292 ahm21: R9:0@2-2
293 R9:0: A ::= . a
294 Earley Set 3
295 ahm22: R9$@2-3
296 R9$: A ::= a .
297 [c=R9:0@2-2; s=a; t=\'a']
298 ahm20: R8$@2-3
299 R8$: S[R0:2] ::= A[] A .
300 [p=R8:1@2-2; c=R9$@2-3]
301 ahm18: R7$@2-3
302 R7$: S[R0:2] ::= A A[] .
303 [p=R7:0@2-2; c=R9$@2-3]
304 ahm15: R6:1@2-3
305 R6:1: S[R0:2] ::= A . A
306 [p=R6:0@2-2; c=R9$@2-3]
307 ahm16: R6$@1-3
308 R6$: S[R0:2] ::= A A .
309 [p=R6:1@1-2; c=R9$@2-3]
310 ahm13: R5$@1-3
311 R5$: S[R0:1] ::= A[] S[R0:2] .
312 [p=R5:1@1-1; c=R6$@1-3]
313 ahm9: R3$@0-3
314 R3$: S[R0:1] ::= A S[R0:2] .
315 [p=R3:1@0-1; c=R6$@1-3]
316 ahm6: R2$@0-3
317 R2$: S ::= A[] S[R0:1] .
318 [p=R2:1@0-0; c=R3$@0-3]
319 ahm24: R10$@0-3
320 R10$: S['] ::= S .
321 [p=R10:0@0-0; c=R0$@0-3] [p=R10:0@0-0; c=R2$@0-3]
322 ahm2: R0$@0-3
323 R0$: S ::= A S[R0:1] .
324 [p=R0:1@0-1; c=R3$@1-3] [p=R0:1@0-1; c=R5$@1-3]
325 ahm9: R3$@1-3
326 R3$: S[R0:1] ::= A S[R0:2] .
327 [p=R3:1@1-2; c=R7$@2-3] [p=R3:1@1-2; c=R8$@2-3]
328 ahm21: R9:0@3-3
329 R9:0: A ::= . a
330 Earley Set 4
331 ahm22: R9$@3-4
332 R9$: A ::= a .
333 [c=R9:0@3-3; s=a; t=\'a']
334 ahm16: R6$@2-4
335 R6$: S[R0:2] ::= A A .
336 [p=R6:1@2-3; c=R9$@3-4]
337 ahm9: R3$@1-4
338 R3$: S[R0:1] ::= A S[R0:2] .
339 [p=R3:1@1-2; c=R6$@2-4]
340 ahm2: R0$@0-4
341 R0$: S ::= A S[R0:1] .
342 [p=R0:1@0-1; c=R3$@1-4]
343 ahm24: R10$@0-4
344 R10$: S['] ::= S .
345 [p=R10:0@0-0; c=R0$@0-4]
346 END_OF_SETS
347
348 my $input_length = 4;
349 for (my $i = 0; $i < $input_length; $i++) {
350 $recce->read( 'a', 'a' );
351 }
352
353 Marpa::R2::Test::is(
354 $recce->show_earley_sets(2),
355 $expected_earley_sets,
356 'Aycock/Horspool Earley sets'
357 );
358
359 my @expected = map {
360 +{ map { ( $_ => 1 ) } @{$_} }
361 }
362 [q{}],
363 [qw( (a;;;) (;a;;) (;;a;) (;;;a) )],
364 [qw( (a;a;;) (a;;a;) (a;;;a) (;a;a;) (;a;;a) (;;a;a) )],
365 [qw( (a;a;a;) (a;a;;a) (a;;a;a) (;a;a;a) )],
366 ['(a;a;a;a)'];
367
368 $recce->set( { max_parses => 20 } );
369
370 for my $i ( 0 .. $input_length ) {
371
372 $recce->reset_evaluation();
373 $recce->set( { end => $i } );
374 my $expected = $expected[$i];
375
376 while ( my $value_ref = $recce->value() ) {
377
378 my $value = $value_ref ? ${$value_ref} : 'No parse';
379 if ( defined $expected->{$value} ) {
380 delete $expected->{$value};
381 Test::More::pass(qq{Expected result for length=$i, "$value"});
382 }
383 else {
384 Test::More::fail(qq{Unexpected result for length=$i, "$value"});
385 }
386 } ## end while ( my $value_ref = $recce->value() )
387
388 for my $value ( keys %{$expected} ) {
389 Test::More::fail(qq{Missing result for length=$i, "$value"});
390 }
391 } ## end for my $i ( 0 .. $input_length )
392
393 1; # In case used as "do" file
394
395 # Local Variables:
396 # mode: cperl
397 # cperl-indent-level: 4
398 # fill-column: 100
399 # End:
400 # vim: expandtab shiftwidth=4: