说困难很困难,要解析结构文法(grammar)+ debug + ...
对学过的文法架构解析也是很简单,写出正确文法描述,就能做出所要(四则运算)代码。
S -> T + T | T - T | T
T -> F * F | F / F | F
F -> NUMBER | '(' S ')'
NUMBER -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
#Here is a simple way to do it by keeping track of how deep you are into parenthesis' and what depth your match occured at:
use strict;
my $str = "((A,B),C,(D,E))";
foreach my $m (qw(A B C D E)) {
print $m." => ".FindMinParens($m,$str)."\n";
}
sub FindMinParens
{
my $match = shift;
my $string = shift;
my @start_pos;
my $depth = -1;
# keep matching parens or $match
while ($string=~/([()]|$match)/g) {
if ($1 eq "(") {
# record opening paren positions
push @start_pos,pos($string);
}
elsif ($1 eq ")") {
# if we reached the closing parens for the minimum pair
# get the sub string and exit
if ($#start_pos == $depth) {
my $start = $start_pos[-1];
my $len = pos($string) - $start -1;
return substr($string,$start,$len);
}else{
pop @start_pos;
}
}
else {
# store depth of $match
$depth = $#start_pos;
}
}
return "";
}
这个方法的输出比较直观:
This is probably a klunky solution, but it reminded me of a parser I had written.. (Evaluate Expressions.)
Quickly hacking at it and only testing a few simple cases (you've been warned)
You could use the returned structure to search for whatever criteria you desire. I'm sure someone here has a much more elegant solution, but my regex abilities are rather limited...
use warnings;
#use strict;
use Data:umper;
my $input = '(a,b,(c,d,(e,f,g)))';
print Dumper parse_expression($input);
sub parse_expression {
my $exp = shift;
my @tokens = ();
$exp=~s/\s*([()])\s*/ $1 /go;
# Get tokens
push @tokens, $1 while $exp=~/\G\s*(".*?"/gc or $exp=~/\G\s*('.*?')/gc or $exp=~/\G\s*(\S+)/gc;
# Find any parens.
my (@lp,@rp) = ();
for (my $p =0; $p < @tokens; $p++){
if ($tokens[$p] eq '('){
push @lp,$p;
}elsif($tokens[$p] eq ')'){
push @rp,$p;
}
}
if ( @lp != @rp){
warn "Mismatched parens in expression.\n";
return;
my $expr = '((A,B),C,(D,E))';
print extract2('C',$expr),"\n";
print extract2('B',$expr),"\n";
print extract2('D',$expr),"\n";
sub extract2 {
my $char = shift;
my ($str,$dup) = (shift) x 2;
1 while $dup =~ s/\([^()$char]*\)/'.' x length $&/e;
print $dup."\n";#转帖时添加,便于寻究其思路
$dup =~ m/(\([^()$char]*$char[^()]*\))/;
return substr($str,$-[1],length $1);
}