A lexer returns tokens, and tokens are rather simple. So simple that parsing them requires no backtracking. This makes it very easy to not use regexps.

But it also your task possible (but not simple) with regexps too. Take Perl single-quoted strings:

sub squote { my $s_pos = pos; my $match = / \G ' (?> (?: \\. | [^\\'] )* ) ' /xsgc; my $e_pos = pos; if ($match) { $_[0] = substr($_, $s_pos, $e_pos-$s_pos); return 1; } else { pos = $s_pos; return 0; } }
becomes
sub squote { my $s_pos = pos; RETRY: { local our $at_eof = ...; local our $need_more = 0; my $match = / \G (?: \z (?{ $need_more = !$at_eof }) )? ' (?> (?: \\ (?: \z (?{ $need_more = !$at_eof }) )? . | [^\\'] )* ) (?: \z (?{ $need_more = !$at_eof }) )? ' /xsgc; if ($need_more) { ... pos = $s_pos; redo RETRY; } my $e_pos = pos; if ($match) { $_[0] = substr($_, $s_pos, $e_pos-$s_pos); return 1; } else { pos = $s_pos; return 0; } } }

By the way, you asked
"How can I tell if a regular expression is matching but fails merely because it ran out of data?"
but you should also be asking
"How can I tell if a regular expression succeeded merely because it ran out of data?"

Take an identifier, for example. If $_ contains 'abc', it'll return a match, but it'll return the wrong match if the next character is 'd'.

sub ident { my $s_pos = pos; my $match = / \G [a-zA-Z_] [a-zA-Z0-9_]* /xsgc; my $e_pos = pos; if ($match) { $_[0] = substr($_, $s_pos, $e_pos-$s_pos); return 1; } else { pos = $s_pos; return 0; } }

becomes

sub ident { my $s_pos = pos; RETRY: { local our $at_eof = ...; local our $need_more = 0; my $match = / \G (?: \z (?{ $need_more = !$at_eof }) )? [a-zA-Z_] (?> [a-zA-Z0-9_]* ) (?: (?= . ) | (?{ $need_more = !$at_eof }) ) /xsgc; if ($need_more) { ... pos = $s_pos; redo RETRY; } my $e_pos = pos; if ($match) { $_[0] = substr($_, $s_pos, $e_pos-$s_pos); return 1; } else { pos = $s_pos; return 0; } } }

Update: Simple optimization.


In reply to Re: How regexes fail. by ikegami
in thread How regexes fail. by jdoege

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.