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

Reply via email to