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) (? (?&term) (?&re) ) (? \s* ([+-]) (?&term) \s* (?{ push @stack, $^N }) (?&re) | # empty ) (? (?&digits) (?&rt) ) (? \s*([*/]) (?&digits) \s* (?{ push @stack, $^N }) (?&rt) | # empty ) (? \s* (\d+) (?{ push @stack, $^N }) ) ) }xms; my $input = <>; chomp($input); if ($input =~ $regexp) { say "matches: $&\nStack=(@stack)"; } else { say "does not match"; } #### 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 -)