On Apr 28, 2:05 pm, mor...@faui2k3.org (Moritz Lenz) wrote: > Am 27.04.2010 06:31, schrieb Stéphane Payrard: > > > 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 . > > > my $twigil := $<twigil> ?? ~$<twigil> !! ''; > > > would read > > > my $twigil := $<twigil> ?? ~$<twigil> !! ''; > > ... end even > > my $twigil = $<twigil> // ''; > > But it raises other questions that need answering. > > Currently the ? quantifier is just syntactic sugar for the general > quantifier ** 0..1. > Would that behave the same?
I contend that the ? quantifier is different then syntactic sugar for the quantifier ** 0..1. It is a special case that deserves to behave differently. Matching zero or one times means something being there or not being there as shown by my proposed presence or absence in the parse tree. This is quite different from the idea conveyed by arrays of length respectively zero or one. > Are you proposing that all quantifiers that match zero times should not > appear in the parse tree? Then <twigil>+ could either lead to no capture > at all, a single value or a list - not very nice. > Or rather special-casing the 0..1 quantifier? > > There's also another problem with your approach: If you have > <twigil>? > in your regex, and it matches the empty string, it is still a successful > match - yet with your proposal, it's impossible to distinguis a > successful zero-width match from an unsuccessful match (which can happen > in alternations). An optional is always a successful match. It is only an interesting one when it matches a non-null string otherwise it does not deserve to appear in the parse- tree. Because the match is alway successful, I suppose the associated action should always be called albeit with some null value for an empty match. > > Cheers, > Moritz