You always use possessive quantifiers and/or other backtracking control when you're convinced that what you've parsed right now is right.

Suppose you want to parse something like Perl code, and you see

from /foo/

First you'd match an identifier, say with \w+, and then a regex. However if that parsing fails (for example because there's another line, and no semicolon), then instead of giving up, \w+ starts matching fro, and the regex parsing code matches m /foo/ (which is a valid regex too).

This is both slower than failing outright, and can lead to misparses in some situations. So you'd really want to match an identifier as \w++ instead of just \w+

The standard Perl 6 grammar uses Perl 6 regexes to parse Perl 6. Every time you see a token or a rule, it's a non-backtracking regex.

There backtracking control is crucial for getting good parse error messages, and for speed.


In reply to Re: When would I want to use possessive quantifiers? by moritz
in thread When would I want to use possessive quantifiers? by petdance

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.