Debian Codegraph @

pod/Tutorial2.pod

raw
1 # Copyright 2022 Jeffrey Kegler
2 # This file is part of Marpa::R2. Marpa::R2 is free software: you can
3 # redistribute it and/or modify it under the terms of the GNU Lesser
4 # General Public License as published by the Free Software Foundation,
5 # either version 3 of the License, or (at your option) any later version.
6 #
7 # Marpa::R2 is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser
13 # General Public License along with Marpa::R2. If not, see
14 # http://www.gnu.org/licenses/.
15
16 =head1 NAME
17
18 Marpa::R2::Tutorial2 - Marpa Tutorial 2
19
20 =head1 Synopsis
21
22 =for Marpa::R2::Display
23 name: Tutorial 2 synopsis
24 normalize-whitespace: 1
25
26 use Marpa::R2;
27
28 my $dsl = <<'END_OF_DSL';
29 :default ::= action => [name,values]
30 lexeme default = latm => 1
31 Calculator ::= Expression action => ::first
32
33 Factor ::= Number action => ::first
34 Term ::=
35 Term '*' Factor action => do_multiply
36 | Factor action => ::first
37 Expression ::=
38 Expression '+' Term action => do_add
39 | Term action => ::first
40 Number ~ digits
41 digits ~ [\d]+
42 :discard ~ whitespace
43 whitespace ~ [\s]+
44 END_OF_DSL
45
46 my $grammar = Marpa::R2::Scanless::G->new( { source => \$dsl } );
47 my $recce = Marpa::R2::Scanless::R->new(
48 { grammar => $grammar, semantics_package => 'My_Actions' } );
49 my $input = '42 * 1 + 7';
50 my $length_read = $recce->read( \$input );
51
52 die "Read ended after $length_read of ", length $input, " characters"
53 if $length_read != length $input;
54
55 if ( my $ambiguous_status = $recce->ambiguous() ) {
56 chomp $ambiguous_status;
57 die "Parse is ambiguous\n", $ambiguous_status;
58 }
59
60 my $value_ref = $recce->value;
61 my $value = ${$value_ref};
62
63 sub My_Actions::do_add {
64 my ( undef, $t1, undef, $t2 ) = @_;
65 return $t1 + $t2;
66 }
67
68 sub My_Actions::do_multiply {
69 my ( undef, $t1, undef, $t2 ) = @_;
70 return $t1 * $t2;
71 }
72
73 =for Marpa::R2::Display::End
74
75 =head1 Description
76
77 =head2 Overview
78
79 This document contains a second tutorial
80 of the Scanless interface (SLIF),
81 which demonstrates a lower level of method calls.
82 These lower level calls
83 allow access to more of Marpa's
84 features.
85 For example, users will need to
86 use the lower level calls
87
88 =over 4
89
90 =item *
91
92 to use SLIF parse events;
93
94 =item *
95
96 to examine the values of multiple parses of an ambiguous parse;
97
98 =item *
99
100 and to get finer control of the response to Marpa errors.
101
102 =back
103
104 This uses the same extremely simple calculator
105 as the tutorial in the L<landing page|Marpa::R2>.
106 Most of the code is exactly the same in fact,
107 and we will skip it.
108 Here is what is new:
109
110 =head2 Marpa::R2::Scanless::R::new
111
112 =for Marpa::R2::Display
113 name: Tutorial 2 synopsis
114 partial: 1
115 normalize-whitespace: 1
116
117 my $recce = Marpa::R2::Scanless::R->new(
118 { grammar => $grammar, semantics_package => 'My_Actions' } );
119
120 =for Marpa::R2::Display::End
121
122 C<Marpa::R2::Scanless::R::new> creates a new SLIF recognizer.
123 Its arguments are references to hashes of named arguments.
124 In this example the first named argument is
125 the required argument: "C<grammar>".
126 The value of the
127 C<grammar>
128 named argument must be a Marpa::R2 SLIF
129 grammar.
130
131 The second argument is optional, but you will use it frequently.
132 The "C<semantics_package>" named argument tells Marpa in which Perl package to
133 look for the closures implementing the semantics for this grammar.
134
135 =head2 Marpa::R2::Scanless::R::read
136
137 =for Marpa::R2::Display
138 name: Tutorial 2 synopsis
139 partial: 1
140 normalize-whitespace: 1
141
142 my $input = '42 * 1 + 7';
143 my $length_read = $recce->read( \$input );
144
145 =for Marpa::R2::Display::End
146
147 To parse a string,
148 we use
149 the C<Marpa::R2::Scanless::R::read()> method.
150 In its simplest form,
151 as here,
152 the C<Marpa::R2::Scanless::R::read()> method
153 takes a reference
154 to a string containing the input stream as its argument.
155
156 =head2 Checking for a premature end
157
158 =for Marpa::R2::Display
159 name: Tutorial 2 synopsis
160 partial: 1
161 normalize-whitespace: 1
162
163 die "Read ended after $length_read of ", length $input, " characters"
164 if $length_read != length $input;
165
166 =for Marpa::R2::Display::End
167
168 Most premature endings occur when a parse is exhausted.
169 A parse is "exhausted" when there is no possible way for it
170 to continue on to success.
171 Premature parse exhaustion is thrown as a failure by
172 the C<Marpa::R2::Scanless::R::read()> method,
173 and it is not necessary to check for it explicitly.
174
175 There are other premature endings
176 that are not necessarily failures,
177 and which therefore are not thrown.
178 These are Marpa's SLIF parse events.
179 SLIF parse events are an advanced feature, and are described
180 L<elsewhere|Marpa::R2::Event>.
181 In this example,
182 any premature ending caused by
183 the triggering of a SLIF parse event will be caused
184 by stray parse events --
185 unexpected parse events due to a mistake in writing the DSL.
186
187 Programming a stray parse event is a programming mistake that you
188 are not likely to make,
189 so arguably this check is not really necessary.
190 There are no SLIF parse events, stray or otherwise, in this example.
191 But this check is included for completeness,
192 and as an example of a cautious programming style.
193
194 =head2 Checking for an ambiguous parse
195
196 =for Marpa::R2::Display
197 name: Tutorial 2 synopsis
198 partial: 1
199 normalize-whitespace: 1
200
201 if ( my $ambiguous_status = $recce->ambiguous() ) {
202 chomp $ambiguous_status;
203 die "Parse is ambiguous\n", $ambiguous_status;
204 }
205
206 =for Marpa::R2::Display::End
207
208 Much more likely than stray SLIF parse events are ambiguous parses --
209 parses where the input can be parsed in two or more ways.
210 Ambiguous parses are not necessarily a problem -- some applications
211 may not care about them.
212 Other applications, as an advanced technique,
213 actually exploit ambiguity,
214
215 Beginners should regard an ambiguous parse
216 as a sign of trouble.
217 The existence of ambiguous parses should be tolerated
218 only if you understand the kind of ambiguity that exists
219 in your grammar,
220 and only if you know that ambiguities of that kind
221 will not cause trouble.
222 The above code uses
223 L<the recognizer's C<ambigous()>
224 method|Marpa::R2::Scanless::R/"ambiguous()"> to spot ambiguous parses.
225 The L<C<ambigous()>
226 method|Marpa::R2::Scanless::R/"ambiguous()"> returns the empty string
227 if there was exactly one parse.
228 If there was no parse,
229 the L<C<ambigous()>
230 method|Marpa::R2::Scanless::R/"ambiguous()"> returns a string stating that.
231 If there were two or more parses,
232 the L<C<ambigous()>
233 method|Marpa::R2::Scanless::R/"ambiguous()"> returns a string describing
234 the ambiguity.
235
236 =head2 Marpa::R2::Scanless::R::value
237
238 =for Marpa::R2::Display
239 name: Tutorial 2 synopsis
240 partial: 1
241 normalize-whitespace: 1
242
243 my $value_ref = $recce->value;
244 my $value = ${$value_ref};
245
246 =for Marpa::R2::Display::End
247
248 The C<Marpa::R2::Scanless::R::value()> method returns
249 a reference to the parse result's value,
250 if there was a parse result.
251 If there was no parse result,
252 C<Marpa::R2::Scanless::R::value()>
253 returns
254 C<undef>.
255 This code does not check for the case where there is
256 no parse result, because we already performed that
257 check along with the check for ambiguity.
258
259 The value of the parse is exactly the same,
260 and computed in exactly the same way,
261 as in the previous tutorial.
262
263 =head1 Copyright and License
264
265 =for Marpa::R2::Display
266 ignore: 1
267
268 Copyright 2022 Jeffrey Kegler
269 This file is part of Marpa::R2. Marpa::R2 is free software: you can
270 redistribute it and/or modify it under the terms of the GNU Lesser
271 General Public License as published by the Free Software Foundation,
272 either version 3 of the License, or (at your option) any later version.
273
274 Marpa::R2 is distributed in the hope that it will be useful,
275 but WITHOUT ANY WARRANTY; without even the implied warranty of
276 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
277 Lesser General Public License for more details.
278
279 You should have received a copy of the GNU Lesser
280 General Public License along with Marpa::R2. If not, see
281 http://www.gnu.org/licenses/.
282
283 =for Marpa::R2::Display::End
284
285 =cut
286
287 # Local Variables:
288 # mode: cperl
289 # cperl-indent-level: 4
290 # fill-column: 100
291 # End:
292 # vim: expandtab shiftwidth=4:
293