develooper Front page | perl.recdescent | Postings from September 2004

Reset local parser variables for each new input file

Thread Next
September 13, 2004 01:05
Reset local parser variables for each new input file
Message ID:

I have a script which compiles a grammar once and then parses quite 
many files using it. The parser is placed into a separate module and 
thus I have to use quite long names to refer to variables from the 
parser rules (for example $My::Module::CurrentDir = '.' in a rule).

I wonder, if there is a way to define and use "local" variables in
a parser? And they shouldn't be something like $parser->{CurrentDir}
because that's even more unreadable ;-)

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:

bolinux72:afarber {518} cat
#!/usr/bin/perl -w

use strict;
use Parse::RecDescent;
use constant GRAMMAR => q(

{ my $i = 1; }

mmpfile: 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';

bolinux72:afarber {519} perl 
1: <A>
2: <B>
3: <C>
Bad text 2 at line 16.
4: <X>
5: <Y>
6: <Z>

Or, to reformulate my question: ist there a way to reset the $i above?
So that $i = 1 for every new input file, regardless if the file before
has been parsed successfully or not.


Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About