Eyapp/Grammar.pm

filter: Statementstmt 68.7% 244 / 355 Branchbran 56.5% 78 / 138 Conditioncond 45.1% 14 / 31 MC/DCmcdc 21.0% 4 / 19 Subroutinesub 76.4% 26 / 34 Podpod 0.0% 0 / 24 totaltotal 60.9% 366 / 601 CC 106 SCAR 34.6
CC
106
Cov
64%
give_token_name
156.0
give_rhs_name
132.0
Warnings
23.6
CRAP 31.8
« Eyapp/Base.pm ↑ Summary Eyapp/Unify.pm »
1 #
2 # Module Parse::Eyapp::Grammar
3 #
4 # Copyright © 2006, 2007, 2008, 2009, 2010, 2011, 2012 Casiano Rodriguez-Leon.
5 # Copyright © 2017 William N. Braswell, Jr.
6 # All Rights Reserved.
7 #
8 # Parse::Yapp is Copyright © 1998, 1999, 2000, 2001, Francois Desarmenien.
9 # Parse::Yapp is Copyright © 2017 William N. Braswell, Jr.
10 # All Rights Reserved.
11 package Parse::Eyapp::Grammar;
12 61 @ISA=qw( Parse::Eyapp::Options );
13  
14 61 require 5.004;
15  
16 61 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 61 ▶use Parse::Eyapp::Options;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 61 ▶use Parse::Eyapp::Parse;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 61 ▶use Scalar::Util qw{reftype};
Subroutine BEGIN: called CC=1 SCAR=0.0
21 61 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 ###############
24 # Constructor #
25 ###############
26 54 ▶sub new {
Subroutine new: called CC=7 SCAR=22.2
Pod undocumented
27 54 my($class)=shift;
28 54 my($values);
29  
30 54 my($self)=$class->SUPER::new(@_);
31  
32 54 my($parser)=new Parse::Eyapp::Parse;
33  
34 54 ▶ defined($self->Option('input'))
Branch: 1 branch
BranchTrueFalseTotal
defined $self->Option("input") or croak("No input grammar") 54 0 54
35 or croak "No input grammar";
36  
37 54 $values = $parser->Parse($self->Option('input'), # 1 input
38 $self->Option('firstline'), # 2 Line where the grammar source starts
39 $self->Option('inputfile'), # 3 The file or program containing the grammar
40 $self->Option('tree'), # 4 %tree activated
41 $self->Option('nocompact'), # 5 %nocompact
42 $self->Option('lexerisdefined'), # 6 lexer is defined
43 $self->Option('prefix'), # 7 accept prefix
44 $self->Option('start'), # 8 specify start symbol
45 #$self->Option('prefixname'), # yyprefix
46 #$self->Option('buildingtree') # If building AST
47 );
48  
49 54 undef($parser);
50  
51 54 $$self{GRAMMAR}=_ReduceGrammar($values);
52  
53 54 ▶ ref($class)
Branch: 1 branch
BranchTrueFalseTotal
ref $class and $class = ref $class 0 54 54
54 and $class=ref($class);
55  
56 54 bless($self, $class);
57  
58 54 my $ns = $self->{GRAMMAR}{NAMINGSCHEME} ;
59 54 ▶ if ($ns && reftype($ns) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ns and builtin::reftype($ns) eq "ARRAY") 0 54 54
Condition33%1/3
$ns and builtin::reftype($ns) eq "ARRAY"
!ll&&!rl&&r
5400
MC/DC0%0/2
$ns and builtin::reftype($ns) eq "ARRAY"
$ns builtin::reftype($ns) eq "ARRAY"
Truth table33%1/3
A and B
A= $nsB= builtin::reftype($ns) eq "ARRAY"
ABresult
0X0
100
111
60 0 $ns = eval "sub { $ns->[0]; }; ";
61 0 ▶ warn "Error in \%namingscheme directive $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
62 0 $ns = $ns->($self);
63 }
64 54 ▶ $ns ||= \&give_default_name;
Condition50%1/2
$ns ||= \&Parse::Eyapp::Grammar::give_default_name
l!l
054
MC/DC0%0/1
$ns ||= \&Parse::Eyapp::Grammar::give_default_name
$ns
Truth table50%1/2
A
A= $ns
Aresult
11
00
65 54 $self->{GRAMMAR}{NAMINGSCHEME} = $ns; # added to allow programmable production naming schemes (%name)
66  
67 54 $self;
68 }
69  
70 ###########
71 # Methods #
72 ###########
73 ##########################
74 # Method To View Grammar #
75 ##########################
76 0 ▶sub ShowRules {
Subroutine ShowRules: not called CC=4 SCAR=30.0
Pod undocumented
77 0 my($self)=shift;
78 0 my($rules)=$$self{GRAMMAR}{RULES};
79 0 my($ruleno)=-1;
80 0 my($text);
81  
82 0 for (@$rules) {
83 0 my($lhs,$rhs)=@$_;
84  
85 0 $text.=++$ruleno.":\t".$lhs." -> ";
86 0 ▶ if(@$rhs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rhs) { } 0 0 0
87 0 ▶ $text.=join(' ',map { $_ eq chr(0) ? '$end' : $_ } @$rhs);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "\000" ? : 0 0 0
88 }
89 else {
90 0 $text.="/* empty */";
91 }
92 0 $text.="\n";
93 }
94 0 $text;
95 }
96  
97 376 ▶sub give_default_name {
Subroutine give_default_name: called CC=1 SCAR=0.0
Pod undocumented
98 376 my ($self, $index, $lhs) = @_;
99  
100 376 my $name = "$lhs"."_$index";
101 376 return $name;
102 }
103  
104 0 ▶sub give_lhs_name {
Subroutine give_lhs_name: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self, $index, $lhs, $rhs) = @_;
106  
107 0 my $name = $lhs;
108 0 return $name;
109 }
110  
111 0 ▶sub give_token_name {
Subroutine give_token_name: not called CC=12 SCAR=50.5
Pod undocumented
112 0 my ($self, $index, $lhs, $rhs) = @_;
113  
114 0 my @rhs = @$rhs;
115 0 $rhs = '';
116  
117 0 ▶ unless (@rhs) { # Empty RHS
Branch: 1 branch
BranchTrueFalseTotal
unless (@rhs) 0 0 0
118 0 return $lhs.'_is_empty';
119 }
120  
121 0 ▶ my $names = $self->{GRAMMAR}{TOKENNAMES} || {};
Condition0%0/2
$self->{'GRAMMAR'}{'TOKENNAMES'} || {}
l!l
00
MC/DC0%0/1
$self->{'GRAMMAR'}{'TOKENNAMES'} || {}
$self->{'GRAMMAR'}{'TOKENNAMES'}
Truth table0%0/2
A
A= $self->{'GRAMMAR'}{'TOKENNAMES'}
Aresult
11
00
122 0 for (@rhs) {
123 0 ▶ if ($self->is_token($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_token($_)) 0 0 0
124 0 s/^'(.*)'$/$1/;
125 0 ▶ my $name = $names->{$_} || '';
Condition0%0/2
$names->{$_} || ''
l!l
00
MC/DC0%0/1
$names->{$_} || ''
$names->{$_}
Truth table0%0/2
A
A= $names->{$_}
Aresult
11
00
126 0 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
127 0 ▶ $name = $_ if /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\w+$/ 0 0 0
128 }
129 0 ▶ $rhs .= "_$name" if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
130 }
131 }
132  
133 0 ▶ unless ($rhs) { # no 'word' tokens in the RHS
Branch: 1 branch
BranchTrueFalseTotal
unless ($rhs) 0 0 0
134 0 for (@rhs) {
135 0 ▶ $rhs .= "_$_" if /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\w+$/ 0 0 0
136 }
137 }
138  
139 # check if another production with such name exists?
140 0 my $name = $lhs.'_is'.$rhs;
141 0 return $name;
142 }
143  
144 sub camelize
145 0 ▶{
Subroutine camelize: not called CC=1 SCAR=6.9
Pod undocumented
146 0 my $s = shift;
147  
148 0 my @a = split(/(?<=[A-Za-z])_(?=[A-Za-z])|\b/, $s);
149 0 my $a = shift @a;
150 0 @a = map { ucfirst $_ } @a;
151 0 join('', ($a, @a));
152 }
153  
154 0 ▶sub give_rhs_name {
Subroutine give_rhs_name: not called CC=11 SCAR=48.8
Pod undocumented
155 0 my ($self, $index, $lhs, $rhs) = @_;
156  
157 0 my @rhs = @$rhs;
158 0 $rhs = '';
159  
160 0 ▶ unless (@rhs) { # Empty RHS
Branch: 1 branch
BranchTrueFalseTotal
unless (@rhs) 0 0 0
161 0 return camelize($lhs).'_is_empty';
162 }
163  
164 0 ▶ my $names = $self->{GRAMMAR}{TOKENNAMES} || {};
Condition0%0/2
$self->{'GRAMMAR'}{'TOKENNAMES'} || {}
l!l
00
MC/DC0%0/1
$self->{'GRAMMAR'}{'TOKENNAMES'} || {}
$self->{'GRAMMAR'}{'TOKENNAMES'}
Truth table0%0/2
A
A= $self->{'GRAMMAR'}{'TOKENNAMES'}
Aresult
11
00
165 0 for (@rhs) {
166 0 ▶ if ($self->is_token($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_token($_)) { } 0 0 0
167 # remove apostrophes
168 0 s/^'(.*)'$/$1/;
169  
170 # explicit name given ?
171 0 ▶ my $name = $names->{$_} || '';
Condition0%0/2
$names->{$_} || ''
l!l
00
MC/DC0%0/1
$names->{$_} || ''
$names->{$_}
Truth table0%0/2
A
A= $names->{$_}
Aresult
11
00
172  
173 # no name was given, use symbol if is an ID
174 0 ▶ unless ($name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 0 0
175 0 ▶ $name = $_ if /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\w+$/ 0 0 0
176 }
177 0 ▶ $rhs .= "_$name" if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
178 }
179 else { # syntactic variable
180 0 ▶ next if exists $self->{GRAMMAR}{CONFLICTHANDLERS}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"GRAMMAR"}{"CONFLICTHANDLERS"}{$_} 0 0 0
181 0 ▶ $rhs .= '_'.camelize($_) if /^\w*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\w*$/ 0 0 0
182 }
183 }
184  
185 # check if another production with such name exists?
186 0 my $name = camelize($lhs).'_is'.$rhs;
187 0 return $name;
188 }
189  
190 1382 ▶sub classname {
Subroutine classname: called CC=10 SCAR=23.2
Pod undocumented
191 1382 my ($self, $name, $index, $lhs, $rhs) = @_;
192  
193 1382 $name = $name->[0];
194  
195 1382 ▶ unless (defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 556 826 1382
196 556 ▶ if ($lhs =~ /\$start/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($lhs =~ /\$start/) { } 108 448 556
elsif ($lhs =~ /\@(\d+)-(\d+)/) { } 16 432 448
elsif ($lhs =~ /PAREN-(\d+)/) { } 16 416 432
elsif ($lhs =~ /STAR-(\d+)/) { } 16 400 416
elsif ($lhs =~ /PLUS-(\d+)/) { } 24 376 400
elsif ($lhs =~ /OPTIONAL-(\d+)/) { } 0 376 376
197 108 $name = "_SUPERSTART"
198 }
199 elsif ($lhs =~ /\@(\d+)-(\d+)/) {
200 16 $name = "_CODE"
201 }
202 elsif ($lhs =~ /PAREN-(\d+)/) {
203 16 $name = "_PAREN"
204 }
205 elsif ($lhs =~ /STAR-(\d+)/) {
206 16 $name = "_STAR_LIST"
207 }
208 elsif ($lhs =~ /PLUS-(\d+)/) {
209 24 $name = "_PLUS_LIST"
210 }
211 elsif ($lhs =~ /OPTIONAL-(\d+)/) {
212 0 $name = "_OPTIONAL"
213 }
214 }
215  
216 1382 my $naming_scheme = $self->{GRAMMAR}{NAMINGSCHEME};
217 1382 ▶ if (!$name) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $name) { } 376 1006 1382
elsif ($name =~ /^:/) { } 0 1006 1006
218 376 $name = $naming_scheme->($self, $index, $lhs, $rhs);
219 }
220 elsif ($name =~ /^:/) { # it is a label only
221 0 $name = $naming_scheme->($self, $index, $lhs, $rhs).$name;
222 }
223  
224 1382 return $name;
225 }
226  
227 # Added by Casiano
228 #####################################
229 # Method To Return the Grammar Rules#
230 #####################################
231 54 ▶sub Rules { # TODO: find proper names
Subroutine Rules: called CC=7 SCAR=19.5
Pod undocumented
232 54 my($self)=shift;
233 54 my($rules)=$$self{GRAMMAR}{RULES};
234 54 my($text) = "[#[productionNameAndLabel => lhs, [ rhs], bypass]]\n";
235 54 my $packages = q{'TERMINAL', '_OPTIONAL', '_STAR_LIST', '_PLUS_LIST', };
236  
237 54 my $index = 0;
238 54 my $label = "{\n"; # To huild a reverse map label => production number
239 54 for (@$rules) {
240 691 my($lhs,$rhs,$prec,$name)=@$_;
241  
242 691 my $bypass = $name->[2];
243 691 ▶ $bypass = $self->Bypass unless defined($bypass);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bypass 334 357 691
244  
245 691 ▶ $label .= " '$1' => $index,\n" if defined($name->[0]) and $name->[0] =~ /(:.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name->[0] and $name->[0] =~ /(:.*)/ 0 691 691
Condition66%2/3
defined $name->[0] and $name->[0] =~ /(:.*)/
!ll&&!rl&&r
2784130
MC/DC0%0/2
defined $name->[0] and $name->[0] =~ /(:.*)/
defined $name->[0] $name->[0] =~ /(:.*)/
Truth table66%2/3
A and B
A= defined $name->[0]B= $name->[0] =~ /(:.*)/
ABresult
0X0
100
111
246  
247 # find an acceptable perl identifier as name
248 691 $name = $self->classname($name, $index, $lhs, $rhs);
249 691 $label .= " '$name' => $index,\n";
250  
251 691 $packages .= "\n".(" "x9)."'$name', ";
252  
253 691 $text.= " [ '$name' => '$lhs', [ ";
254 691 ▶ $text.=join(', ',map { $_ eq chr(0) ? "'\$end'" : $_ =~ m{^'} ? $_ : "'$_'" } @$rhs);
Branch: 2 branches
BranchTrueFalseTotal
$_ eq "\000" ? : 54 1429 1483
$_ =~ /^'/ ? : 465 964 1429
255 691 $text.=" ], $bypass ],\n";
256 691 $index++;
257 }
258 54 $text .= ']';
259 54 $label .= '}';
260 54 return ($text, $packages, $label);
261 }
262  
263 # Added by Casiano
264 #####################################
265 # Method To Return the Grammar Terms#
266 #####################################
267 54 ▶sub Terms {
Subroutine Terms: called CC=2 SCAR=6.9
Pod undocumented
268 54 my($self)=shift;
269 54 my(@terms)= sort(keys(%{$$self{GRAMMAR}{TERM}}));
270 54 my %semantic = %{$self->{GRAMMAR}{SEMANTIC}};
271  
272 54 my $text = "{ ";
273 $text .= join(",\n\t",
274 # Warning! bug. Before: map { $_ eq chr(0) ? "'\$end' => 0" : "$_ => $semantic{$_}"} @terms);
275 54 ▶ map { $_ eq chr(0) ? "'' => { ISSEMANTIC => 0 }" : "$_ => { ISSEMANTIC => $semantic{$_} }"} @terms);
Branch: 1 branch
BranchTrueFalseTotal
$_ eq "\000" ? : 54 516 570
276 54 $text .= ",\n\terror => { ISSEMANTIC => 0 },\n}";
277 }
278  
279 54 ▶sub conflictHandlers {
Subroutine conflictHandlers: called CC=1 SCAR=0.0
Pod undocumented
280 54 my $self = shift;
281  
282 54 my $t = Dumper $self->{GRAMMAR}{CONFLICTHANDLERS};
283 54 $t =~ s/^\$VAR\d*\s*=\s*//;
284 54 $t =~s/;$//;
285 54 $t =~s/\\'//g; # quotes inside quotes
286 54 $t;
287 }
288  
289  
290 # produces the text mapping states to conflicthandlers
291 54 ▶sub stateConflict {
Subroutine stateConflict: called CC=6 SCAR=27.3
Pod undocumented
292 54 my $self = shift;
293  
294 54 my $c = $self->{GRAMMAR}{CONFLICTHANDLERS};
295 54 my %stateConflict;
296  
297 54 my %t = ();
298 54 for my $cn (keys %$c) {
299 0 my $ce = $c->{$cn};
300 0 my $codeh = $ce->{codeh};
301 0 $codeh = "sub { $codeh }";
302 0 ▶ my @s = defined($ce->{states}) ? @{$ce->{states}} : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $ce->{'states'} ? : 0 0 0
303 0 for my $s (@s) {
304 0 my ($sn) = keys %$s;
305 #my ($tokens) = values %$s;
306 #$tokens = join ',', @$tokens;
307 0 ▶ $t{$sn} = '' unless defined($t{$sn});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $t{$sn} 0 0 0
308 0 $t{$sn} .= << "NEWSTATECONFLICTENTRY";
309 {
310 name => '$cn',
311 codeh => $codeh,
312 },
313 NEWSTATECONFLICTENTRY
314 } #for states
315 } #for conflict names
316
317 54 my $t = '{ ';
318 54 for my $s (keys %t) {
319 0 $t .= "$s => [ $t{$s} ],";
320 }
321 54 $t .= ' }';
322 }
323  
324 #####################################
325 # Method To Return the Bypass Option#
326 #####################################
327 388 ▶sub Bypass {
Subroutine Bypass: called CC=1 SCAR=0.0
Pod undocumented
328 388 my($self)=shift;
329
330 return $$self{GRAMMAR}{BYPASS}
331 388 }
332  
333 #####################################
334 # Method To Return the Prefix Option#
335 #####################################
336 54 ▶sub Prefix {
Subroutine Prefix: called CC=1 SCAR=0.0
Pod undocumented
337 54 my($self)=shift;
338
339 return $$self{GRAMMAR}{PREFIX}
340 54 }
341  
342  
343 54 ▶sub Buildingtree {
Subroutine Buildingtree: called CC=1 SCAR=0.0
Pod undocumented
344 54 my($self)=shift;
345
346 return $$self{GRAMMAR}{BUILDINGTREE}
347 54 }
348  
349 54 ▶sub Prompt {
Subroutine Prompt: called CC=2 SCAR=7.1
Pod undocumented
350 54 my $self = shift;
351  
352 54 ▶ return "our \$PROMPT = $$self{GRAMMAR}{INCREMENTAL};\n" if defined($$self{GRAMMAR}{INCREMENTAL});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"GRAMMAR"}{"INCREMENTAL"} 0 54 54
353 54 return '';
354 }
355  
356 0 ▶sub is_token {
Subroutine is_token: not called CC=1 SCAR=6.9
Pod undocumented
357 0 my($self)=shift;
358  
359 0 exists($self->{GRAMMAR}{TERM}{$_[0]})
360 }
361  
362 #####################################
363 # Method To Return the ACCESSORS
364 #####################################
365 54 ▶sub Accessors {
Subroutine Accessors: called CC=1 SCAR=0.0
Pod undocumented
366 54 my($self)=shift;
367
368 return $$self{GRAMMAR}{ACCESSORS}
369 54 }
370  
371 ###########################
372 # Method To View Warnings #
373 ###########################
374 4 ▶sub Warnings {
Subroutine Warnings: called CC=8 SCAR=31.6
Pod undocumented
375 4 my($self)=shift;
376  
377 4 ▶ return '' if $self->Option('start');
Branch: 1 branch
BranchTrueFalseTotal
if $self->Option("start") 0 4 4
378  
379 4 my($text) = '';
380 4 my($grammar)=$$self{GRAMMAR};
381  
382 exists($$grammar{UUTERM})
383 4 ▶ 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 4 4
384 0 $text="Unused terminals:\n\n";
385 0 for (@{$$grammar{UUTERM}}) {
386 0 $text.="\t$$_[0], declared line $$_[1]\n";
387 }
388 0 $text.="\n";
389 };
390 exists($$grammar{UUNTERM})
391 4 ▶ 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 4 4
392 0 $text.="Useless non-terminals:\n\n";
393 0 for (@{$$grammar{UUNTERM}}) {
394 0 $text.="\t$$_[0], declared line $$_[1]\n";
395 }
396 0 $text.="\n";
397 };
398 exists($$grammar{UURULES})
399 4 ▶ 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 4 4
400 0 $text.="Useless rules:\n\n";
401 0 for (@{$$grammar{UURULES}}) {
402 0 $text.="\t$$_[0] -> ".join(' ',@{$$_[1]})."\n";
403 }
404 0 $text.="\n";
405 };
406 4 $text;
407 }
408  
409 ######################################
410 # Method to get summary about parser #
411 ######################################
412 0 ▶sub Summary {
Subroutine Summary: not called CC=1 SCAR=6.9
Pod undocumented
413 0 my($self)=shift;
414 0 my($text);
415  
416 $text ="Number of rules : ".
417 0 scalar(@{$$self{GRAMMAR}{RULES}})."\n";
418 $text.="Number of terminals : ".
419 0 scalar(keys(%{$$self{GRAMMAR}{TERM}}))."\n";
420 $text.="Number of non-terminals : ".
421 0 scalar(keys(%{$$self{GRAMMAR}{NTERM}}))."\n";
422 0 $text;
423 }
424  
425 ###############################
426 # Method to Ouput rules table #
427 ###############################
428 54 ▶sub RulesTable {
Subroutine RulesTable: called CC=4 SCAR=13.9
Pod undocumented
429 54 my($self)=shift;
430 54 my($inputfile)=$self->Option('inputfile');
431 54 my($linenums)=$self->Option('linenumbers');
432 54 my($rules)=$$self{GRAMMAR}{RULES};
433 54 my $ruleno = 0;
434 54 my($text);
435  
436 54 ▶ defined($inputfile)
Branch: 1 branch
BranchTrueFalseTotal
defined $inputfile or $inputfile = "unknown" 54 0 54
437 or $inputfile = 'unknown';
438  
439 54 $text="[\n\t";
440  
441 $text.=join(",\n\t",
442 map {
443 54 my($lhs,$rhs,$rname,$code)=@$_[0,1,3,4];
444 691 my($len)=scalar(@$rhs);
445  
446 691 my($text);
447  
448 691 $rname = $self->classname($rname, $ruleno, $lhs, $rhs);
449  
450 691 $ruleno++;
451 691 $text.="[#Rule $rname\n\t\t '$lhs', $len,";
452 691 ▶ if($code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code) { } 635 56 691
453 635 ▶ $text.= "\nsub {".
Branch: 1 branch
BranchTrueFalseTotal
$linenums ? : 630 5 635
454 ( $linenums
455 ? qq(\n#line $$code[1] "$inputfile"\n)
456 : " ").
457 "$$code[0]}";
458 }
459 else {
460 56 $text.=' undef';
461 }
462 691 $text.="\n$Parse::Eyapp::Output::pattern\n\t]";
463  
464 691 $text;
465 } @$rules);
466  
467 54 $text.="\n]";
468  
469 54 $text;
470 }
471  
472 ################################
473 # Methods to get HEAD and TAIL #
474 ################################
475 54 ▶sub Head {
Subroutine Head: called CC=5 SCAR=16.2
Pod undocumented
476 54 my($self)=shift;
477 54 my($inputfile)=$self->Option('inputfile');
478 54 my($linenums)=$self->Option('linenumbers');
479 54 my($text);
480  
481 54 ▶ $$self{GRAMMAR}{HEAD}[0]
Branch: 1 branch
BranchTrueFalseTotal
$self->{"GRAMMAR"}{"HEAD"}[0] or return "" 34 20 54
482 or return '';
483  
484 34 ▶ defined($inputfile)
Branch: 1 branch
BranchTrueFalseTotal
defined $inputfile or $inputfile = "unkown" 34 0 34
485 or $inputfile = 'unkown';
486  
487 34 for (@{$$self{GRAMMAR}{HEAD}}) {
488 34 ▶ $linenums
Branch: 1 branch
BranchTrueFalseTotal
$linenums and $text .= qq[#line $_->[1] "$inputfile"\n] 34 0 34
489 and $text.=qq(#line $$_[1] "$inputfile"\n);
490 34 $text.=$$_[0];
491 }
492 $text
493 34 }
494  
495 54 ▶sub Tail {
Subroutine Tail: called CC=5 SCAR=16.6
Pod undocumented
496 54 my($self)=shift;
497 54 my($inputfile)=$self->Option('inputfile');
498 54 my($linenums)=$self->Option('linenumbers');
499 54 my($text);
500  
501 ((reftype $$self{GRAMMAR}{TAIL} eq 'ARRAY') and
502 54 ▶ $$self{GRAMMAR}{TAIL}[0])
Branch: 1 branch
BranchTrueFalseTotal
builtin::reftype($self->{"GRAMMAR"}{"TAIL"}) eq "ARRAY" and $self->{"GRAMMAR"}{"TAIL"}[0] or return "" 54 0 54
Condition33%1/3
builtin::reftype($self->{"GRAMMAR"}{"TAIL"}) eq "ARRAY" and $self->{"GRAMMAR"}{"TAIL"}[0]
!ll&&!rl&&r
0054
MC/DC0%0/2
builtin::reftype($self->{"GRAMMAR"}{"TAIL"}) eq "ARRAY" and $self->{"GRAMMAR"}{"TAIL"}[0]
builtin::reftype($self->{"GRAMMAR"}{"TAIL"}) eq "ARRAY" $self->{"GRAMMAR"}{"TAIL"}[0]
Truth table33%1/3
A and B
A= builtin::reftype($self->{"GRAMMAR"}{"TAIL"}) eq "ARRAY"B= $self->{"GRAMMAR"}{"TAIL"}[0]
ABresult
0X0
100
111
503 or return '';
504  
505 54 ▶ defined($inputfile)
Branch: 1 branch
BranchTrueFalseTotal
defined $inputfile or $inputfile = "unkown" 54 0 54
506 or $inputfile = 'unkown';
507  
508 54 ▶ $linenums
Branch: 1 branch
BranchTrueFalseTotal
$linenums and $text = qq[#line $self->{"GRAMMAR"}{"TAIL"}[1] "$inputfile"\n] 53 1 54
509 and $text=qq(#line $$self{GRAMMAR}{TAIL}[1] "$inputfile"\n);
510 54 $text.=$$self{GRAMMAR}{TAIL}[0];
511  
512 54 $text
513 }
514  
515  
516 #################
517 # Private Stuff #
518 #################
519  
520 54 ▶sub _UsefulRules {
Subroutine _UsefulRules: called CC=10 SCAR=23.0
521 54 my($rules,$nterm) = @_;
522 54 my($ufrules,$ufnterm);
523 54 my($done);
524  
525 54 $ufrules=pack('b'.@$rules);
526 54 $ufnterm={};
527  
528 54 vec($ufrules,0,1)=1; #start rules IS always useful
529  
530 RULE:
531 54 for (1..$#$rules) { # Ignore start rule
532 637 for my $sym (@{$$rules[$_][1]}) {
533 803 ▶ exists($$nterm{$sym})
Branch: 1 branch
BranchTrueFalseTotal
exists $nterm->{$sym} and next RULE 482 321 803
534 and next RULE;
535 }
536 155 vec($ufrules,$_,1)=1;
537 155 ++$$ufnterm{$$rules[$_][0]};
538 }
539  
540 54 ▶ 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]} }; } } } 54 60 114
541 114 $done=1;
542  
543 RULE:
544 114 for (grep { vec($ufrules,$_,1) == 0 } 1..$#$rules) {
545 632 for my $sym (@{$$rules[$_][1]}) {
546 exists($$nterm{$sym})
547 1432 ▶ and not exists($$ufnterm{$sym})
Branch: 1 branch
BranchTrueFalseTotal
exists $nterm->{$sym} and not exists $ufnterm->{$sym} and next RULE 150 1282 1432
Condition100%3/3
exists $nterm->{$sym} and not exists $ufnterm->{$sym}
!ll&&!rl&&r
502780150
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
548 and next RULE;
549 }
550 482 vec($ufrules,$_,1)=1;
551 exists($$ufnterm{$$rules[$_][0]})
552 482 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
exists $$ufnterm{$rules->[$_][0]} or do { $done = 0; ++$$ufnterm{$rules->[$_][0]} } 373 109 482
553 109 $done=0;
554 109 ++$$ufnterm{$$rules[$_][0]};
555 };
556 }
557  
558 }until($done);
559  
560 54 ($ufrules,$ufnterm)
561  
562 }#_UsefulRules
563  
564 54 ▶sub _Reachable {
Subroutine _Reachable: called CC=6 SCAR=17.9
565 54 my($rules,$nterm,$term,$ufrules,$ufnterm)=@_;
566 54 my($reachable);
567 54 my(@fifo)=( 0 );
568  
569 54 $reachable={ '$start' => 1 }; #$start is always reachable
570  
571 54 ▶ while(@fifo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fifo) 683 54 737
572 683 my($ruleno)=shift(@fifo);
573  
574 683 for my $sym (@{$$rules[$ruleno][1]}) {
575  
576 exists($$term{$sym})
577 1483 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $term->{$sym} and do { ++$reachable->{$sym}; next } 604 879 1483
578 604 ++$$reachable{$sym};
579 604 next;
580 };
581  
582 ( not exists($$ufnterm{$sym})
583 879 ▶ or exists($$reachable{$sym}) )
Branch: 1 branch
BranchTrueFalseTotal
!exists $ufnterm->{$sym} || exists $reachable->{$sym} and next 656 223 879
Condition100%3/3
not exists $ufnterm->{$sym} or exists $reachable->{$sym}
l!l&&r!l&&!r
54602223
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
584 and next;
585  
586 223 ++$$reachable{$sym};
587 223 push(@fifo, grep { vec($ufrules,$_,1) } @{$$nterm{$sym}});
588 }
589 }
590  
591 $reachable
592  
593 54 }#_Reachable
594  
595 54 ▶sub _SetNullable {
Subroutine _SetNullable: called CC=10 SCAR=23.0
596 54 my($rules,$term,$nullable) = @_;
597 54 my(@nrules);
598 my($done);
599  
600 RULE:
601 54 for (@$rules) {
602 691 my($lhs,$rhs)=@$_;
603  
604 691 ▶ exists($$nullable{$lhs})
Branch: 1 branch
BranchTrueFalseTotal
exists $nullable->{$lhs} and next 32 659 691
605 and next;
606  
607 659 for (@$rhs) {
608 976 ▶ exists($$term{$_})
Branch: 1 branch
BranchTrueFalseTotal
exists $term->{$_} and next RULE 531 445 976
609 and next RULE;
610 }
611 128 push(@nrules,[$lhs,$rhs]);
612 }
613  
614 54 ▶ 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}; } } } 54 5 59
615 59 $done=1;
616  
617 RULE:
618 59 for (@nrules) {
619 184 my($lhs,$rhs)=@$_;
620  
621 184 ▶ exists($$nullable{$lhs})
Branch: 1 branch
BranchTrueFalseTotal
exists $nullable->{$lhs} and next 12 172 184
622 and next;
623  
624 172 for (@$rhs) {
625 165 ▶ exists($$nullable{$_})
Branch: 1 branch
BranchTrueFalseTotal
exists $nullable->{$_} or next RULE 5 160 165
626 or next RULE;
627 }
628 12 $done=0;
629 12 ++$$nullable{$lhs};
630 }
631  
632 }until($done);
633 }
634  
635 54 ▶sub _ReduceGrammar {
Subroutine _ReduceGrammar: called CC=12 SCAR=26.8
636 54 my($values)=@_;
637 54 my($ufrules,$ufnterm,$reachable);
638  
639 my($grammar)= bless {
640 HEAD => $values->{HEAD},
641 TAIL => $values->{TAIL},
642 EXPECT => $values->{EXPECT},
643 # Casiano modifications
644 SEMANTIC => $values->{SEMANTIC}, # added to simplify AST
645 BYPASS => $values->{BYPASS}, # added to simplify AST
646 BUILDINGTREE => $values->{BUILDINGTREE}, # influences the semantic of lists * + ?
647 ACCESSORS => $values->{ACCESSORS}, # getter-setter for %tree and %metatree
648 PREFIX => $values->{PREFIX}, # yyprefix
649 NAMINGSCHEME => $values->{NAMINGSCHEME}, # added to allow programmable production naming schemes (%name)
650 NOCOMPACT => $values->{NOCOMPACT}, # Do not compact action tables. No DEFAULT field for "STATES"
651 CONFLICTHANDLERS => $values->{CONFLICTHANDLERS}, # list of conflict handlers
652 TERMDEF => $values->{TERMDEF}, # token => associated regular expression (for lexical analyzer)
653 WHITES => $values->{WHITES}, # string with the code to skip whites (for lexical analyzer)
654 LEXERISDEFINED => $values->{LEXERISDEFINED}, # true if %lexer was used
655 INCREMENTAL => $values->{INCREMENTAL}, # true if '%incremental lexer' was used
656 MODULINO => $values->{MODULINO}, # hash perlpath => path, prompt => question
657 STRICT => $values->{STRICT}, # true if %stric
658 DUMMY => $values->{DUMMY}, # array ref
659 54 TOKENNAMES => {}, # for naming schemes
660 }, __PACKAGE__;
661  
662 54 my($rules,$nterm,$term) = @$values {'RULES', 'NTERM', 'TERM'};
663  
664 54 ($ufrules,$ufnterm) = _UsefulRules($rules,$nterm);
665  
666 exists($$ufnterm{$values->{START}})
667 54 ▶ 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] 54 0 54
668  
669 54 $reachable = _Reachable($rules,$nterm,$term,$ufrules,$ufnterm);
670  
671 54 $$grammar{TERM}{chr(0)}=undef;
672 54 for my $sym (keys %$term) {
673 ( exists($$reachable{$sym})
674 or exists($values->{PREC}{$sym}) )
675 516 ▶ 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 } 516 0 516
Condition66%2/3
exists $reachable->{$sym} or exists $values->{"PREC"}{$sym}
l!l&&r!l&&!r
480360
MC/DC0%0/2
exists $reachable->{$sym} or exists $values->{"PREC"}{$sym}
exists $reachable->{$sym} exists $values->{"PREC"}{$sym}
Truth table66%2/3
A or B
A= exists $reachable->{$sym}B= exists $values->{"PREC"}{$sym}
ABresult
1X1
011
000
676 $$grammar{TERM}{$sym}
677 516 ▶ = defined($$term{$sym}[0]) ? $$term{$sym} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $term->{$sym}[0] ? : 254 262 516
678 516 next;
679 };
680 0 push(@{$$grammar{UUTERM}},[ $sym, $values->{SYMS}{$sym} ]);
681 }
682  
683 54 $$grammar{NTERM}{'$start'}=[];
684 54 for my $sym (keys %$nterm) {
685 exists($$reachable{$sym})
686 223 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
exists $reachable->{$sym} and do { exists $values->{"NULL"}{$sym} and ++$grammar->{"NULLABLE"}{$sym}; $grammar->{"NTERM"}{$sym} = []; next } 223 0 223
687 exists($values->{NULL}{$sym})
688 223 ▶ and ++$$grammar{NULLABLE}{$sym};
Branch: 1 branch
BranchTrueFalseTotal
exists $values->{"NULL"}{$sym} and ++$grammar->{"NULLABLE"}{$sym} 16 207 223
689 223 $$grammar{NTERM}{$sym}=[];
690 223 next;
691 };
692 0 push(@{$$grammar{UUNTERM}},[ $sym, $values->{SYMS}{$sym} ]);
693 }
694  
695 54 for my $ruleno (0..$#$rules) {
696 vec($ufrules,$ruleno,1)
697 and exists($$grammar{NTERM}{$$rules[$ruleno][0]})
698 691 ▶ 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 } 691 0 691
Condition33%1/3
vec $ufrules, $ruleno, 1 and exists $grammar->{"NTERM"}{$rules->[$ruleno][0]}
!ll&&!rl&&r
00691
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
699 691 push(@{$$grammar{RULES}},$$rules[$ruleno]);
700 691 push(@{$$grammar{NTERM}{$$rules[$ruleno][0]}},$#{$$grammar{RULES}});
701 691 next;
702 };
703 0 push(@{$$grammar{UURULES}},[ @{$$rules[$ruleno]}[0,1] ]);
704 }
705  
706 54 _SetNullable(@$grammar{'RULES', 'TERM', 'NULLABLE'});
707  
708 54 $grammar;
709 }#_ReduceGrammar
710  
711 0 ▶sub tokennames {
Subroutine tokennames: not called CC=2 SCAR=17.9
Pod undocumented
712 0 my $self = shift;
713  
714 0 my $grammar = $self->{GRAMMAR};
715 0 ▶ $grammar->{TOKENNAMES} = { (%{$grammar->{TOKENNAMES}}, @_) } if (@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
716 $grammar->{TOKENNAMES}
717 0 }
718  
719 61 1;
720  
« Eyapp/Base.pm ↑ Summary Eyapp/Unify.pm »