blib/lib/Parse/Yapp/Grammar.pm

filter: Statementstmt 75.2% 152 / 202 Branchbran 67.6% 46 / 68 Conditioncond 83.3% 10 / 12 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 7 totaltotal 72.4% 226 / 312 CC 56 SCAR 25.9
CC
56
Cov
74%
Warnings
56.0
ShowRules
20.0
_ReduceGrammar
13.0
CRAP 13.4
« blib/lib/Parse/Yapp/Driver.pm ↑ Summary YappParse.yp »
1 #
2 # Module Parse::Yapp::Grammar
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::Grammar;
9 3 @ISA=qw( Parse::Yapp::Options );
10  
11 3 require 5.004;
12  
13 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Parse::Yapp::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Parse::Yapp::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 ###############
19 # Constructor #
20 ###############
21 10 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod undocumented
22 10 my($class)=shift;
23 10 my($values);
24  
25 10 my($self)=$class->SUPER::new(@_);
26  
27 10 my($parser)=new Parse::Yapp::Parse;
28  
29 10 ▶ defined($self->Option('input'))
Branch: 1 branch
BranchTrueFalseTotal
defined $self->Option("input") or croak("No input grammar") 10 0 10
30 or croak "No input grammar";
31  
32 10 $values = $parser->Parse($self->Option('input'));
33  
34 10 undef($parser);
35  
36 10 $$self{GRAMMAR}=_ReduceGrammar($values);
37  
38 10 ▶ ref($class)
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 10 10
39 and $class=ref($class);
40  
41 10 bless($self, $class);
42 }
43  
44 ###########
45 # Methods #
46 ###########
47 ##########################
48 # Method To View Grammar #
49 ##########################
50 0 ▶sub ShowRules {
Subroutine ShowRules: not called CC=4 SCAR=30.0
Pod undocumented
51 0 my($self)=shift;
52 0 my($rules)=$$self{GRAMMAR}{RULES};
53 0 my($ruleno)=-1;
54 0 my($text);
55  
56 0 for (@$rules) {
57 0 my($lhs,$rhs)=@$_;
58  
59 0 $text.=++$ruleno.":\t".$lhs." -> ";
60 0 ▶ if(@$rhs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rhs) { } 0 0 0
61 0 ▶ $text.=join(' ',map { $_ eq chr(0) ? '$end' : $_ } @$rhs);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "\000" ? : 0 0 0
62 }
63 else {
64 0 $text.="/* empty */";
65 }
66 0 $text.="\n";
67 }
68 0 $text;
69 }
70  
71 ###########################
72 # Method To View Warnings #
73 ###########################
74 0 ▶sub Warnings {
Subroutine Warnings: not called CC=7 SCAR=40.3
Pod undocumented
75 0 my($self)=shift;
76 0 my($text);
77 0 my($grammar)=$$self{GRAMMAR};
78  
79 exists($$grammar{UUTERM})
80 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $grammar->{"UUTERM"} and do { $text = "Unused terminals:\n\n"; foreach $_ (@{$$grammar{"UUTERM"};}) { $text .= "\t$_->[0], declared line $_->[1]\n"; } ; $text .= "\n" } 0 0 0
81 0 $text="Unused terminals:\n\n";
82 0 for (@{$$grammar{UUTERM}}) {
83 0 $text.="\t$$_[0], declared line $$_[1]\n";
84 }
85 0 $text.="\n";
86 };
87 exists($$grammar{UUNTERM})
88 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $grammar->{"UUNTERM"} and do { $text .= "Useless non-terminals:\n\n"; foreach $_ (@{$$grammar{"UUNTERM"};}) { $text .= "\t$_->[0], declared line $_->[1]\n"; } ; $text .= "\n" } 0 0 0
89 0 $text.="Useless non-terminals:\n\n";
90 0 for (@{$$grammar{UUNTERM}}) {
91 0 $text.="\t$$_[0], declared line $$_[1]\n";
92 }
93 0 $text.="\n";
94 };
95 exists($$grammar{UURULES})
96 0 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $grammar->{"UURULES"} and do { $text .= "Useless rules:\n\n"; foreach $_ (@{$$grammar{"UURULES"};}) { $text .= "\t$_->[0] -> " . join(" ", @{$_->[1];}) . "\n"; } ; $text .= "\n" } 0 0 0
97 0 $text.="Useless rules:\n\n";
98 0 for (@{$$grammar{UURULES}}) {
99 0 $text.="\t$$_[0] -> ".join(' ',@{$$_[1]})."\n";
100 }
101 0 $text.="\n";
102 };
103 0 $text;
104 }
105  
106 ######################################
107 # Method to get summary about parser #
108 ######################################
109 0 ▶sub Summary {
Subroutine Summary: not called CC=1 SCAR=6.9
Pod undocumented
110 0 my($self)=shift;
111 0 my($text);
112  
113 $text ="Number of rules : ".
114 0 scalar(@{$$self{GRAMMAR}{RULES}})."\n";
115 $text.="Number of terminals : ".
116 0 scalar(keys(%{$$self{GRAMMAR}{TERM}}))."\n";
117 $text.="Number of non-terminals : ".
118 0 scalar(keys(%{$$self{GRAMMAR}{NTERM}}))."\n";
119 0 $text;
120 }
121  
122 ###############################
123 # Method to Ouput rules table #
124 ###############################
125 9 ▶sub RulesTable {
Subroutine RulesTable: called CC=4 SCAR=13.9
Pod undocumented
126 9 my($self)=shift;
127 9 my($inputfile)=$self->Option('inputfile');
128 9 my($linenums)=$self->Option('linenumbers');
129 9 my($rules)=$$self{GRAMMAR}{RULES};
130 9 my($ruleno);
131 my($text);
132  
133 9 ▶ defined($inputfile)
Branch: 1 branch
BranchTrueFalseTotal
defined $inputfile or $inputfile = "unkown" 0 9 9
134 or $inputfile = 'unkown';
135  
136 9 $text="[\n\t";
137  
138 $text.=join(",\n\t",
139 map {
140 9 my($lhs,$rhs,$code)=@$_[0,1,3];
141 63 my($len)=scalar(@$rhs);
142 63 my($text);
143  
144 63 $text.="[#Rule ".$ruleno++."\n\t\t '$lhs', $len,";
145 63 ▶ if($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 32 31 63
146 32 ▶ $text.= "\nsub".
Branch: 1 branch
BranchTrueFalseTotal
$linenums ? : 32 0 32
147 ( $linenums
148 ? qq(\n#line $$code[1] "$inputfile"\n)
149 : " ").
150 "{$$code[0]}";
151 }
152 else {
153 31 $text.=' undef';
154 }
155 63 $text.="\n\t]";
156  
157 63 $text;
158 } @$rules);
159  
160 9 $text.="\n]";
161  
162 9 $text;
163 }
164  
165 ################################
166 # Methods to get HEAD and TAIL #
167 ################################
168 9 ▶sub Head {
Subroutine Head: called CC=5 SCAR=16.2
Pod undocumented
169 9 my($self)=shift;
170 9 my($inputfile)=$self->Option('inputfile');
171 9 my($linenums)=$self->Option('linenumbers');
172 9 my($text);
173  
174 9 ▶ $$self{GRAMMAR}{HEAD}[0]
Branch: 1 branch
BranchTrueFalseTotal
$self->{"GRAMMAR"}{"HEAD"}[0] or return "" 8 1 9
175 or return '';
176  
177 8 ▶ defined($inputfile)
Branch: 1 branch
BranchTrueFalseTotal
defined $inputfile or $inputfile = "unkown" 0 8 8
178 or $inputfile = 'unkown';
179  
180 8 for (@{$$self{GRAMMAR}{HEAD}}) {
181 8 ▶ $linenums
Branch: 1 branch
BranchTrueFalseTotal
$linenums and $text .= qq[#line $_->[1] "$inputfile"\n] 8 0 8
182 and $text.=qq(#line $$_[1] "$inputfile"\n);
183 8 $text.=$$_[0];
184 }
185 $text
186 8 }
187  
188 9 ▶sub Tail {
Subroutine Tail: called CC=4 SCAR=14.2
Pod undocumented
189 9 my($self)=shift;
190 9 my($inputfile)=$self->Option('inputfile');
191 9 my($linenums)=$self->Option('linenumbers');
192 9 my($text);
193  
194 9 ▶ $$self{GRAMMAR}{TAIL}[0]
Branch: 1 branch
BranchTrueFalseTotal
$self->{"GRAMMAR"}{"TAIL"}[0] or return "" 9 0 9
195 or return '';
196  
197 9 ▶ defined($inputfile)
Branch: 1 branch
BranchTrueFalseTotal
defined $inputfile or $inputfile = "unkown" 0 9 9
198 or $inputfile = 'unkown';
199  
200 9 ▶ $linenums
Branch: 1 branch
BranchTrueFalseTotal
$linenums and $text = qq[#line $self->{"GRAMMAR"}{"TAIL"}[1] "$inputfile"\n] 9 0 9
201 and $text=qq(#line $$self{GRAMMAR}{TAIL}[1] "$inputfile"\n);
202 9 $text.=$$self{GRAMMAR}{TAIL}[0];
203  
204 9 $text
205 }
206  
207  
208 #################
209 # Private Stuff #
210 #################
211  
212 10 ▶sub _UsefulRules {
Subroutine _UsefulRules: called CC=10 SCAR=23.0
213 10 my($rules,$nterm) = @_;
214 10 my($ufrules,$ufnterm);
215 10 my($done);
216  
217 10 $ufrules=pack('b'.@$rules);
218 10 $ufnterm={};
219  
220 10 vec($ufrules,0,1)=1; #start rules IS always useful
221  
222 RULE:
223 10 for (1..$#$rules) { # Ignore start rule
224 878 for my $sym (@{$$rules[$_][1]}) {
225 1084 ▶ exists($$nterm{$sym})
Branch: 1 branch
BranchTrueFalseTotal
exists $nterm->{$sym} and next RULE 709 375 1084
226 and next RULE;
227 }
228 169 vec($ufrules,$_,1)=1;
229 169 ++$$ufnterm{$$rules[$_][0]};
230 }
231  
232 10 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$done or do { do { $done = 1; RULE: foreach $_ (grep {vec($ufrules, $_, 1) == 0;} 1 .. $rules->$#*) { foreach my $sym (@{$$rules[$_][1];}) { exists $nterm->{$sym} and not exists $ufnterm->{$sym} and next RULE; } ; vec($ufrules, $_, 1) = 1; exists $$ufnterm{$rules->[$_][0]} or do { $done = 0; ++$$ufnterm{$rules->[$_][0]} }; } } } 10 17 27
233 27 $done=1;
234  
235 RULE:
236 27 for (grep { vec($ufrules,$_,1) == 0 } 1..$#$rules) {
237 1157 for my $sym (@{$$rules[$_][1]}) {
238 exists($$nterm{$sym})
239 2570 ▶ and not exists($$ufnterm{$sym})
Branch: 1 branch
BranchTrueFalseTotal
exists $nterm->{$sym} and not exists $ufnterm->{$sym} and next RULE 448 2122 2570
Condition100%3/3
exists $nterm->{$sym} and not exists $ufnterm->{$sym}
!ll&&!rl&&r
7451377448
MC/DC100%2/2
exists $nterm->{$sym} and not exists $ufnterm->{$sym}
exists $nterm->{$sym} not exists $ufnterm->{$sym}
Truth table100%3/3
A and not(B)
A= exists $nterm->{$sym}B= not exists $ufnterm->{$sym}
ABresult
0X0
100
111
240 and next RULE;
241 }
242 709 vec($ufrules,$_,1)=1;
243 exists($$ufnterm{$$rules[$_][0]})
244 709 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
exists $$ufnterm{$rules->[$_][0]} or do { $done = 0; ++$$ufnterm{$rules->[$_][0]} } 559 150 709
245 150 $done=0;
246 150 ++$$ufnterm{$$rules[$_][0]};
247 };
248 }
249  
250 }until($done);
251  
252 10 ($ufrules,$ufnterm)
253  
254 }#_UsefulRules
255  
256 10 ▶sub _Reachable {
Subroutine _Reachable: called CC=6 SCAR=17.9
257 10 my($rules,$nterm,$term,$ufrules,$ufnterm)=@_;
258 10 my($reachable);
259 10 my(@fifo)=( 0 );
260  
261 10 $reachable={ '$start' => 1 }; #$start is always reachable
262  
263 10 ▶ while(@fifo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fifo) 885 10 895
264 885 my($ruleno)=shift(@fifo);
265  
266 885 for my $sym (@{$$rules[$ruleno][1]}) {
267  
268 exists($$term{$sym})
269 2029 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $term->{$sym} and do { ++$reachable->{$sym}; next } 784 1245 2029
270 784 ++$$reachable{$sym};
271 784 next;
272 };
273  
274 ( not exists($$ufnterm{$sym})
275 1245 ▶ or exists($$reachable{$sym}) )
Branch: 1 branch
BranchTrueFalseTotal
!exists $ufnterm->{$sym} || exists $reachable->{$sym} and next 985 260 1245
Condition100%3/3
not exists $ufnterm->{$sym} or exists $reachable->{$sym}
l!l&&r!l&&!r
10975260
MC/DC100%2/2
not exists $ufnterm->{$sym} or exists $reachable->{$sym}
not exists $ufnterm->{$sym} exists $reachable->{$sym}
Truth table100%3/3
not(A) or B
A= not exists $ufnterm->{$sym}B= exists $reachable->{$sym}
ABresult
1X1
011
000
276 and next;
277  
278 260 ++$$reachable{$sym};
279 260 push(@fifo, grep { vec($ufrules,$_,1) } @{$$nterm{$sym}});
280 }
281 }
282  
283 $reachable
284  
285 10 }#_Reachable
286  
287 10 ▶sub _SetNullable {
Subroutine _SetNullable: called CC=10 SCAR=23.0
288 10 my($rules,$term,$nullable) = @_;
289 10 my(@nrules);
290 my($done);
291  
292 RULE:
293 10 for (@$rules) {
294 888 my($lhs,$rhs)=@$_;
295  
296 888 ▶ exists($$nullable{$lhs})
Branch: 1 branch
BranchTrueFalseTotal
exists $nullable->{$lhs} and next 89 799 888
297 and next;
298  
299 799 for (@$rhs) {
300 1285 ▶ exists($$term{$_})
Branch: 1 branch
BranchTrueFalseTotal
exists $term->{$_} and next RULE 511 774 1285
301 and next RULE;
302 }
303 288 push(@nrules,[$lhs,$rhs]);
304 }
305  
306 10 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$done or do { do { $done = 1; RULE: foreach $_ (@nrules) { () = @$_; exists $nullable->{$lhs} and next; foreach $_ (@$rhs) { exists $nullable->{$_} or next RULE; } ; $done = 0; ++$nullable->{$lhs}; } } } 10 2 12
307 12 $done=1;
308  
309 RULE:
310 12 for (@nrules) {
311 567 my($lhs,$rhs)=@$_;
312  
313 567 ▶ exists($$nullable{$lhs})
Branch: 1 branch
BranchTrueFalseTotal
exists $nullable->{$lhs} and next 8 559 567
314 and next;
315  
316 559 for (@$rhs) {
317 558 ▶ exists($$nullable{$_})
Branch: 1 branch
BranchTrueFalseTotal
exists $nullable->{$_} or next RULE 5 553 558
318 or next RULE;
319 }
320 6 $done=0;
321 6 ++$$nullable{$lhs};
322 }
323  
324 }until($done);
325 }
326  
327 10 ▶sub _ReduceGrammar {
Subroutine _ReduceGrammar: called CC=12 SCAR=25.6
328 10 my($values)=@_;
329 10 my($ufrules,$ufnterm,$reachable);
330 my($grammar)={ HEAD => $values->{HEAD},
331 TAIL => $values->{TAIL},
332 10 EXPECT => $values->{EXPECT} };
333 10 my($rules,$nterm,$term) = @$values {'RULES', 'NTERM', 'TERM'};
334  
335 10 ($ufrules,$ufnterm) = _UsefulRules($rules,$nterm);
336  
337 exists($$ufnterm{$values->{START}})
338 10 ▶ or die "*Fatal* Start symbol $values->{START} derives nothing, at eof\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $$ufnterm{$values->{"START"}} or die qq[*Fatal* Start symbol $values->{"START"} derives nothing, at eof\n] 10 0 10
339  
340 10 $reachable = _Reachable($rules,$nterm,$term,$ufrules,$ufnterm);
341  
342 10 $$grammar{TERM}{chr(0)}=undef;
343 10 for my $sym (keys %$term) {
344 ( exists($$reachable{$sym})
345 or exists($values->{PREC}{$sym}) )
346 157 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $reachable->{$sym} || exists $values->{"PREC"}{$sym} and do { $grammar->{"TERM"}{$sym} = defined $term->{$sym}[0] ? $term->{$sym} : undef; next } 154 3 157
Condition100%3/3
exists $reachable->{$sym} or exists $values->{"PREC"}{$sym}
l!l&&r!l&&!r
15043
MC/DC100%2/2
exists $reachable->{$sym} or exists $values->{"PREC"}{$sym}
exists $reachable->{$sym} exists $values->{"PREC"}{$sym}
Truth table100%3/3
A or B
A= exists $reachable->{$sym}B= exists $values->{"PREC"}{$sym}
ABresult
1X1
011
000
347 $$grammar{TERM}{$sym}
348 154 ▶ = defined($$term{$sym}[0]) ? $$term{$sym} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $term->{$sym}[0] ? : 74 80 154
349 154 next;
350 };
351 3 push(@{$$grammar{UUTERM}},[ $sym, $values->{SYMS}{$sym} ]);
352 }
353  
354 10 $$grammar{NTERM}{'$start'}=[];
355 10 for my $sym (keys %$nterm) {
356 exists($$reachable{$sym})
357 260 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $reachable->{$sym} and do { exists $values->{"NULL"}{$sym} and ++$grammar->{"NULLABLE"}{$sym}; $grammar->{"NTERM"}{$sym} = []; next } 260 0 260
358 exists($values->{NULL}{$sym})
359 260 ▶ and ++$$grammar{NULLABLE}{$sym};
Branch: 1 branch
BranchTrueFalseTotal
exists $values->{"NULL"}{$sym} and ++$grammar->{"NULLABLE"}{$sym} 41 219 260
360 260 $$grammar{NTERM}{$sym}=[];
361 260 next;
362 };
363 0 push(@{$$grammar{UUNTERM}},[ $sym, $values->{SYMS}{$sym} ]);
364 }
365  
366 10 for my $ruleno (0..$#$rules) {
367 vec($ufrules,$ruleno,1)
368 and exists($$grammar{NTERM}{$$rules[$ruleno][0]})
369 888 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
vec $ufrules, $ruleno, 1 and exists $grammar->{"NTERM"}{$rules->[$ruleno][0]} and do { push @{$$grammar{"RULES"};}, $rules->[$ruleno]; push @{$grammar->{"NTERM"}{$rules->[$ruleno][0]};}, $#{$$grammar{"RULES"};}; next } 888 0 888
Condition33%1/3
vec $ufrules, $ruleno, 1 and exists $grammar->{"NTERM"}{$rules->[$ruleno][0]}
!ll&&!rl&&r
00888
MC/DC0%0/2
vec $ufrules, $ruleno, 1 and exists $grammar->{"NTERM"}{$rules->[$ruleno][0]}
vec $ufrules, $ruleno, 1 exists $grammar->{"NTERM"}{$rules->[$ruleno][0]}
Truth table33%1/3
A and B
A= vec $ufrules, $ruleno, 1B= exists $grammar->{"NTERM"}{$rules->[$ruleno][0]}
ABresult
0X0
100
111
370 888 push(@{$$grammar{RULES}},$$rules[$ruleno]);
371 888 push(@{$$grammar{NTERM}{$$rules[$ruleno][0]}},$#{$$grammar{RULES}});
372 888 next;
373 };
374 0 push(@{$$grammar{UURULES}},[ @{$$rules[$ruleno]}[0,1] ]);
375 }
376  
377 10 _SetNullable(@$grammar{'RULES', 'TERM', 'NULLABLE'});
378  
379 10 $grammar;
380 }#_ReduceGrammar
381  
382 3 1;
« blib/lib/Parse/Yapp/Driver.pm ↑ Summary YappParse.yp »