When doing an analyse of a sample parse tree, I note that it is
cluttered by the reduction of optional subrules
to generate a zero length parse subtree. That is, rules with a '?'
quantifier matching zero time.
Suppressing such matching rules from the parse tree would make it
easier to read.
Additionnally, the actions associated with such a quantified rule
matching once would not necessitate a trailing [0].
my $twigil := $<twigil> ?? ~$<twigil>[0] !! '';
would read
my $twigil := $<twigil> ?? ~$<twigil> !! '';
To substantiate my claim of the omnipresence of arrays of length 0 for
optional subrules,
I analyse the parse tree generated by the following program...
sub a ( |$c ($a, $b)) { say $c }
a(1,2);
my $a;
say $a+4*3
..using this one liner
perl -ne '/\<(\w+)\>.*size:(\d+)/ and ++$a[$2] and !$2 && $r{$1}++ ;
END { $,=", "; print $i++ . " : $_\n" for @a; print "$_ : $r{$_}\n"
for sort keys %r }'
..with these results
0 : 49
1 : 9
2 : 1
3 :
4 : 1
arglist : 3
colonpair : 6
default_value : 3
morename : 6
post_constraint : 2
semilist : 1
signature : 1
statement_mod_cond : 5
statement_mod_loop : 5
trait : 5
twigil : 6
type_constraint : 3
typename : 3
It means that most arrays, 49 of them, in the parse tree are of zero
length. When counting rules generating these arrays, we see that most
of them are used in the Perl 6 grammar with a '?' quantifier.
perl -ne '/\<(\w+)\>/ and $i++; END { print $i }' prg.parsed
162
Counting 162 reductions, that means that more than one fourth of them
are "spurious".
When analyzing the rules that have generated 0-length arrays, we
easily understand they are not specific
to the sample program. For example, in normal programs, most
statements have indeed no optional modifiers and many variables don't
have twigils.
--
cognominal stef