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 -)

In reply to Re^2: Backreference variables in code embedded inside Perl 5.10 regexps (try2) by casiano
in thread Backreference variables in code embedded inside Perl 5.10 regexps by casiano

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.