Debian Codegraph @

t/equation.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 # An ambiguous equation
17
18 use 5.010001;
19 use strict;
20 use warnings;
21
22 use Test::More tests => 11;
23
24 use lib 'inc';
25 use Marpa::R2::Test;
26 use English qw( -no_match_vars );
27 use Fatal qw( close open );
28 use Marpa::R2;
29
30 ## no critic (InputOutput::RequireBriefOpen)
31 open my $original_stdout, q{>&STDOUT};
32 ## use critic
33
34 sub save_stdout {
35 my $save;
36 my $save_ref = \$save;
37 close STDOUT;
38 open STDOUT, q{>}, $save_ref;
39 return $save_ref;
40 } ## end sub save_stdout
41
42 sub restore_stdout {
43 close STDOUT;
44 open STDOUT, q{>&}, $original_stdout;
45 return 1;
46 }
47
48 ## no critic (Subroutines::RequireArgUnpacking, ErrorHandling::RequireCarping)
49
50 sub do_op {
51 shift;
52 my ( $right_string, $right_value ) = ( $_[2] =~ /^(.*)==(.*)$/xms );
53 my ( $left_string, $left_value ) = ( $_[0] =~ /^(.*)==(.*)$/xms );
54 my $op = $_[1];
55 my $value;
56 if ( $op eq q{+} ) {
57 $value = $left_value + $right_value;
58 }
59 elsif ( $op eq q{*} ) {
60 $value = $left_value * $right_value;
61 }
62 elsif ( $op eq q{-} ) {
63 $value = $left_value - $right_value;
64 }
65 else {
66 die "Unknown op: $op";
67 }
68 return '(' . $left_string . $op . $right_string . ')==' . $value;
69 } ## end sub do_op
70
71 sub number {
72 shift;
73 my $v0 = pop @_;
74 return $v0 . q{==} . $v0;
75 }
76
77 sub default_action {
78 shift;
79 my $v_count = scalar @_;
80 return q{} if $v_count <= 0;
81 return $_[0] if $v_count == 1;
82 return '(' . join( q{;}, @_ ) . ')';
83 } ## end sub default_action
84
85 my $grammar = Marpa::R2::Grammar->new(
86 { start => 'E',
87 actions => 'main',
88 rules => [
89 [ 'E', [qw/E Op E/], 'do_op' ],
90 [ 'E', [qw/Number/], 'number' ],
91 ],
92 default_action => 'default_action',
93 }
94 );
95
96 $grammar->precompute();
97
98 my $actual_ref;
99 $actual_ref = save_stdout();
100
101 # Marpa::R2::Display
102 # name: show_symbols Synopsis
103
104 print $grammar->show_symbols()
105 or die "print failed: $ERRNO";
106
107 # Marpa::R2::Display::End
108
109 restore_stdout();
110
111 Marpa::R2::Test::is( ${$actual_ref},
112 <<'END_SYMBOLS', 'Ambiguous Equation Symbols' );
113 0: E
114 1: Op, terminal
115 2: Number, terminal
116 END_SYMBOLS
117
118 $actual_ref = save_stdout();
119
120 # Marpa::R2::Display
121 # name: show_rules Synopsis
122
123 print $grammar->show_rules()
124 or die "print failed: $ERRNO";
125
126 # Marpa::R2::Display::End
127
128 restore_stdout();
129
130 Marpa::R2::Test::is( ${$actual_ref},
131 <<'END_RULES', 'Ambiguous Equation Rules' );
132 0: E -> E Op E
133 1: E -> Number
134 END_RULES
135
136 $actual_ref = save_stdout();
137
138 print $grammar->show_ahms()
139 or die "print failed: $ERRNO";
140
141 restore_stdout();
142
143 Marpa::R2::Test::is( ${$actual_ref},
144 <<'EOS', 'Ambiguous Equation AHMs' );
145 AHM 0: postdot = "E"
146 E ::= . E Op E
147 AHM 1: postdot = "Op"
148 E ::= E . Op E
149 AHM 2: postdot = "E"
150 E ::= E Op . E
151 AHM 3: completion
152 E ::= E Op E .
153 AHM 4: postdot = "Number"
154 E ::= . Number
155 AHM 5: completion
156 E ::= Number .
157 AHM 6: postdot = "E"
158 E['] ::= . E
159 AHM 7: completion
160 E['] ::= E .
161 EOS
162
163 $actual_ref = save_stdout();
164
165 # Marpa::R2::Display
166 # name: show_problems Synopsis
167
168 print $grammar->show_problems()
169 or die "print failed: $ERRNO";
170
171 # Marpa::R2::Display::End
172
173 Marpa::R2::Test::is(
174 ${$actual_ref},
175 "Grammar has no problems\n",
176 'Ambiguous Equation Problems'
177 );
178
179 restore_stdout();
180
181 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
182
183 $recce->read( 'Number', 2 );
184 $recce->read( 'Op', q{-} );
185 $recce->read( 'Number', 0 );
186 $recce->read( 'Op', q{*} );
187 $recce->read( 'Number', 3 );
188 $recce->read( 'Op', q{+} );
189 $recce->read( 'Number', 1 );
190
191 $actual_ref = save_stdout();
192
193 print $recce->show_earley_sets()
194 or die "print failed: $ERRNO";
195
196 my $expected_earley_sets = <<'END_OF_EARLEY_SETS';
197 Last Completed: 7; Furthest: 7
198 Earley Set 0
199 ahm6: R2:0@0-0
200 R2:0: E['] ::= . E
201 ahm0: R0:0@0-0
202 R0:0: E ::= . E Op E
203 ahm4: R1:0@0-0
204 R1:0: E ::= . Number
205 Earley Set 1
206 ahm5: R1$@0-1
207 R1$: E ::= Number .
208 [c=R1:0@0-0; s=Number; t=\2]
209 ahm1: R0:1@0-1
210 R0:1: E ::= E . Op E
211 [p=R0:0@0-0; c=R1$@0-1]
212 ahm7: R2$@0-1
213 R2$: E['] ::= E .
214 [p=R2:0@0-0; c=R1$@0-1]
215 Earley Set 2
216 ahm2: R0:2@0-2
217 R0:2: E ::= E Op . E
218 [c=R0:1@0-1; s=Op; t=\'-']
219 ahm0: R0:0@2-2
220 R0:0: E ::= . E Op E
221 ahm4: R1:0@2-2
222 R1:0: E ::= . Number
223 Earley Set 3
224 ahm5: R1$@2-3
225 R1$: E ::= Number .
226 [c=R1:0@2-2; s=Number; t=\0]
227 ahm1: R0:1@2-3
228 R0:1: E ::= E . Op E
229 [p=R0:0@2-2; c=R1$@2-3]
230 ahm3: R0$@0-3
231 R0$: E ::= E Op E .
232 [p=R0:2@0-2; c=R1$@2-3]
233 ahm1: R0:1@0-3
234 R0:1: E ::= E . Op E
235 [p=R0:0@0-0; c=R0$@0-3]
236 ahm7: R2$@0-3
237 R2$: E['] ::= E .
238 [p=R2:0@0-0; c=R0$@0-3]
239 Earley Set 4
240 ahm2: R0:2@0-4
241 R0:2: E ::= E Op . E
242 [c=R0:1@0-3; s=Op; t=\'*']
243 ahm2: R0:2@2-4
244 R0:2: E ::= E Op . E
245 [c=R0:1@2-3; s=Op; t=\'*']
246 ahm0: R0:0@4-4
247 R0:0: E ::= . E Op E
248 ahm4: R1:0@4-4
249 R1:0: E ::= . Number
250 Earley Set 5
251 ahm5: R1$@4-5
252 R1$: E ::= Number .
253 [c=R1:0@4-4; s=Number; t=\3]
254 ahm1: R0:1@4-5
255 R0:1: E ::= E . Op E
256 [p=R0:0@4-4; c=R1$@4-5]
257 ahm3: R0$@2-5
258 R0$: E ::= E Op E .
259 [p=R0:2@2-4; c=R1$@4-5]
260 ahm3: R0$@0-5
261 R0$: E ::= E Op E .
262 [p=R0:2@0-2; c=R0$@2-5] [p=R0:2@0-4; c=R1$@4-5]
263 ahm1: R0:1@0-5
264 R0:1: E ::= E . Op E
265 [p=R0:0@0-0; c=R0$@0-5]
266 ahm7: R2$@0-5
267 R2$: E['] ::= E .
268 [p=R2:0@0-0; c=R0$@0-5]
269 ahm1: R0:1@2-5
270 R0:1: E ::= E . Op E
271 [p=R0:0@2-2; c=R0$@2-5]
272 Earley Set 6
273 ahm2: R0:2@2-6
274 R0:2: E ::= E Op . E
275 [c=R0:1@2-5; s=Op; t=\'+']
276 ahm2: R0:2@0-6
277 R0:2: E ::= E Op . E
278 [c=R0:1@0-5; s=Op; t=\'+']
279 ahm2: R0:2@4-6
280 R0:2: E ::= E Op . E
281 [c=R0:1@4-5; s=Op; t=\'+']
282 ahm0: R0:0@6-6
283 R0:0: E ::= . E Op E
284 ahm4: R1:0@6-6
285 R1:0: E ::= . Number
286 Earley Set 7
287 ahm5: R1$@6-7
288 R1$: E ::= Number .
289 [c=R1:0@6-6; s=Number; t=\1]
290 ahm1: R0:1@6-7
291 R0:1: E ::= E . Op E
292 [p=R0:0@6-6; c=R1$@6-7]
293 ahm3: R0$@4-7
294 R0$: E ::= E Op E .
295 [p=R0:2@4-6; c=R1$@6-7]
296 ahm3: R0$@0-7
297 R0$: E ::= E Op E .
298 [p=R0:2@0-2; c=R0$@2-7] [p=R0:2@0-4; c=R0$@4-7] [p=R0:2@0-6; c=R1$@6-7]
299 ahm3: R0$@2-7
300 R0$: E ::= E Op E .
301 [p=R0:2@2-4; c=R0$@4-7] [p=R0:2@2-6; c=R1$@6-7]
302 ahm1: R0:1@2-7
303 R0:1: E ::= E . Op E
304 [p=R0:0@2-2; c=R0$@2-7]
305 ahm1: R0:1@0-7
306 R0:1: E ::= E . Op E
307 [p=R0:0@0-0; c=R0$@0-7]
308 ahm7: R2$@0-7
309 R2$: E['] ::= E .
310 [p=R2:0@0-0; c=R0$@0-7]
311 ahm1: R0:1@4-7
312 R0:1: E ::= E . Op E
313 [p=R0:0@4-4; c=R0$@4-7]
314 END_OF_EARLEY_SETS
315
316 Marpa::R2::Test::is( ${$actual_ref}, $expected_earley_sets,
317 'Ambiguous Equation Earley Sets' );
318
319 restore_stdout();
320
321 $actual_ref = save_stdout();
322
323 # Marpa::R2::Display
324 # name: show_progress Synopsis
325
326 print $recce->show_progress()
327 or die "print failed: $ERRNO";
328
329 # Marpa::R2::Display::End
330
331 Marpa::R2::Test::is( ${$actual_ref},
332 <<'END_OF_PROGRESS_REPORT', 'Ambiguous Equation Progress Report' );
333 R0:1 x4 @0...6-7 E -> E . Op E
334 F0 x3 @0,2,4-7 E -> E Op E .
335 F1 @6-7 E -> Number .
336 END_OF_PROGRESS_REPORT
337
338 restore_stdout();
339
340 my %expected_value = (
341 '(2-(0*(3+1)))==2' => 1,
342 '(((2-0)*3)+1)==7' => 1,
343 '((2-(0*3))+1)==3' => 1,
344 '((2-0)*(3+1))==8' => 1,
345 '(2-((0*3)+1))==1' => 1,
346 );
347
348 # Set max at 10 just in case there's an infinite loop.
349 # This is for debugging, after all
350
351 # Marpa::R2::Display
352 # name: Recognizer set Synopsis
353
354 $recce->set( { max_parses => 10, } );
355
356 # Marpa::R2::Display::End
357
358 my $i = 0;
359 while ( defined( my $value = $recce->value() ) ) {
360 my $value = ${$value};
361 if ( defined $expected_value{$value} ) {
362 delete $expected_value{$value};
363 Test::More::pass("Expected Value $i: $value");
364 }
365 else {
366 Test::More::fail("Unexpected Value $i: $value");
367 }
368 $i++;
369 } ## end while ( defined( my $value = $recce->value() ) )
370
371 1; # In case used as "do" file
372
373 # Local Variables:
374 # mode: cperl
375 # cperl-indent-level: 4
376 # fill-column: 100
377 # End:
378 # vim: expandtab shiftwidth=4: