in reply to Backreference variables in code embedded inside Perl 5.10 regexps

Got a better solution for you:
use strict; use warnings; sub rel_cap { my ($ofs) = @_; substr($_, $-[$ofs], $+[$ofs] - $-[$ofs] +) } sub parser { local our @stack; local our @rv; my $parser = qr{ ^ (?&expr) (?&expr) \z (?{ @rv = @stack; }) (?(DEFINE) (?<expr> (.) (.) (?{ local @stack = (@stack, rel_cap(-2) . "|" . rel_cap(-1) ); }) ) ) }x; return $_[0] =~ /$parser/ && \@rv; } my $rv = parser('abcd'); print("$_\n") for @$rv;
a|b c|d

Update: Nevermind. @- and/or @+ become all wonky if there's a (?&...) inside the (?<...>...).

Even something as simple as the following fails:

(?<expr> (?&foo) (.) (?{ local @stack = (@stack, rel_cap(-1) ); }) ) (?<foo> . )

Replies are listed 'Best First'.
Re^2: Backreference variables in code embedded inside Perl 5.10 regexps (try2)
by casiano (Pilgrim) on Sep 11, 2009 at 12:05 UTC
    Many, many thanks ikegami.
    ... Nevermind. @- and/or @+ become all wonky if there's a (?&...) inside the (?<...>...).
    It seems to me that I have found the reason it becomes wonky: see the node

    Strange behavior of @- and @+ in perl5.10 regexps.

    The following version of your rel_cap subroutine seems to work:

    pl@nereida:~/Lperltesting$ cat calc510withactions4.pl #!/usr/local/lib/perl/5.10.1/bin//perl5.10.1 use v5.10; # Infix to postfix translator using 5.10 regexp # Original grammar: # exp -> exp [-+] term # | term # term -> term [*/] digits # | digits # Applying left-recursion elimination we have: # exp -> term re # re -> [+-] term re # | # empty # term -> digits rt # rt -> [*/] rt # | # empty sub rc { my $ofs = - shift; my $np = @-; substr($_, $-[$ofs], $+[$np+$ofs] - $-[$ofs]) } my $input; my @stack; my $regexp = qr{ (?&exp) (?(DEFINE) (?<exp> (?&term) (?&re) (?{ say "exp -> term re" }) ) (?<re> \s* ([+-]) (?&term) \s* (?{ push @stack, $^N }) (?& +re) (?{ say "re -> [+-] term re" }) | # empty (?{ say "re -> empty" }) ) (?<term> ((?&digits)) (?{ # intermediate action push @stack, $^N }) (?&rt) (?{ say "term-> digits($^N) rt"; }) ) (?<rt> \s*([*/]) ((?&digits)) \s* (?{ # intermediate action push @stack, rc(1), rc(2) }) (?&rt) # end of <rt> definition (?{ say "rt -> [*/] digits($^N) rt" }) | # empty (?{ say "rt -> empty" }) ) (?<digits> \s* \d+ ) ) }xms; $input = <>; chomp($input); if ($input =~ $regexp) { say "matches: $&\nStack=(@stack)"; } else { say "does not match"; }
    Now I can access the attributes of the previous symbols. See the line
    push @stack, rc(1), rc(2)
    Follows an execution:
    pl@nereida:~/Lperltesting$ ./calc510withactions4.pl 2-8/4/2-1 rt -> empty term-> digits(2) rt rt -> empty rt -> [*/] digits(2) rt rt -> [*/] digits(4) rt term-> digits(8) rt rt -> empty term-> digits(1) rt re -> empty re -> [+-] term re re -> [+-] term re exp -> term re matches: 2-8/4/2-1 Stack=(2 8 4 / 2 / - 1 -)