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