On Monday, Sep 13, 2004 [EMAIL PROTECTED] said: > Yes, I've tried that and it seems to work, but I'm > worried about the cases when the input files don't parse. > Will the mmpfile: { $i = 1 } still always trigger?
Yes. Note the syntax. mmpfile: This is the name of the rule. { $i = 1 } This is an action. If it returns true (which it does, although apparently by "accident"), then mmpfile *may* still be parsed. letter(s) This is a one or more named sub-rule call If one or more instances of "letter" are found consecutively, this returns true. /^\Z/ This is a regular expression match. If this regular expression matches the remainder of the string at this point, it returns true. If all items return true, then mmpfile succeeds. So far, I'm not telling you anything you don't already know, but the whole point is that the "{ $i = 1 }" action will *always* fire because it is first in line. If the action occurred anywhere else, there is the possibility that the desired side effect won't happen. This may or may not be good, depending on the rest of the design. Personally, I "don't like" the approach, because it depends on side effects and places the information storage somewhere else than in the parsed object itself, but it can be made to work as your example clearly shows. My objection falls under the "globals are bad" motherhood statement. Two things to remember are that the initializer action should always be first and that it must return "true". > > Also, what is it? It's not an action for the mmprule, isn't it? > > > -----Original Message----- > > From: ext Sean O'Rourke [mailto:[EMAIL PROTECTED] > > > > At Mon, 13 Sep 2004 10:04:37 +0200, <[EMAIL PROTECTED]> wrote: > > > Something like the "Start-up actions" from the "perldoc P::RecDescent", > > > but they should be reset on every new parser call and not just on the > > > grammar compilation, like here: > > > > Can you just add an action to the beginning of your start rule, like so? > > > > mmpfile: { $i = 1 } letter(s) /^\Z/ > > bolinux72:afarber {514} perl test_pd.pl > 1: <A> > 2: <B> > 3: <C> > Bad text 2 at test_pd.pl line 16. > 1: <X> > 2: <Y> > 3: <Z> > > bolinux72:afarber {515} cat test_pd.pl > #!/usr/bin/perl -w > > use strict; > use Parse::RecDescent; > use constant GRAMMAR => q( > > { my $i = 1; } > > mmpfile: { $i = 1} letter(s) /^\Z/ > letter: /([A-Z])/ { print "$i: <$1>\n"; $i++ } > > ); > > my $parser = Parse::RecDescent->new(GRAMMAR) or die 'Bad grammar'; > defined $parser->mmpfile('ABC') or warn 'Bad text 1'; > defined $parser->mmpfile('klm') or warn 'Bad text 2'; > defined $parser->mmpfile('XYZ') or warn 'Bad text 3'; > -- Intel, Corp. 5000 W. Chandler Blvd. Chandler, AZ 85226 -- Intel, Corp. 5000 W. Chandler Blvd. Chandler, AZ 85226