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

Reset local parser variables for each new input file

Thread Next
From:
Alexander.Farber
Date:
September 13, 2004 01:05
Subject:
Reset local parser variables for each new input file
Message ID:
FF5965B28E306049BAE4B32AA38359B463EDA7@boebe003.europe.nokia.com
Hi,

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 test_pd.pl
#!/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 test_pd.pl 
1: <A>
2: <B>
3: <C>
Bad text 2 at test_pd.pl 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.

Regards
Alex

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About