Hello monks,

After playing a bit with perl 5.10 regexps I have being able to work a primitive infix to postfix translator with them:

pl@nereida:~/Lperltesting$ cat ./calc510withactions2.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 my @stack; my $regexp = qr{ (?&exp) (?(DEFINE) (?<exp> (?&term) (?&re) ) (?<re> \s* ([+-]) (?&term) \s* (?{ push @stack, $^N }) (?& +re) | # empty ) (?<term> (?&digits) (?&rt) ) (?<rt> \s*([*/]) (?&digits) \s* (?{ push @stack, $^N }) (? +&rt) | # empty ) (?<digits> \s* (\d+) (?{ push @stack, $^N }) ) ) }xms; my $input = <>; chomp($input); if ($input =~ $regexp) { say "matches: $&\nStack=(@stack)"; } else { say "does not match"; }
Minus and divide operators seem to behave according the expected left associativity:
pl@nereida:~/Lperltesting$ ./calc510withactions2.pl 2-8*4/2/4-1 matches: 2-8*4/2/4-1 Stack=(2 8 4 * 2 / 4 / - 1 -)
I feel however that I can't extend this methodology to more complex tasks because I haven't found a way other than $^N to refer to the values associated with the previous parenthesis. Which means that I can only refer to the attribute of the last parenthesis.

Is in Perl 5.10 something like relative variable backreferences so that I can use something like $-1, $-2, etc. to refer to the last parenthesis, the one before the last, etc. inside the embeded code? I.e. I am looking for s.t. similar to relative backreferences like \g{-1} but to be used inside inserted code?

Apologies for not being able to clarify the question enough. I feel I do not have yet a clear understanding of Perl 5.10 regexps.


In reply to 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.