Re: Another tweak of the ol' Mental Model
[email protected] ((Randal L. Schwartz))
| Newsgroups | perl.recdescent |
|---|---|
| Organization | Stonehenge Consulting Services; Portland, Oregon, USA |
| Message-ID | <[email protected]> |
>>>>> "Ken" == Ken Lacrosse <[email protected]> writes: Ken> Howdy All, I am once again (no big surprise here) puzzled about some RecDescent Ken> behaviour. Would anyone care to attempt to explain in small words why the Ken> following snippet won't parse? Ken> -------------------------- Ken> use Parse::RecDescent; Ken> my $parse = Parse::RecDescent->new(join '', <DATA>) or die "Bad Grammar!"; Ken> my $text = "a\nb\nc\nd\n"; Ken> my $x = $parse->lines($text); Ken> if ($x) Ken> { Ken> print "x=$x $text parsed successfully!\n"; Ken> } Ken> else Ken> { Ken> print "$text is NOT parsable!!\n"; Ken> } Ken> __DATA__ Ken> lines: /a|b|c|d/ EOL {print "line $item[0]\n"; 1} Ken> EOL: /\n/ {1} Ken> -------------------------- Ken> Thanks again for taking pity on the unenlightened! :-) Ken> Ken I'm lost. Your grammar will parse /^[abcd]\n/, and that's it, but you've given it "a\nb\nc\nd\n". Oh, I see, you're expecting it to parse the prefix. You're swallowing your newline (because "skip" is whitespace) between the /a/ and the EOL, so there's no *additional* EOL to be found, and you lose. I was able to fix it by changing your rule to lines: <skip:''> /a|b|c|d/ EOL {print "line $item[0]\n"; 1} -- Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095 <[email protected]> <URL:http://www.stonehenge.com/merlyn/> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc. See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!