develooper Front page | perl.perl6.language | Postings from May 2005

Re: C<::> in rules

Thread Previous | Thread Next
From:
Luke Palmer
Date:
May 13, 2005 18:15
Subject:
Re: C<::> in rules
Message ID:
7ca3f016050513181516d3bc06@mail.gmail.com
On 5/13/05, Patrick R. Michaud <pmichaud@pobox.com> wrote:
> First, I'm quite certain that $r2 and $r3 are different.  For
> illustration, let's use a variation like:
> 
>     $q2 = rx / \w [ abc ::: def | ghi ::: jkl | mn ::: op ] /;
>     $q3 = rx / \w [ [ abc :: def | ghi :: jkl | mn :: op ] ]/;
> 
>     "xyzabc---xyzghijklmno" ~~ $q2     # fails after seeing "zabc"
>     "xyzabc---xyzghijklmno" ~~ $q3     # matches "zghijkl"

Okay, I know where the misunderstanding is.  When we use these kinds
of examples, let's not rely on the implicit matching semantic.  I'm
saying that the above code is equivalent to:

    # the following is a rule, so ::: backtracks out of it and no further
    rule q2 { \w [ abc ::: def | ghi ::: jkl | mn ::: op ] }
    rule q3 { \w [ [ abc :: def | ghi :: jkl | mn :: op ] ] }
    "xyzabc---xyzghijklmno" ~~ /^ .*? <q2>/;   # ::: backtracks into the .*?
    "xyzabc---xyzghijklmno" ~~ /^ .*? <q3>/;

The presence of the \w does nothing, because \w doesn't backtrack. 
Alternations and quantifiers backtrack when you fail beyond them, \w
just fails.  You never enter the same subpattern (meant in the most
general case: .* is a subpattern, for instance) in the same state. 
Something had to change behind you in order for a subpattern to be
re-entered.

I think the misunderstanding is rather simple.  You keep talking like
you prepend a .*? to the rule we're matching.  I think that's wrong
(and this is where I'm making a design call, so we can dispute on this
once we're clear that it's this that is being disputed).  I think
there is a special rule:

    rule matchanywhere($rx) { .*? <$rx> }

Which makes a *subrule call* to the rule we're matching.  Therefore
::: just breaks out of that subrule, and backtracks into the .*?
again.

Because of this, I think there will be a difference between ::: and
<commit> at the top level, but not :: and :::.

Luke

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About