Debian Codegraph @

pod/Semantics/Null.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::Semantics::Null - How the SLIF evaluates null rules and symbols
19
20 =head1 Overview
21
22 In Marpa parses, rules and symbols can be nulled --
23 in other words they can derive the zero-length, or null, string.
24 Which symbols can be, or are, nulled, depends on the grammar
25 and the input.
26 When a symbol or rule is not nulled,
27 the symbol is said to be B<visible>.
28
29 Even the start symbol can be nulled,
30 in which case the entire parse derives the null string.
31 A parse in which the start symbol is nulled is
32 called a B<null parse>.
33
34 When evaluating a parse, nulled rules and symbols are
35 assigned values as described
36 L<in the semantics document|Marpa::R2::Semantics>.
37 This document provides additional detail on the assignment
38 of values to nulled symbols.
39
40 =head1 Description
41
42 =head2 Null values come from rules
43
44 Nulled subtrees are pruned back to their topmost symbol.
45 Lexemes are never nulled, so a nulled symbol is always the LHS of a rule instance,
46 and the action is determined from the rule alternative.
47
48 A complication arises if the symbol appears on the LHS of more than one
49 nullable rule alternative. Because the symbol is nulled, the input is no help in determining
50 which rule alternative to use. The rule alternative whose semantics are used for a nulled symbol
51 is determined as follows:
52
53 =over 4
54
55 =item * If all nullable rule alternatives have the same semantics, that semantics is used.
56
57 =item * If one of the nullable rule alternatives
58 is empty (that is, has a zero-length RHS),
59 then the empty alternative's semantics are used.
60
61 =item *
62 In the remaining case,
63 two or more of the rule alternatives have different action names,
64 but none of the alternatives has a zero-length RHS.
65 When this happens, Marpa throws an exception.
66 One easy way
67 to fix the issue,
68 is to add an empty rule with the intended semantics.
69
70 =back
71
72 In determining whether the semantics of two nullable rule alternatives
73 is "the same",
74 the blessing is taken into account.
75 Two rule alternatives are considered to have different semantics if
76 they are blessed differently.
77
78 The "lost" semantics of the non-topmost symbols and rules
79 of null subtrees are usually not missed.
80 Nulled subtrees cannot contain input,
81 and therefore do not contain token symbols.
82 So no token values are lost when
83 nulled subtrees are pruned.
84 As bushy as a null subtree might be,
85 all of its symbols and rules are nulled.
86
87 Since nulled symbols and rules correspond to zero-length strings,
88 so we are literally dealing here with
89 the "semantics of nothing".
90 In theory the semantics of nothing can be arbitrarily complex.
91 In practice it should be possible to keep them simple.
92
93 =head1 Example
94
95 As already stated,
96 Marpa prunes every null subtree back to its topmost
97 null symbol.
98 Here is an example:
99
100 =for Marpa::R2::Display
101 name: SLIF null value example
102 normalize-whitespace: 1
103
104 sub do_L {
105 shift;
106 return 'L(' . ( join q{;}, map { $_ // '[ERROR!]' } @_ ) . ')';
107 }
108
109 sub do_R {
110 return 'R(): I will never be called';
111 }
112
113 sub do_S {
114 shift;
115 return 'S(' . ( join q{;}, map { $_ // '[ERROR!]' } @_ ) . ')';
116 }
117
118 sub do_X { return 'X(' . $_[1] . ')'; }
119 sub do_Y { return 'Y(' . $_[1] . ')'; }
120
121 ## no critic (Variables::ProhibitPackageVars)
122 our $null_A = 'null A';
123 our $null_B = 'null B';
124 our $null_L = 'null L';
125 our $null_R = 'null R';
126 our $null_X = 'null X';
127 our $null_Y = 'null Y';
128 ## use critic
129
130 my $slg = Marpa::R2::Scanless::G->new(
131 { source => \<<'END_OF_DSL',
132 :start ::= S
133 S ::= L R action => do_S
134 L ::= A B X action => do_L
135 L ::= action => null_L
136 R ::= A B Y action => do_R
137 R ::= action => null_R
138 A ::= action => null_A
139 B ::= action => null_B
140 X ::= action => null_X
141 X ::= 'x' action => do_X
142 Y ::= action => null_Y
143 Y ::= 'y' action => do_Y
144 END_OF_DSL
145 }
146 );
147
148 my $slr = Marpa::R2::Scanless::R->new(
149 { grammar => $slg,
150 semantics_package => 'main',
151 }
152 );
153
154 $slr->read( \'x' );
155
156 =for Marpa::R2::Display::End
157
158 If we write the unpruned parse tree
159 in pre-order, depth-first, indenting children
160 below their parents, we get something like this:
161
162 =for Marpa::R2::Display
163 ignore: 1
164
165 0: Visible Rule: S := L R
166 1: Visible Rule L := A B X
167 1.1: Nulled Symbol A
168 1.2: Nulled Symbol B
169 1.3: Token, Value is 'x'
170 2: Nulled Rule, Rule R := A B Y
171 2.1: Nulled Symbol A
172 2.2: Nulled Symbol B
173 2.3: Nulled Symbol Y
174
175 =for Marpa::R2::Display::End
176
177 In this example, five symbols and a rule are nulled.
178 The rule and three of the symbols are in a single subtree: 2, 2.1, 2.2 and 2.3.
179 Marpa prunes every null subtree back to its topmost symbol,
180 which in this case is the LHS of the rule numbered 2.
181
182 The pruned tree looks like this
183
184 =for Marpa::R2::Display
185 ignore: 1
186
187 0: Visible Rule: S := L R
188 1: Visible Rule L := A B X
189 1.1: Nulled Symbol A
190 1.2: Nulled Symbol B
191 1.3: Token, Value is 'x'
192 2: LHS of Nulled Rule, Symbol R
193
194 =for Marpa::R2::Display::End
195
196 Nulled nodes 1.1, 1.2 and 2 were all kept, because they are topmost in their
197 nulled subtree.
198 All the other nulled nodes were discarded.
199
200 Here is the output:
201
202 =for Marpa::R2::Display
203 name: SLIF null value example output
204 normalize-whitespace: 1
205
206 S(L(null A;null B;X(x));null R)
207
208 =for Marpa::R2::Display::End
209
210 In the output we see
211
212 =over
213
214 =item * The null value for symbol 1.1: "C<null A>".
215 This comes from the empty rule for C<A>.
216
217 =item * The null value for symbol 1.2: "C<null B>".
218 This comes from the empty rule for C<B>.
219
220 =item * The token value for symbol 1.3: "C<x>".
221
222 =item * An application of the rule evaluation closure for the rule
223 C<L := A B X>.
224
225 =item * The null value for rule 2: "C<null R>".
226 This comes from the empty rule for C<R>.
227
228 =item * An application of the rule evaluation closure for the rule
229 C<S := L R>
230
231 =back
232
233 We B<do not> see any output
234 for symbols
235 2.1 (C<A>),
236 2.2 (C<B>),
237 or 2.3 (C<Y>)
238 because they were not topmost
239 in the pruned subtree.
240 We B<do not> see an application of the rule evaluation closure for rule C<R := A B Y>,
241 because there is an empty rule for C<R>, and that takes priority.
242
243 =head1 Copyright and License
244
245 =for Marpa::R2::Display
246 ignore: 1
247
248 Copyright 2022 Jeffrey Kegler
249 This file is part of Marpa::R2. Marpa::R2 is free software: you can
250 redistribute it and/or modify it under the terms of the GNU Lesser
251 General Public License as published by the Free Software Foundation,
252 either version 3 of the License, or (at your option) any later version.
253
254 Marpa::R2 is distributed in the hope that it will be useful,
255 but WITHOUT ANY WARRANTY; without even the implied warranty of
256 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
257 Lesser General Public License for more details.
258
259 You should have received a copy of the GNU Lesser
260 General Public License along with Marpa::R2. If not, see
261 http://www.gnu.org/licenses/.
262
263 =for Marpa::R2::Display::End
264
265 =cut
266
267 # Local Variables:
268 # mode: cperl
269 # cperl-indent-level: 4
270 # fill-column: 100
271 # End:
272 # vim: expandtab shiftwidth=4: