blib/lib/Parse/Yapp/Lalr.pm

filter: Statementstmt 76.1% 344 / 452 Branchbran 65.2% 107 / 164 Conditioncond 77.7% 42 / 54 MC/DCmcdc 66.6% 22 / 33 Subroutinesub 84.0% 21 / 25 Podpod 0.0% 0 / 6 totaltotal 73.0% 536 / 734 CC 134 SCAR 47.9
CC
134
Cov
74%
ShowDfa
650.0
Conflicts
110.0
Warnings
72.0
CRAP 120.2
↑ Summary blib/lib/Parse/Yapp/Driver.pm »
1 #
2 # Module Parse::Yapp::Lalr
3 #
4 # Copyright © 1998, 1999, 2000, 2001, Francois Desarmenien.
5 # Copyright © 2017 William N. Braswell, Jr.
6 # (see the pod text in Parse::Yapp module for use and distribution rights)
7 #
8 package Parse::Yapp::Lalr;
9 3 @ISA=qw( Parse::Yapp::Grammar );
10  
11 3 require 5.004;
12  
13 3 ▶use Parse::Yapp::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =for nobody
16  
17 Parse::Yapp::Compile Object Structure:
18 --------------------------------------
19 {
20 GRAMMAR => Parse::Yapp::Grammar,
21 STATES => [ { CORE => [ items... ],
22 ACTIONS => { term => action }
23 GOTOS => { nterm => stateno }
24 }... ]
25 CONFLICTS=>{ SOLVED => { stateno => [ ruleno, token, solved ] },
26 FORCED => { TOTAL => [ nbsr, nbrr ],
27 DETAIL => { stateno => { TOTAL => [ nbsr, nbrr ] }
28 LIST => [ ruleno, token ]
29 }
30 }
31 }
32 }
33  
34 'items' are of form: [ ruleno, dotpos ]
35 'term' in ACTIONS is '' means default action
36 'action' may be:
37 undef: explicit error (nonassociativity)
38 0 : accept
39 >0 : shift and go to state 'action'
40 <0 : reduce using rule -'action'
41 'solved' may have values of:
42 'shift' if solved as Shift
43 'reduce' if solved as Reduce
44 'error' if solved by discarding both Shift and Reduce (nonassoc)
45  
46 SOLVED is a set of states containing Solved conflicts
47 FORCED are forced conflict resolutions
48  
49 nbsr and nbrr are number of shift/reduce and reduce/reduce conflicts
50  
51 TOTAL is the total number of SR/RR conflicts for the parser
52  
53 DETAIL is the detail of conflicts for each state
54 TOTAL is the total number of SR/RR conflicts for a state
55 LIST is the list of discarded reductions (for display purpose only)
56  
57  
58 =cut
59  
60 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61  
62 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 ###############
65 # Constructor #
66 ###############
67 10 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
68 10 my($class)=shift;
69  
70 10 ▶ ref($class)
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 10 10
71 and $class=ref($class);
72  
73 10 my($self)=$class->SUPER::new(@_);
74 10 $self->_Compile();
75 10 bless($self,$class);
76 }
77 ###########
78 # Methods #
79 ###########
80  
81 ###########################
82 # Method To View Warnings #
83 ###########################
84 0 ▶sub Warnings {
Subroutine Warnings: not called CC=8 SCAR=42.8
Pod undocumented
85 0 my($self)=shift;
86 0 my($text);
87 0 my($nbsr,$nbrr)=@{$$self{CONFLICTS}{FORCED}{TOTAL}};
88  
89 0 $text=$self->SUPER::Warnings();
90  
91 $nbsr != $$self{GRAMMAR}{EXPECT}
92 0 ▶ and $text.="$nbsr shift/reduce conflict".($nbsr > 1 ? "s" : "");
Branch: 2 branches
BranchTrueFalseTotal
$nbsr != $self->{"GRAMMAR"}{"EXPECT"} and $text .= "$nbsr shift/reduce conflict" . ($nbsr > 1 ? "s" : "") 0 0 0
$nbsr > 1 ? : 0 0 0
93  
94 $nbrr
95 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$nbrr and do { $nbsr and $text .= ' and '; $text .= "$nbrr reduce/reduce conflict" . ($nbrr > 1 ? "s" : "") } 0 0 0
96 0 ▶ $nbsr
Branch: 1 branch
BranchTrueFalseTotal
$nbsr and $text .= ' and ' 0 0 0
97 and $text.=" and ";
98 0 ▶ $text.="$nbrr reduce/reduce conflict".($nbrr > 1 ? "s" : "");
Branch: 1 branch
BranchTrueFalseTotal
$nbrr > 1 ? : 0 0 0
99 };
100  
101 ( $nbsr != $$self{GRAMMAR}{EXPECT}
102 0 ▶ or $nbrr)
Branch: 1 branch
BranchTrueFalseTotal
$nbsr != $self->{"GRAMMAR"}{"EXPECT"} || $nbrr and $text .= "\n" 0 0 0
Condition0%0/3
$nbsr != $self->{"GRAMMAR"}{"EXPECT"} or $nbrr
l!l&&r!l&&!r
000
MC/DC0%0/2
$nbsr != $self->{"GRAMMAR"}{"EXPECT"} or $nbrr
$nbsr != $self->{"GRAMMAR"}{"EXPECT"} $nbrr
Truth table0%0/3
A or B
A= $nbsr != $self->{"GRAMMAR"}{"EXPECT"}B= $nbrr
ABresult
1X1
011
000
103 and $text.="\n";
104  
105 0 $text;
106 }
107 #############################
108 # Method To View DFA States #
109 #############################
110 0 ▶sub ShowDfa {
Subroutine ShowDfa: not called CC=25 SCAR=64.8
Pod undocumented
111 0 my($self)=shift;
112 0 my($text);
113 0 my($grammar,$states)=($$self{GRAMMAR}, $$self{STATES});
114  
115 0 for my $stateno (0..$#$states) {
116 0 my(@shifts,@reduces,@errors,$default);
117  
118 0 $text.="State $stateno:\n\n";
119  
120 #Dump Kernel Items
121 0 ▶ for (sort { $$a[0] <=> $$b[0]
Condition0%0/3
$Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0] || $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0] || $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
$Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0] $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
Truth table0%0/3
A || B
A= $Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0]B= $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
ABresult
1X1
011
000
122 0 or $$a[1] <=> $$b[1] } @{$$states[$stateno]{'CORE'}}) {
123 0 my($ruleno,$pos)=@$_;
124 0 my($lhs,$rhs)=@{$$grammar{RULES}[$ruleno]}[0,1];
125 0 my(@rhscopy)=@$rhs;
126
127 0 ▶ $ruleno
Branch: 1 branch
BranchTrueFalseTotal
$ruleno or $rhscopy[-1] = "\$end" 0 0 0
128 or $rhscopy[-1] = '$end';
129  
130 0 splice(@rhscopy,$pos,0,'.');
131 0 $text.= "\t$lhs -> ".join(' ',@rhscopy)."\t(Rule $ruleno)\n";
132 }
133  
134 #Prepare Actions
135 0 for (keys(%{$$states[$stateno]{ACTIONS}})) {
136 0 my($term,$action)=($_,$$states[$stateno]{ACTIONS}{$_});
137  
138 0 ▶ $term eq chr(0)
Branch: 1 branch
BranchTrueFalseTotal
$term eq "\0" and $term = "\$end" 0 0 0
139 and $term = '$end';
140  
141 not defined($action)
142 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
defined $action or do { push @errors, $term; next } 0 0 0
143 0 push(@errors,$term);
144 0 next;
145 };
146  
147 $action > 0
148 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$action > 0 and do { push @shifts, [$term, $action]; next } 0 0 0
149 0 push(@shifts,[ $term, $action ]);
150 0 next;
151 };
152  
153 0 $action = -$action;
154  
155 $term
156 0 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
$term or do { $default = ["\$default", $action]; next } 0 0 0
157 0 $default= [ '$default', $action ];
158 0 next;
159 };
160  
161 0 push(@reduces,[ $term, $action ]);
162 }
163  
164 #Dump shifts
165 @shifts
166 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
@shifts and do { $text .= "\n"; foreach $_ (sort {$Parse::Yapp::Lalr::a->[0] cmp $Parse::Yapp::Lalr::b->[0];} @shifts) { () = @$_; $text .= "\t$term\tshift, and go to state $shift\n"; } } 0 0 0
167 0 $text.="\n";
168 0 for (sort { $$a[0] cmp $$b[0] } @shifts) {
169 0 my($term,$shift)=@$_;
170  
171 0 $text.="\t$term\tshift, and go to state $shift\n";
172 }
173 };
174  
175 #Dump errors
176 @errors
177 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
@errors and do { $text .= "\n"; foreach my $term (sort {$a cmp $b;} @errors) { $text .= "\t$term\terror (nonassociative)\n"; } } 0 0 0
178 0 $text.="\n";
179 0 for my $term (sort { $a cmp $b } @errors) {
180 0 $text.="\t$term\terror (nonassociative)\n";
181 }
182 };
183  
184 #Prepare reduces
185 exists($$self{CONFLICTS}{FORCED}{DETAIL}{$stateno})
186 0 ▶ and push(@reduces,@{$$self{CONFLICTS}{FORCED}{DETAIL}{$stateno}{LIST}});
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{"CONFLICTS"}{"FORCED"}{"DETAIL"}{$stateno} and push @reduces, @{$self->{"CONFLICTS"}{"FORCED"}{"DETAIL"}{$stateno}{"LIST"};} 0 0 0
187  
188 0 ▶ @reduces=sort { $$a[0] cmp $$b[0] or $$a[1] <=> $$b[1] } @reduces;
Condition0%0/3
$Parse::Yapp::Lalr::a->[0] cmp $Parse::Yapp::Lalr::b->[0] || $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Parse::Yapp::Lalr::a->[0] cmp $Parse::Yapp::Lalr::b->[0] || $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
$Parse::Yapp::Lalr::a->[0] cmp $Parse::Yapp::Lalr::b->[0] $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
Truth table0%0/3
A || B
A= $Parse::Yapp::Lalr::a->[0] cmp $Parse::Yapp::Lalr::b->[0]B= $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
ABresult
1X1
011
000
189  
190 0 ▶ defined($default)
Branch: 1 branch
BranchTrueFalseTotal
defined $default and push @reduces, $default 0 0 0
191 and push(@reduces,$default);
192  
193 #Dump reduces
194 @reduces
195 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
@reduces and do { $text .= "\n"; foreach $_ (@reduces) { () = @$_; my $discard; $ruleno < 0 and do { ++$discard; $ruleno = -$ruleno }; $text .= "\t$term\t" . ($discard ? "[" : ""); if ($ruleno) { $text .= "reduce using rule $ruleno " . qq[($grammar->{"RULES"}[$ruleno][0])]; } else { $text .= 'accept'; } ; $text .= ($discard ? "]" : "") . "\n"; } } 0 0 0
196 0 $text.="\n";
197 0 for (@reduces) {
198 0 my($term,$ruleno)=@$_;
199 0 my($discard);
200  
201 $ruleno < 0
202 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$ruleno < 0 and do { ++$discard; $ruleno = -$ruleno } 0 0 0
203 0 ++$discard;
204 0 $ruleno = -$ruleno;
205 };
206  
207 0 ▶ $text.= "\t$term\t".($discard ? "[" : "");
Branch: 1 branch
BranchTrueFalseTotal
$discard ? : 0 0 0
208 0 ▶ if($ruleno) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ruleno) { } 0 0 0
209 0 $text.= "reduce using rule $ruleno ".
210 "($$grammar{RULES}[$ruleno][0])";
211 }
212 else {
213 0 $text.='accept';
214 }
215 0 ▶ $text.=($discard ? "]" : "")."\n";
Branch: 1 branch
BranchTrueFalseTotal
$discard ? : 0 0 0
216 }
217 };
218  
219 #Dump gotos
220 exists($$states[$stateno]{GOTOS})
221 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $states->[$stateno]{"GOTOS"} and do { $text .= "\n"; foreach $_ (keys %{$$states[$stateno]{"GOTOS"};}) { $text .= qq[\t$_\tgo to state $states->[$stateno]{"GOTOS"}{$_}\n]; } } 0 0 0
222 0 $text.= "\n";
223 0 for (keys(%{$$states[$stateno]{GOTOS}})) {
224 0 $text.= "\t$_\tgo to state $$states[$stateno]{GOTOS}{$_}\n";
225 }
226 };
227  
228 0 $text.="\n";
229 }
230 0 $text;
231 }
232  
233 ######################################
234 # Method to get summary about parser #
235 ######################################
236 0 ▶sub Summary {
Subroutine Summary: not called CC=1 SCAR=6.9
Pod undocumented
237 0 my($self)=shift;
238 0 my($text);
239  
240 0 $text=$self->SUPER::Summary();
241 $text.="Number of states : ".
242 0 scalar(@{$$self{STATES}})."\n";
243 0 $text;
244 }
245  
246 #######################################
247 # Method To Get Infos about conflicts #
248 #######################################
249 0 ▶sub Conflicts {
Subroutine Conflicts: not called CC=10 SCAR=47.0
Pod undocumented
250 0 my($self)=shift;
251 0 my($states)=$$self{STATES};
252 0 my($conflicts)=$$self{CONFLICTS};
253 0 my($text);
254  
255 0 for my $stateno ( sort { $a <=> $b } keys(%{$$conflicts{SOLVED}})) {
256  
257 0 for (@{$$conflicts{SOLVED}{$stateno}}) {
258 0 my($ruleno,$token,$how)=@$_;
259  
260 0 ▶ $token eq chr(0)
Branch: 1 branch
BranchTrueFalseTotal
$token eq "\0" and $token = "\$end" 0 0 0
261 and $token = '$end';
262  
263 0 $text.="Conflict in state $stateno between rule ".
264 "$ruleno and token $token resolved as $how.\n";
265 }
266 };
267  
268 0 for my $stateno ( sort { $a <=> $b } keys(%{$$conflicts{FORCED}{DETAIL}})) {
269 0 my($nbsr,$nbrr)=@{$$conflicts{FORCED}{DETAIL}{$stateno}{TOTAL}};
270  
271 0 $text.="State $stateno contains ";
272  
273 0 ▶ $nbsr
Branch: 2 branches
BranchTrueFalseTotal
$nbsr and $text .= "$nbsr shift/reduce conflict" . ($nbsr > 1 ? "s" : "") 0 0 0
$nbsr > 1 ? : 0 0 0
274 and $text.="$nbsr shift/reduce conflict".
275 ($nbsr > 1 ? "s" : "");
276  
277 $nbrr
278 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$nbrr and do { $nbsr and $text .= ' and '; $text .= "$nbrr reduce/reduce conflict" . ($nbrr > 1 ? "s" : "") } 0 0 0
279 0 ▶ $nbsr
Branch: 1 branch
BranchTrueFalseTotal
$nbsr and $text .= ' and ' 0 0 0
280 and $text.=" and ";
281  
282 0 ▶ $text.="$nbrr reduce/reduce conflict".
Branch: 1 branch
BranchTrueFalseTotal
$nbrr > 1 ? : 0 0 0
283 ($nbrr > 1 ? "s" : "");
284 };
285 0 $text.="\n";
286 };
287  
288 0 $text;
289 }
290  
291 #################################
292 # Method to dump parsing tables #
293 #################################
294 9 ▶sub DfaTable {
Subroutine DfaTable: called CC=9 SCAR=22.0
Pod undocumented
295 9 my($self)=shift;
296 9 my($states)=$$self{STATES};
297 9 my($stateno);
298 my($text);
299  
300 9 $text="[\n\t{";
301  
302 $text.=join("\n\t},\n\t{",
303 map {
304 9 my($state)=$_;
305 107 my($text);
306  
307 107 $text="#State ".$stateno++."\n\t\t";
308  
309 ( not exists($$state{ACTIONS}{''})
310 63 or keys(%{$$state{ACTIONS}}) > 1)
311 107 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
!exists $state->{"ACTIONS"}{""} || keys %{$$state{"ACTIONS"};} > 1 and do { $text .= "ACTIONS => {\n\t\t\t"; $text .= join(",\n\t\t\t", map({() = ($_, $state->{"ACTIONS"}{$_}); my $text; if (substr($term, 0, 1) eq "'") { $term =~ s/([\@\$\"])/\\$1/g; $term =~ s/^'|'$/"/g; } else { $term = $term eq "\0" ? "''" : "'${term}'"; } ; if (defined $action) { $action = int $action; } else { $action = "undef"; } ; "$term => $action";} grep({$_;} keys %{$$state{"ACTIONS"};}))); $text .= "\n\t\t}" } 57 50 107
Condition100%3/3
not exists $state->{"ACTIONS"}{""} or keys %{$$state{"ACTIONS"};} > 1
l!l&&r!l&&!r
441350
MC/DC100%2/2
not exists $state->{"ACTIONS"}{""} or keys %{$$state{"ACTIONS"};} > 1
not exists $state->{"ACTIONS"}{""} keys %{$$state{"ACTIONS"};} > 1
Truth table100%3/3
not(A) or B
A= not exists $state->{"ACTIONS"}{""}B= keys %{$$state{"ACTIONS"};} > 1
ABresult
1X1
011
000
312  
313 57 $text.="ACTIONS => {\n\t\t\t";
314  
315 $text.=join(",\n\t\t\t",
316 map {
317 126 my($term,$action)=($_,$$state{ACTIONS}{$_});
318 126 my($text);
319  
320 126 ▶ if(substr($term,0,1) eq "'") {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($term, 0, 1) eq q[']) { } 96 30 126
321 96 $term=~s/([\@\$\"])/\\$1/g;
322 96 $term=~s/^'|'$/"/g;
323 }
324 else {
325 30 ▶ $term= $term eq chr(0)
Branch: 1 branch
BranchTrueFalseTotal
$term eq "\000" ? : 9 21 30
326 ? "''"
327 : "'$term'";
328 }
329  
330 126 ▶ if(defined($action)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action) { } 125 1 126
331 125 $action=int($action);
332 }
333 else {
334 1 $action='undef';
335 }
336  
337 126 "$term => $action";
338
339 57 } grep { $_ } keys(%{$$state{ACTIONS}}));
340  
341 57 $text.="\n\t\t}";
342 };
343  
344 exists($$state{ACTIONS}{''})
345 107 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"ACTIONS"}{""} and do { keys %{$$state{"ACTIONS"};} > 1 and $text .= ",\n\t\t"; $text .= qq[DEFAULT => $state->{"ACTIONS"}{""}] } 63 44 107
346 63 ▶ keys(%{$$state{ACTIONS}}) > 1
Branch: 1 branch
BranchTrueFalseTotal
keys %{$$state{"ACTIONS"};} > 1 and $text .= ",\n\t\t" 13 50 63
347 and $text.=",\n\t\t";
348  
349 63 $text.="DEFAULT => $$state{ACTIONS}{''}";
350 };
351  
352 exists($$state{GOTOS})
353 107 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"GOTOS"} and do { $text .= ",\n\t\tGOTOS => {\n\t\t\t"; $text .= join(",\n\t\t\t", map({() = ($_, $state->{"GOTOS"}{$_}); my $text; "'${nterm}' => $stateno";} keys %{$$state{"GOTOS"};})); $text .= "\n\t\t}" } 29 78 107
354 29 $text.=",\n\t\tGOTOS => {\n\t\t\t";
355 $text.=join(",\n\t\t\t",
356 map {
357 43 my($nterm,$stateno)=($_,$$state{GOTOS}{$_});
358 43 my($text);
359  
360 43 "'$nterm' => $stateno";
361
362 29 } keys(%{$$state{GOTOS}}));
363 29 $text.="\n\t\t}";
364 };
365  
366 107 $text;
367  
368 }@$states);
369  
370 9 $text.="\n\t}\n]";
371  
372 9 $text;
373  
374 }
375  
376  
377 ####################################
378 # Method to build Dfa from Grammar #
379 ####################################
380 10 ▶sub _Compile {
Subroutine _Compile: called CC=1 SCAR=0.0
381 10 my($self)=shift;
382 10 my($grammar,$states);
383  
384 10 $grammar=$self->{GRAMMAR};
385  
386 10 $states = _LR0($grammar);
387  
388 10 $self->{CONFLICTS} = _LALR($grammar,$states);
389  
390 10 $self->{STATES}=$states;
391 }
392  
393 #########################
394 # LR0 States Generation #
395 #########################
396 #
397 ###########################
398 # General digraph routine #
399 ###########################
400 30 ▶sub _Digraph {
Subroutine _Digraph: called CC=3 SCAR=11.0
401 30 my($rel,$F)=@_;
402 30 my(%N,@S);
403 30 my($infinity)=(~(1<<31));
404 30 my($Traverse);
405  
406 10659 ▶ $Traverse = sub {
Subroutine __ANON__: called CC=7 SCAR=19.5
407 10659 my($x,$d)=@_;
408 10659 my($y);
409  
410 10659 push(@S,$x);
411 10659 $N{$x}=$d;
412  
413 exists($$rel{$x})
414 10659 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $rel->{$x} and do { foreach $y (keys %{$$rel{$x};}) { exists $N{$y} or &$Traverse($y, $d + 1); $N{$y} < $N{$x} and $N{$x} = $N{$y}; $F->{$x} |= $F->{$y}; } } 9183 1476 10659
415 9183 for $y (keys(%{$$rel{$x}})) {
416 42992 ▶ exists($N{$y})
Branch: 1 branch
BranchTrueFalseTotal
exists $N{$y} or &$Traverse($y, $d + 1) 35171 7821 42992
417 or &$Traverse($y,$d+1);
418  
419 $N{$y} < $N{$x}
420 42992 ▶ and $N{$x} = $N{$y};
Branch: 1 branch
BranchTrueFalseTotal
$N{$y} < $N{$x} and $N{$x} = $N{$y} 448 42544 42992
421  
422 42992 $$F{$x}|=$$F{$y};
423 }
424 };
425  
426 $N{$x} == $d
427 10659 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$N{$x} == $d and do { while (1) { $y = pop @S; $N{$y} = $infinity; $y eq $x and last; $F->{$y} = $F->{$x}; } } 10322 337 10659
428 10322 for(;;) {
429 10659 $y=pop(@S);
430 10659 $N{$y}=$infinity;
431 10659 ▶ $y eq $x
Branch: 1 branch
BranchTrueFalseTotal
$y eq $x and last 10322 337 10659
432 and last;
433 337 $$F{$y}=$$F{$x};
434 }
435 };
436 30 };
437  
438 30 for (keys(%$rel)) {
439 9183 ▶ exists($N{$_})
Branch: 1 branch
BranchTrueFalseTotal
exists $N{$_} or &$Traverse($_, 1) 6345 2838 9183
440 or &$Traverse($_,1);
441 }
442 }
443 #######################
444 # Generate LR0 states #
445 #######################
446 =for nobody
447 Formula used for closures:
448  
449 CLOSE(A) = DCLOSE(A) u U (CLOSE(B) | A close B)
450  
451 where:
452  
453 DCLOSE(A) = { [ A -> alpha ] in P }
454  
455 A close B iff [ A -> B gamma ] in P
456  
457 =cut
458 10 ▶sub _SetClosures {
Subroutine _SetClosures: called CC=5 SCAR=16.1
459 10 my($grammar)=@_;
460 10 my($rel,$closures);
461  
462 10 for my $symbol (keys(%{$$grammar{NTERM}})) {
463 270 $closures->{$symbol}=pack('b'.@{$$grammar{RULES}});
464  
465 270 for my $ruleno (@{$$grammar{NTERM}{$symbol}}) {
466 888 my($rhs)=$$grammar{RULES}[$ruleno][1];
467  
468 888 vec($closures->{$symbol},$ruleno,1)=1;
469  
470 @$rhs > 0
471 and exists($$grammar{NTERM}{$$rhs[0]})
472 888 ▶ and ++$rel->{$symbol}{$$rhs[0]};
Branch: 1 branch
BranchTrueFalseTotal
@$rhs > 0 and exists $grammar->{"NTERM"}{$rhs->[0]} and ++$rel->{$symbol}{$rhs->[0]} 561 327 888
Condition100%3/3
@$rhs > 0 and exists $grammar->{"NTERM"}{$rhs->[0]}
!ll&&!rl&&r
44283561
MC/DC100%2/2
@$rhs > 0 and exists $grammar->{"NTERM"}{$rhs->[0]}
@$rhs > 0 exists $grammar->{"NTERM"}{$rhs->[0]}
Truth table100%3/3
A and B
A= @$rhs > 0B= exists $grammar->{"NTERM"}{$rhs->[0]}
ABresult
0X0
100
111
473 }
474 }
475 10 _Digraph($rel,$closures);
476  
477 10 $closures
478 }
479  
480 1718 ▶sub _Closures {
Subroutine _Closures: called CC=4 SCAR=13.9
481 1718 my($grammar,$core,$closures)=@_;
482 1718 my($ruleset)=pack('b'.@{$$grammar{RULES}});
483  
484 1718 for (@$core) {
485 3895 my($ruleno,$pos)=@$_;
486 3895 my($rhs)=$$grammar{RULES}[$ruleno][1];
487  
488 $pos < @$rhs
489 and exists($closures->{$$rhs[$pos]})
490 3895 ▶ and $ruleset|=$closures->{$$rhs[$pos]};
Branch: 1 branch
BranchTrueFalseTotal
$pos < @$rhs and exists $$closures{$rhs->[$pos]} and $ruleset |= $$closures{$rhs->[$pos]} 1210 2685 3895
Condition100%3/3
$pos < @$rhs and exists $$closures{$rhs->[$pos]}
!ll&&!rl&&r
96617191210
MC/DC100%2/2
$pos < @$rhs and exists $$closures{$rhs->[$pos]}
$pos < @$rhs exists $$closures{$rhs->[$pos]}
Truth table100%3/3
A and B
A= $pos < @$rhsB= exists $$closures{$rhs->[$pos]}
ABresult
0X0
100
111
491 }
492 56069 [ @$core, map { [ $_, 0 ] }
493 1330007 grep { vec($ruleset,$_,1) }
494 1718 0..$#{$$grammar{RULES}} ];
495 }
496  
497 1718 ▶sub _Transitions {
Subroutine _Transitions: called CC=7 SCAR=19.5
498 1718 my($grammar,$cores,$closures,$states,$stateno)=@_;
499 1718 my($core)=$$states[$stateno]{'CORE'};
500 1718 my(%transitions);
501  
502 1718 for (@{_Closures($grammar,$core,$closures)}) {
503 59964 my($ruleno,$pos)=@$_;
504 59964 my($rhs)=$$grammar{RULES}[$ruleno][1];
505  
506 $pos == @$rhs
507 59964 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$pos == @$rhs and do { push @{$$states[$stateno]{"ACTIONS"}{""};}, $ruleno; next } 1187 58777 59964
508 1187 push(@{$$states[$stateno]{ACTIONS}{''}},$ruleno);
509 1187 next;
510 };
511 58777 push(@{$transitions{$$rhs[$pos]}},[ $ruleno, $pos+1 ]);
512 }
513  
514 1718 for (keys(%transitions)) {
515 23159 my($symbol,$core)=($_,$transitions{$_});
516 58777 my($corekey)=join(',',map { join('.',@$_) }
517 23159 ▶ sort { $$a[0] <=> $$b[0]
Condition66%2/3
$Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0] || $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
l!l&&r!l&&!r
51966310
MC/DC0%0/2
$Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0] || $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
$Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0] $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
Truth table66%2/3
A || B
A= $Parse::Yapp::Lalr::a->[0] <=> $Parse::Yapp::Lalr::b->[0]B= $Parse::Yapp::Lalr::a->[1] <=> $Parse::Yapp::Lalr::b->[1]
ABresult
1X1
011
000
518 or $$a[1] <=> $$b[1] }
519 @$core);
520 23159 my($tostateno);
521  
522 exists($cores->{$corekey})
523 23159 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
exists $cores->{$corekey} or do { push @$states, {"CORE", $core}; $cores->{$corekey} = $states->$#* } 21451 1708 23159
524 1708 push(@$states,{ 'CORE' => $core });
525 1708 $cores->{$corekey}=$#$states;
526 };
527  
528 23159 $tostateno=$cores->{$corekey};
529 23159 push(@{$$states[$tostateno]{FROM}},$stateno);
530  
531 exists($$grammar{TERM}{$_})
532 23159 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $grammar->{"TERM"}{$_} and do { $states->[$stateno]{"ACTIONS"}{$_} = [$tostateno]; next } 11824 11335 23159
533 11824 $$states[$stateno]{ACTIONS}{$_} = [ $tostateno ];
534 11824 next;
535 };
536 11335 $$states[$stateno]{GOTOS}{$_} = $tostateno;
537 }
538 }
539  
540 10 ▶sub _LR0 {
Subroutine _LR0: called CC=2 SCAR=6.9
541 10 my($grammar)=@_;
542 10 my($states) = [];
543 10 my($stateno);
544 my($closures); #$closures={ nterm => ruleset,... }
545 10 my($cores)={}; # { "itemlist" => stateno, ... }
546 # where "itemlist" has the form:
547 # "ruleno.pos,ruleno.pos" ordered by ruleno,pos
548  
549 10 $closures = _SetClosures($grammar);
550 10 push(@$states,{ 'CORE' => [ [ 0, 0 ] ] });
551 10 ▶ for($stateno=0;$stateno<@$states;++$stateno) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stateno < @$states) 1718 10 1728
552 1718 _Transitions($grammar,$cores,$closures,$states,$stateno);
553 }
554  
555 10 $states
556 }
557  
558 #########################################################
559 # Add Lookahead tokens where needed to make LALR states #
560 #########################################################
561 =for nobody
562 Compute First sets for non-terminal using the following formula:
563  
564 FIRST(A) = { a in T u { epsilon } | A l a }
565 u
566 U { FIRST(B) | B in V and A l B }
567  
568 where:
569  
570 A l x iff [ A -> X1 X2 .. Xn x alpha ] in P and Xi =>* epsilon, 1 <= i <= n
571 =cut
572 10 ▶sub _SetFirst {
Subroutine _SetFirst: called CC=6 SCAR=17.9
573 10 my($grammar,$termlst,$terminx)=@_;
574 10 my($rel,$first)=( {}, {} );
575  
576 10 for my $symbol (keys(%{$$grammar{NTERM}})) {
577 270 $first->{$symbol}=pack('b'.@$termlst);
578  
579 RULE:
580 270 for my $ruleno (@{$$grammar{NTERM}{$symbol}}) {
581 888 my($rhs)=$$grammar{RULES}[$ruleno][1];
582  
583 888 for (@$rhs) {
584 exists($terminx->{$_})
585 868 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $terminx->{$_} and do { vec($first->{$symbol}, $terminx->{$_}, 1) = 1; next RULE } 299 569 868
586 299 vec($first->{$symbol},$terminx->{$_},1)=1;
587 299 next RULE;
588 };
589 569 ++$rel->{$symbol}{$_};
590 569 ▶ exists($$grammar{NULLABLE}{$_})
Branch: 1 branch
BranchTrueFalseTotal
exists $grammar->{"NULLABLE"}{$_} or next RULE 27 542 569
591 or next RULE;
592 }
593 47 vec($first->{$symbol},0,1)=1;
594 }
595 }
596 10 _Digraph($rel,$first);
597  
598 10 $first
599 }
600  
601 8974 ▶sub _Preds {
Subroutine _Preds: called CC=4 SCAR=13.9
602 8974 my($states,$stateno,$len)=@_;
603 8974 my($queue, $preds);
604  
605 8974 ▶ $len
Branch: 1 branch
BranchTrueFalseTotal
$len or return [$stateno] 1004 7970 8974
606 or return [ $stateno ];
607  
608 1004 $queue=[ [ $stateno, $len ] ];
609 1004 ▶ while(@$queue) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$queue) 3862 1004 4866
610 3862 my($pred) = shift(@$queue);
611 3862 my($stateno, $len) = @$pred;
612  
613 $len == 1
614 3862 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$len == 1 and do { push @$preds, @{$states->[$stateno]{"FROM"};}; next } 3169 693 3862
615 3169 push(@$preds,@{$states->[$stateno]{FROM}});
616 3169 next;
617 };
618  
619 2858 push(@$queue, map { [ $_, $len - 1 ] }
620 693 @{$states->[$stateno]{FROM}});
621 }
622  
623 # Pass @$preds through a hash to ensure unicity
624 1004 [ keys( %{ +{ map { ($_,1) } @$preds } } ) ];
625 }
626  
627 1235 ▶sub _FirstSfx {
Subroutine _FirstSfx: called CC=5 SCAR=16.1
628 1235 my($grammar,$firstset,$termlst,$terminx,$ruleno,$pos,$key)=@_;
629 1235 my($first)=pack('b'.@$termlst);
630 1235 my($rhs)=$$grammar{RULES}[$ruleno][1];
631  
632 1235 ▶ for (;$pos < @$rhs;++$pos) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < @$rhs) 817 597 1414
633 exists($terminx->{$$rhs[$pos]})
634 817 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $$terminx{$rhs->[$pos]} and do { vec($first, $$terminx{$rhs->[$pos]}, 1) = 1; return $first } 395 422 817
635 395 vec($first,$terminx->{$$rhs[$pos]},1)=1;
636 395 return($first);
637 };
638 422 $first|=$firstset->{$$rhs[$pos]};
639  
640 422 ▶ vec($first,0,1)
Branch: 1 branch
BranchTrueFalseTotal
vec $first, 0, 1 and vec($first, 0, 1) = 0 181 241 422
641 and vec($first,0,1)=0;
642  
643 422 ▶ exists($$grammar{NULLABLE}{$$rhs[$pos]})
Branch: 1 branch
BranchTrueFalseTotal
exists $grammar->{"NULLABLE"}{$rhs->[$pos]} or return $first 179 243 422
644 or return($first);
645  
646 }
647 597 vec($first,0,1)=1;
648 597 $first;
649 }
650  
651 =for noboby
652 Compute Follow sets using following formula:
653  
654 FOLLOW(p,A) = READ(p,A)
655 u
656 U { FOLLOW(q,B) | (p,A) include (q,B)
657  
658 where:
659
660 READ(p,A) = U { FIRST(beta) | [ A -> alpha A . beta ] in KERNEL(GOTO(p,A))
661 } - { epsilon }
662  
663 (p,a) include (q,B) iff [ B -> alpha A . beta ] in KERNEL(GOTO(p,A),
664 epsilon in FIRST(beta) and
665 q in PRED(p,alpha)
666 =cut
667 10 ▶sub _ComputeFollows {
Subroutine _ComputeFollows: called CC=13 SCAR=25.6
668 10 my($grammar,$states,$termlst)=@_;
669 10 my($firstset,$terminx);
670 10 my($inconsistent, $rel, $follows, $sfx)= ( {}, {}, {}, {} );
671  
672 10 %$terminx= map { ($termlst->[$_],$_) } 0..$#$termlst;
673  
674 10 $firstset=_SetFirst($grammar,$termlst,$terminx);
675  
676 10 for my $stateno (0..$#$states) {
677 1718 my($state)=$$states[$stateno];
678  
679 exists($$state{ACTIONS}{''})
680 1148 and ( @{$$state{ACTIONS}{''}} > 1
681 1111 or keys(%{$$state{ACTIONS}}) > 1 )
682 1718 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"ACTIONS"}{""} and @{$state->{"ACTIONS"}{""};} > 1 || keys %{$$state{"ACTIONS"};} > 1 and do { ++$inconsistent->{$stateno}; foreach my $ruleno (@{$$state{"ACTIONS"}{""};}) { () = @{$$grammar{"RULES"}[$ruleno];}[0, 1]; foreach my $predno (@{_Preds($states, $stateno, scalar @$rhs);}) { ++$$rel{"$stateno.$ruleno"}{"$predno.$lhs"}; } ; } } 397 1321 1718
Condition100%6/6
exists $state->{"ACTIONS"}{""} and @{$state->{"ACTIONS"}{""};} > 1 || keys %{$$state{"ACTIONS"};} > 1
!ll&&!rl&&r
570751397
@{$state->{"ACTIONS"}{""};} > 1 or keys %{$$state{"ACTIONS"};} > 1
l!l&&r!l&&!r
37360751
MC/DC100%3/3
exists $state->{"ACTIONS"}{""} and @{$state->{"ACTIONS"}{""};} > 1 || keys %{$$state{"ACTIONS"};} > 1
exists $state->{"ACTIONS"}{""} @{$state->{"ACTIONS"}{""};} > 1 keys %{$$state{"ACTIONS"};} > 1
Truth table100%4/4
A and B or C
A= exists $state->{"ACTIONS"}{""}B= @{$state->{"ACTIONS"}{""};} > 1C= keys %{$$state{"ACTIONS"};} > 1
ABCresult
0XX0
1000
11X1
1011
683 397 ++$inconsistent->{$stateno};
684  
685 397 for my $ruleno (@{$$state{ACTIONS}{''}}) {
686 436 my($lhs,$rhs)=@{$$grammar{RULES}[$ruleno]}[0,1];
687  
688 436 for my $predno (@{_Preds($states,$stateno,scalar(@$rhs))}) {
689 11975 ++$rel->{"$stateno.$ruleno"}{"$predno.$lhs"};
690 }
691 }
692 };
693  
694 exists($$state{GOTOS})
695 1718 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"GOTOS"} or next 656 1062 1718
696  
697 656 for my $symbol (keys(%{$$state{GOTOS}})) {
698 11335 my($tostate)=$$states[$$state{GOTOS}{$symbol}];
699 11335 my($goto)="$stateno.$symbol";
700  
701 11335 $follows->{$goto}=pack('b'.@$termlst);
702  
703 11335 for my $item (@{$$tostate{'CORE'}}) {
704 39723 my($ruleno,$pos)=@$item;
705 39723 my($key)="$ruleno.$pos";
706  
707 exists($sfx->{$key})
708 39723 ▶ or $sfx->{$key} = _FirstSfx($grammar,$firstset,
Branch: 1 branch
BranchTrueFalseTotal
exists $sfx->{$key} or $sfx->{$key} = _FirstSfx($grammar, $firstset, $termlst, $terminx, $ruleno, $pos, $key) 38488 1235 39723
709 $termlst,$terminx,
710 $ruleno,$pos,$key);
711  
712 39723 $follows->{$goto}|=$sfx->{$key};
713  
714 vec($follows->{$goto},0,1)
715 39723 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
vec $follows->{$goto}, 0, 1 and do { my($lhs) = $grammar->{"RULES"}[$ruleno][0]; vec($follows->{$goto}, 0, 1) = 0; foreach my $predno (@{_Preds($states, $stateno, $pos - 1);}) { ++$rel->{$goto}{"$predno.$lhs"}; } } 8538 31185 39723
716 8538 my($lhs)=$$grammar{RULES}[$ruleno][0];
717  
718 8538 vec($follows->{$goto},0,1)=0;
719  
720 8538 for my $predno (@{_Preds($states,$stateno,$pos-1)}) {
721 30297 ++$rel->{$goto}{"$predno.$lhs"};
722 }
723 };
724 }
725 }
726 }
727 10 _Digraph($rel,$follows);
728  
729 10 ($follows,$inconsistent)
730 }
731  
732 10 ▶sub _ComputeLA {
Subroutine _ComputeLA: called CC=6 SCAR=17.9
733 10 my($grammar,$states)=@_;
734 10 my($termlst)= [ '',keys(%{$$grammar{TERM}}) ];
735  
736 10 my($follows,$inconsistent) = _ComputeFollows($grammar,$states,$termlst);
737  
738 10 for my $stateno ( keys(%$inconsistent ) ) {
739 397 my($state)=$$states[$stateno];
740 397 my($conflict);
741  
742 #NB the sort is VERY important for conflicts resolution order
743 397 for my $ruleno (sort { $a <=> $b }
744 397 @{$$state{ACTIONS}{''}}) {
745 436 for my $term ( map { $termlst->[$_] } grep {
746 46056 vec($follows->{"$stateno.$ruleno"},$_,1) }
747 0..$#$termlst) {
748 7755 ▶ exists($$state{ACTIONS}{$term})
Branch: 1 branch
BranchTrueFalseTotal
exists $state->{"ACTIONS"}{$term} and ++$conflict 898 6857 7755
749 and ++$conflict;
750 7755 push(@{$$state{ACTIONS}{$term}},-$ruleno);
751 }
752 }
753 397 delete($$state{ACTIONS}{''});
754 $conflict
755 397 ▶ or delete($inconsistent->{$stateno});
Branch: 1 branch
BranchTrueFalseTotal
$conflict or delete $inconsistent->{$stateno} 155 242 397
756 }
757  
758 $inconsistent
759 10 }
760  
761 #############################
762 # Solve remaining conflicts #
763 #############################
764  
765 10 ▶sub _SolveConflicts {
Subroutine _SolveConflicts: called CC=20 SCAR=30.0
766 10 my($grammar,$states,$inconsistent)=@_;
767 10 my(%rulesprec,$RulePrec);
768 10 my($conflicts)={ SOLVED => {},
769 FORCED => { TOTAL => [ 0, 0 ],
770 DETAIL => {}
771 }
772 };
773  
774 845 ▶ $RulePrec = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
775 845 my($ruleno)=@_;
776 845 my($rhs,$rprec)=@{$$grammar{RULES}[$ruleno]}[1,2];
777 845 my($lastterm);
778  
779 845 ▶ defined($rprec)
Branch: 1 branch
BranchTrueFalseTotal
defined $rprec and return $rprec 233 612 845
780 and return($rprec);
781  
782 exists($rulesprec{$ruleno})
783 612 ▶ and return($rulesprec{$ruleno});
Branch: 1 branch
BranchTrueFalseTotal
exists $rulesprec{$ruleno} and return $rulesprec{$ruleno} 545 67 612
784  
785 67 $lastterm=(grep { exists($$grammar{TERM}{$_}) } @$rhs)[-1];
786  
787 defined($lastterm)
788 and ref($$grammar{TERM}{$lastterm})
789 67 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
defined $lastterm and ref $grammar->{"TERM"}{$lastterm} and do { $rulesprec{$ruleno} = $grammar->{"TERM"}{$lastterm}[1]; return $rulesprec{$ruleno} } 48 19 67
Condition66%2/3
defined $lastterm and ref $grammar->{"TERM"}{$lastterm}
!ll&&!rl&&r
19048
MC/DC50%1/2
defined $lastterm and ref $grammar->{"TERM"}{$lastterm}
defined $lastterm ref $grammar->{"TERM"}{$lastterm}
Truth table66%2/3
A and B
A= defined $lasttermB= ref $grammar->{"TERM"}{$lastterm}
ABresult
0X0
100
111
790 48 $rulesprec{$ruleno}=$$grammar{TERM}{$lastterm}[1];
791 48 return($rulesprec{$ruleno});
792 };
793  
794 19 undef;
795 10 };
796  
797 10 for my $stateno (keys(%$inconsistent)) {
798 155 my($state)=$$states[$stateno];
799 155 my($actions)=$$state{ACTIONS};
800 155 my($nbsr,$nbrr);
801  
802 155 for my $term ( keys(%$actions) ) {
803 5702 my($act)=$$actions{$term};
804  
805 5702 ▶ @$act > 1
Branch: 1 branch
BranchTrueFalseTotal
@$act > 1 or next 895 4807 5702
806 or next;
807  
808 $$act[0] > 0
809 and ref($$grammar{TERM}{$term})
810 895 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$act->[0] > 0 and ref $grammar->{"TERM"}{$term} and do { () = @{$grammar->{"TERM"}{$term};}; my($k, $error); for ($k = 1; $k < @$act; ++$k) { my($ruleno) = -$act->[$k]; my($rprec) = &$RulePrec($ruleno); defined $rprec or next; $tprec > $rprec || $tprec == $rprec && $assoc eq "RIGHT" and do { push @{$$conflicts{"SOLVED"}{$stateno};}, [$ruleno, $term, "shift"]; splice @$act, $k--, 1; next }; $tprec < $rprec || $assoc eq "LEFT" and do { push @{$$conflicts{"SOLVED"}{$stateno};}, [$ruleno, $term, "reduce"]; $act->[0] > 0 and do { splice @$act, 0, 1; --$k }; next }; push @{$$conflicts{"SOLVED"}{$stateno};}, [$ruleno, $term, "error"]; splice @$act, $k--, 1; $act->[0] > 0 and do { splice @$act, 0, 1; ++$error; --$k }; } ; $error and unshift @$act, undef } 842 53 895
Condition100%3/3
$act->[0] > 0 and ref $grammar->{"TERM"}{$term}
!ll&&!rl&&r
4013842
MC/DC100%2/2
$act->[0] > 0 and ref $grammar->{"TERM"}{$term}
$act->[0] > 0 ref $grammar->{"TERM"}{$term}
Truth table100%3/3
A and B
A= $act->[0] > 0B= ref $grammar->{"TERM"}{$term}
ABresult
0X0
100
111
811 842 my($assoc,$tprec)=@{$$grammar{TERM}{$term}};
812 842 my($k,$error);
813  
814 842 ▶ for ($k=1;$k<@$act;++$k) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @$act) 845 842 1687
815 845 my($ruleno)=-$$act[$k];
816 845 my($rprec)=&$RulePrec($ruleno);
817  
818 845 ▶ defined($rprec)
Branch: 1 branch
BranchTrueFalseTotal
defined $rprec or next 826 19 845
819 or next;
820  
821 ( $tprec > $rprec
822 or ( $tprec == $rprec and $assoc eq 'RIGHT'))
823 826 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$tprec > $rprec || $tprec == $rprec && $assoc eq "RIGHT" and do { push @{$$conflicts{"SOLVED"}{$stateno};}, [$ruleno, $term, "shift"]; splice @$act, $k--, 1; next } 542 284 826
Condition100%6/6
$tprec > $rprec or $tprec == $rprec and $assoc eq "RIGHT"
l!l&&r!l&&!r
5348284
$tprec == $rprec and $assoc eq "RIGHT"
!ll&&!rl&&r
233518
MC/DC100%3/3
$tprec > $rprec or $tprec == $rprec and $assoc eq "RIGHT"
$tprec > $rprec $tprec == $rprec $assoc eq "RIGHT"
Truth table100%4/4
A or B and C
A= $tprec > $rprecB= $tprec == $rprecC= $assoc eq "RIGHT"
ABCresult
1XX1
0111
00X0
0100
824 542 push(@{$$conflicts{SOLVED}{$stateno}},
825 [ $ruleno, $term, 'shift' ]);
826 542 splice(@$act,$k--,1);
827 542 next;
828 };
829 ( $tprec < $rprec
830 or $assoc eq 'LEFT')
831 284 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$tprec < $rprec || $assoc eq "LEFT" and do { push @{$$conflicts{"SOLVED"}{$stateno};}, [$ruleno, $term, "reduce"]; $act->[0] > 0 and do { splice @$act, 0, 1; --$k }; next } 283 1 284
Condition100%3/3
$tprec < $rprec or $assoc eq "LEFT"
l!l&&r!l&&!r
233501
MC/DC100%2/2
$tprec < $rprec or $assoc eq "LEFT"
$tprec < $rprec $assoc eq "LEFT"
Truth table100%3/3
A or B
A= $tprec < $rprecB= $assoc eq "LEFT"
ABresult
1X1
011
000
832 283 push(@{$$conflicts{SOLVED}{$stateno}},
833 [ $ruleno, $term, 'reduce' ]);
834 $$act[0] > 0
835 283 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$act->[0] > 0 and do { splice @$act, 0, 1; --$k } 283 0 283
836 283 splice(@$act,0,1);
837 283 --$k;
838 };
839 283 next;
840 };
841 1 push(@{$$conflicts{SOLVED}{$stateno}},
842 [ $ruleno, $term, 'error' ]);
843 1 splice(@$act,$k--,1);
844 $$act[0] > 0
845 1 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$act->[0] > 0 and do { splice @$act, 0, 1; ++$error; --$k } 1 0 1
846 1 splice(@$act,0,1);
847 1 ++$error;
848 1 --$k;
849 };
850 }
851 842 ▶ $error
Branch: 1 branch
BranchTrueFalseTotal
$error and unshift @$act, undef 1 841 842
852 and unshift(@$act,undef);
853 };
854  
855 @$act > 1
856 895 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
@$act > 1 and do { $nbrr += @$act - 2; $act->[0] > 0 ? $nbsr : $nbrr += 1; push @{$$conflicts{"FORCED"}{"DETAIL"}{$stateno}{"LIST"};}, map({[$term, $_];} splice(@$act, 1)) } 72 823 895
857 72 $nbrr += @$act - 2;
858 72 ▶ ($$act[0] > 0 ? $nbsr : $nbrr) += 1;
Branch: 1 branch
BranchTrueFalseTotal
$act->[0] > 0 ? : 30 42 72
859 72 push(@{$$conflicts{FORCED}{DETAIL}{$stateno}{LIST}},
860 72 map { [ $term, $_ ] } splice(@$act,1));
861 };
862 }
863  
864 $nbsr
865 155 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$nbsr and do { $conflicts->{"FORCED"}{"TOTAL"}[0] += $nbsr; $conflicts->{"FORCED"}{"DETAIL"}{$stateno}{"TOTAL"}[0] += $nbsr } 27 128 155
866 27 $$conflicts{FORCED}{TOTAL}[0]+=$nbsr;
867 27 $$conflicts{FORCED}{DETAIL}{$stateno}{TOTAL}[0]+=$nbsr;
868 };
869  
870 $nbrr
871 155 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
$nbrr and do { $conflicts->{"FORCED"}{"TOTAL"}[1] += $nbrr; $conflicts->{"FORCED"}{"DETAIL"}{$stateno}{"TOTAL"}[1] += $nbrr } 11 144 155
872 11 $$conflicts{FORCED}{TOTAL}[1]+=$nbrr;
873 11 $$conflicts{FORCED}{DETAIL}{$stateno}{TOTAL}[1]+=$nbrr;
874 };
875  
876 }
877  
878 $conflicts
879 10 }
880  
881 ###############################
882 # Make default reduce actions #
883 ###############################
884 10 ▶sub _SetDefaults {
Subroutine _SetDefaults: called CC=11 SCAR=24.0
885 10 my($states)=@_;
886  
887 10 for my $state (@$states) {
888 1718 my($actions)=$$state{ACTIONS};
889 1718 my(%reduces,$default,$nodefault);
890  
891 exists($$actions{''})
892 1718 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $actions->{""} and do { $actions->{""}[0] = -$actions->{""}[0]; ++$nodefault } 751 967 1718
893 751 $$actions{''}[0] = -$$actions{''}[0];
894 751 ++$nodefault;
895 };
896  
897 #shift error token => no default
898 exists($$actions{error})
899 1718 ▶ and $$actions{error}[0] > 0
Branch: 1 branch
BranchTrueFalseTotal
exists $actions->{"error"} and $actions->{"error"}[0] > 0 and ++$nodefault 56 1662 1718
Condition100%3/3
exists $actions->{"error"} and $actions->{"error"}[0] > 0
!ll&&!rl&&r
152913356
MC/DC100%2/2
exists $actions->{"error"} and $actions->{"error"}[0] > 0
exists $actions->{"error"} $actions->{"error"}[0] > 0
Truth table100%3/3
A and B
A= exists $actions->{"error"}B= $actions->{"error"}[0] > 0
ABresult
0X0
100
111
900 and ++$nodefault;
901  
902 1718 for my $term (keys(%$actions)) {
903  
904 19432 $$actions{$term}=$$actions{$term}[0];
905  
906 ( not defined($$actions{$term})
907 19432 ▶ or $$actions{$term} > 0
Branch: 1 branch
BranchTrueFalseTotal
!defined($actions->{$term}) || $actions->{$term} > 0 || $nodefault and next 12373 7059 19432
Condition100%6/6
not defined $actions->{$term} or $actions->{$term} > 0 or $nodefault
l!l&&r!l&&!r
115418327059
not defined $actions->{$term} or $actions->{$term} > 0
l!l&&r!l&&!r
1115407891
MC/DC100%3/3
not defined $actions->{$term} or $actions->{$term} > 0 or $nodefault
not defined $actions->{$term} $actions->{$term} > 0 $nodefault
Truth table100%4/4
not(A) or B or C
A= not defined $actions->{$term}B= $actions->{$term} > 0C= $nodefault
ABCresult
1XX1
01X1
0011
0000
908 or $nodefault)
909 and next;
910  
911 7059 push(@{$reduces{$$actions{$term}}},$term);
912 }
913  
914 1718 ▶ keys(%reduces) > 0
Branch: 1 branch
BranchTrueFalseTotal
keys %reduces > 0 or next 377 1341 1718
915 or next;
916  
917 416 $default=( map { $$_[0] }
918 40 ▶ sort { $$b[1] <=> $$a[1] or $$b[0] <=> $$a[0] }
Condition66%2/3
$Parse::Yapp::Lalr::b->[1] <=> $Parse::Yapp::Lalr::a->[1] || $Parse::Yapp::Lalr::b->[0] <=> $Parse::Yapp::Lalr::a->[0]
l!l&&r!l&&!r
3460
MC/DC0%0/2
$Parse::Yapp::Lalr::b->[1] <=> $Parse::Yapp::Lalr::a->[1] || $Parse::Yapp::Lalr::b->[0] <=> $Parse::Yapp::Lalr::a->[0]
$Parse::Yapp::Lalr::b->[1] <=> $Parse::Yapp::Lalr::a->[1] $Parse::Yapp::Lalr::b->[0] <=> $Parse::Yapp::Lalr::a->[0]
Truth table66%2/3
A || B
A= $Parse::Yapp::Lalr::b->[1] <=> $Parse::Yapp::Lalr::a->[1]B= $Parse::Yapp::Lalr::b->[0] <=> $Parse::Yapp::Lalr::a->[0]
ABresult
1X1
011
000
919 377 map { [ $_, scalar(@{$reduces{$_}}) ] }
920 keys(%reduces))[0];
921  
922 377 delete(@$actions{ @{$reduces{$default}} });
923 377 $$state{ACTIONS}{''}=$default;
924 }
925 }
926  
927 10 ▶sub _LALR {
Subroutine _LALR: called CC=1 SCAR=0.0
928 10 my($grammar,$states) = @_;
929 10 my($conflicts,$inconsistent);
930  
931 10 $inconsistent = _ComputeLA($grammar,$states);
932  
933 10 $conflicts = _SolveConflicts($grammar,$states,$inconsistent);
934 10 _SetDefaults($states);
935  
936 10 $conflicts
937 }
938  
939  
940 3 1;
↑ Summary blib/lib/Parse/Yapp/Driver.pm »