Debian Codegraph @

t/implementation.t

raw
1 #!/usr/bin/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 use 5.010001;
18 use strict;
19 use warnings;
20
21 use Fatal qw(open close);
22 use Test::More tests => 8;
23
24 use lib 'inc';
25 use Marpa::R2::Test;
26 use Marpa::R2;
27
28 my $grammar = Marpa::R2::Grammar->new(
29 { start => 'Expression',
30 actions => 'My_Actions',
31 default_action => 'first_arg',
32 rules => [
33 { lhs => 'Expression', rhs => [qw/Term/] },
34 { lhs => 'Term', rhs => [qw/Factor/] },
35 { lhs => 'Factor', rhs => [qw/Number/] },
36 { lhs => 'Term', rhs => [qw/Term Add Term/], action => 'do_add' },
37 { lhs => 'Factor',
38 rhs => [qw/Factor Multiply Factor/],
39 action => 'do_multiply'
40 },
41 ],
42 }
43 );
44
45 $grammar->precompute();
46
47 my $recce = Marpa::R2::Recognizer->new( { grammar => $grammar } );
48
49 my @tokens = (
50 [ 'Number', 42 ],
51 [ 'Multiply', q{*} ],
52 [ 'Number', 1 ],
53 [ 'Add', q{+} ],
54 [ 'Number', 7 ],
55 );
56
57 for my $token_and_value (@tokens) {
58 $recce->read( @{$token_and_value} );
59 }
60
61 sub My_Actions::do_add {
62 my ( undef, $t1, undef, $t2 ) = @_;
63 return $t1 + $t2;
64 }
65
66 sub My_Actions::do_multiply {
67 my ( undef, $t1, undef, $t2 ) = @_;
68 return $t1 * $t2;
69 }
70
71 sub My_Actions::first_arg { shift; return shift; }
72
73 my $value_ref = $recce->value();
74 my $value = $value_ref ? ${$value_ref} : 'No Parse';
75
76 Marpa::R2::Test::is( 49, $value, 'Implementation Example Value 1' );
77
78 $recce->reset_evaluation();
79
80 my $show_symbols_output = $grammar->show_symbols();
81
82 Marpa::R2::Test::is( $show_symbols_output,
83 <<'END_SYMBOLS', 'Implementation Example Symbols' );
84 0: Expression
85 1: Term
86 2: Factor
87 3: Number, terminal
88 4: Add, terminal
89 5: Multiply, terminal
90 END_SYMBOLS
91
92 my $show_rules_output = $grammar->show_rules();
93
94 Marpa::R2::Test::is( $show_rules_output,
95 <<'END_RULES', 'Implementation Example Rules' );
96 0: Expression -> Term
97 1: Term -> Factor
98 2: Factor -> Number
99 3: Term -> Term Add Term
100 4: Factor -> Factor Multiply Factor
101 END_RULES
102
103 my $show_ahms_output = $grammar->show_ahms();
104
105 Marpa::R2::Test::is( $show_ahms_output,
106 <<'END_AHM', 'Implementation Example AHMs' );
107 AHM 0: postdot = "Term"
108 Expression ::= . Term
109 AHM 1: completion
110 Expression ::= Term .
111 AHM 2: postdot = "Factor"
112 Term ::= . Factor
113 AHM 3: completion
114 Term ::= Factor .
115 AHM 4: postdot = "Number"
116 Factor ::= . Number
117 AHM 5: completion
118 Factor ::= Number .
119 AHM 6: postdot = "Term"
120 Term ::= . Term Add Term
121 AHM 7: postdot = "Add"
122 Term ::= Term . Add Term
123 AHM 8: postdot = "Term"
124 Term ::= Term Add . Term
125 AHM 9: completion
126 Term ::= Term Add Term .
127 AHM 10: postdot = "Factor"
128 Factor ::= . Factor Multiply Factor
129 AHM 11: postdot = "Multiply"
130 Factor ::= Factor . Multiply Factor
131 AHM 12: postdot = "Factor"
132 Factor ::= Factor Multiply . Factor
133 AHM 13: completion
134 Factor ::= Factor Multiply Factor .
135 AHM 14: postdot = "Expression"
136 Expression['] ::= . Expression
137 AHM 15: completion
138 Expression['] ::= Expression .
139 END_AHM
140
141 my $show_earley_sets_output = $recce->show_earley_sets();
142
143 my $expected_earley_sets = <<'END_EARLEY_SETS';
144 Last Completed: 5; Furthest: 5
145 Earley Set 0
146 ahm14: R5:0@0-0
147 R5:0: Expression['] ::= . Expression
148 ahm0: R0:0@0-0
149 R0:0: Expression ::= . Term
150 ahm2: R1:0@0-0
151 R1:0: Term ::= . Factor
152 ahm6: R3:0@0-0
153 R3:0: Term ::= . Term Add Term
154 ahm4: R2:0@0-0
155 R2:0: Factor ::= . Number
156 ahm10: R4:0@0-0
157 R4:0: Factor ::= . Factor Multiply Factor
158 Earley Set 1
159 ahm5: R2$@0-1
160 R2$: Factor ::= Number .
161 [c=R2:0@0-0; s=Number; t=\42]
162 ahm11: R4:1@0-1
163 R4:1: Factor ::= Factor . Multiply Factor
164 [p=R4:0@0-0; c=R2$@0-1]
165 ahm3: R1$@0-1
166 R1$: Term ::= Factor .
167 [p=R1:0@0-0; c=R2$@0-1]
168 ahm7: R3:1@0-1
169 R3:1: Term ::= Term . Add Term
170 [p=R3:0@0-0; c=R1$@0-1]
171 ahm1: R0$@0-1
172 R0$: Expression ::= Term .
173 [p=R0:0@0-0; c=R1$@0-1]
174 ahm15: R5$@0-1
175 R5$: Expression['] ::= Expression .
176 [p=R5:0@0-0; c=R0$@0-1]
177 Earley Set 2
178 ahm12: R4:2@0-2
179 R4:2: Factor ::= Factor Multiply . Factor
180 [c=R4:1@0-1; s=Multiply; t=\'*']
181 ahm4: R2:0@2-2
182 R2:0: Factor ::= . Number
183 ahm10: R4:0@2-2
184 R4:0: Factor ::= . Factor Multiply Factor
185 Earley Set 3
186 ahm5: R2$@2-3
187 R2$: Factor ::= Number .
188 [c=R2:0@2-2; s=Number; t=\1]
189 ahm11: R4:1@2-3
190 R4:1: Factor ::= Factor . Multiply Factor
191 [p=R4:0@2-2; c=R2$@2-3]
192 ahm13: R4$@0-3
193 R4$: Factor ::= Factor Multiply Factor .
194 [p=R4:2@0-2; c=R2$@2-3]
195 ahm11: R4:1@0-3
196 R4:1: Factor ::= Factor . Multiply Factor
197 [p=R4:0@0-0; c=R4$@0-3]
198 ahm3: R1$@0-3
199 R1$: Term ::= Factor .
200 [p=R1:0@0-0; c=R4$@0-3]
201 ahm7: R3:1@0-3
202 R3:1: Term ::= Term . Add Term
203 [p=R3:0@0-0; c=R1$@0-3]
204 ahm1: R0$@0-3
205 R0$: Expression ::= Term .
206 [p=R0:0@0-0; c=R1$@0-3]
207 ahm15: R5$@0-3
208 R5$: Expression['] ::= Expression .
209 [p=R5:0@0-0; c=R0$@0-3]
210 Earley Set 4
211 ahm8: R3:2@0-4
212 R3:2: Term ::= Term Add . Term
213 [c=R3:1@0-3; s=Add; t=\'+']
214 ahm2: R1:0@4-4
215 R1:0: Term ::= . Factor
216 ahm4: R2:0@4-4
217 R2:0: Factor ::= . Number
218 ahm6: R3:0@4-4
219 R3:0: Term ::= . Term Add Term
220 ahm10: R4:0@4-4
221 R4:0: Factor ::= . Factor Multiply Factor
222 Earley Set 5
223 ahm5: R2$@4-5
224 R2$: Factor ::= Number .
225 [c=R2:0@4-4; s=Number; t=\7]
226 ahm11: R4:1@4-5
227 R4:1: Factor ::= Factor . Multiply Factor
228 [p=R4:0@4-4; c=R2$@4-5]
229 ahm3: R1$@4-5
230 R1$: Term ::= Factor .
231 [p=R1:0@4-4; c=R2$@4-5]
232 ahm7: R3:1@4-5
233 R3:1: Term ::= Term . Add Term
234 [p=R3:0@4-4; c=R1$@4-5]
235 ahm9: R3$@0-5
236 R3$: Term ::= Term Add Term .
237 [p=R3:2@0-4; c=R1$@4-5]
238 ahm7: R3:1@0-5
239 R3:1: Term ::= Term . Add Term
240 [p=R3:0@0-0; c=R3$@0-5]
241 ahm1: R0$@0-5
242 R0$: Expression ::= Term .
243 [p=R0:0@0-0; c=R3$@0-5]
244 ahm15: R5$@0-5
245 R5$: Expression['] ::= Expression .
246 [p=R5:0@0-0; c=R0$@0-5]
247 END_EARLEY_SETS
248
249 Marpa::R2::Test::is( $show_earley_sets_output, $expected_earley_sets,
250 'Implementation Example Earley Sets' );
251
252 my $trace_output;
253 open my $trace_fh, q{>}, \$trace_output;
254 $recce->set( { trace_fh => $trace_fh, trace_values => 2 } );
255 $value_ref = $recce->value();
256 $recce->set( { trace_fh => \*STDOUT, trace_values => 0 } );
257 close $trace_fh;
258
259 $value = $value_ref ? ${$value_ref} : 'No Parse';
260 Marpa::R2::Test::is( 49, $value, 'Implementation Example Value 2' );
261
262 my $expected_trace_output = <<'END_TRACE_OUTPUT';
263 Setting trace_values option
264 Pushed value from R2:1@0-1S3@0: Number = \42
265 Popping 1 values to evaluate R2:1@0-1S3@0, rule: 2: Factor -> Number
266 Calculated and pushed value: 42
267 Pushed value from R4:2@0-2S5@1: Multiply = \'*'
268 Pushed value from R2:1@2-3S3@2: Number = \1
269 Popping 1 values to evaluate R2:1@2-3S3@2, rule: 2: Factor -> Number
270 Calculated and pushed value: 1
271 Popping 3 values to evaluate R4:3@0-3C2@2, rule: 4: Factor -> Factor Multiply Factor
272 Calculated and pushed value: 42
273 Popping 1 values to evaluate R1:1@0-3C4@0, rule: 1: Term -> Factor
274 Calculated and pushed value: 42
275 Pushed value from R3:2@0-4S4@3: Add = \'+'
276 Pushed value from R2:1@4-5S3@4: Number = \7
277 Popping 1 values to evaluate R2:1@4-5S3@4, rule: 2: Factor -> Number
278 Calculated and pushed value: 7
279 Popping 1 values to evaluate R1:1@4-5C2@4, rule: 1: Term -> Factor
280 Calculated and pushed value: 7
281 Popping 3 values to evaluate R3:3@0-5C1@4, rule: 3: Term -> Term Add Term
282 Calculated and pushed value: 49
283 Popping 1 values to evaluate R0:1@0-5C3@0, rule: 0: Expression -> Term
284 Calculated and pushed value: 49
285 New Virtual Rule: R5:1@0-5C0@0, rule: 5: Expression['] -> Expression
286 Real symbol count is 1
287 END_TRACE_OUTPUT
288
289 Marpa::R2::Test::is( $trace_output, $expected_trace_output,
290 'Implementation Example Trace Output' );
291
292 $recce->reset_evaluation();
293
294 $value_ref = $recce->value();
295 $value = $value_ref ? ${$value_ref} : 'No Parse';
296 Marpa::R2::Test::is( 49, $value, 'Implementation Example Value 3' );
297
298 1; # In case used as "do" file
299
300 # Local Variables:
301 # mode: cperl
302 # cperl-indent-level: 4
303 # fill-column: 100
304 # End:
305 # vim: expandtab shiftwidth=4: