in reply to Matching terminals with Parse::RecDescent

You need to force a end of line (or end of file) match. For the sample given, changing your grammar to:

FOO: /foo$/

does the trick. In the context of a file being parsed you are more likely to want

FOO: "foo" /\Z/

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Matching terminals with Parse::RecDescent
by fce2 (Sexton) on Dec 12, 2006 at 04:01 UTC
    /foo$/ works for the example script. "foo" /\Z/ didn't match anything (though /foo\Z/ did). All fall down if I test with this grammar:
    line: FOO BAR FOO: /foo\Z/ BAR: /bar\Z/
    (and use $p->line instead of $p->foo). I probably should have been more specific, but I'm looking for a more generalised solution to this, or at least some understanding of where my expectations are wrong.

      or at least some understanding of where my expectations are wrong.

      Your grammar expects to match

      1. 0 or more whitespace characters (see <skip>),
      2. followed by "foo",
      3. followed by the end of the string to parse,
      4. 0 or more whitespace characters,
      5. followed by "bar",
      6. followed by the end of the string to parse.

      With the grammar:

      line: FOO BAR /$/ FOO: "foo" BAR: "bar"

      and the invocation changed line to:

      if (defined (my $ret = $p->line($test))) {

      I get:

      no match: 'foo' no match: 'foo ' match: 'foo bar' -> '' no match: 'football' no match: 'fog' no match: 'barfoo' no match: 'bar foo'

      Is that not what you expect?


      DWIM is Perl's answer to Gödel