0

我使用模块 Parse::RecDescent
并尝试构建语法以捕获 perl 子名称,为什么在我的代码中 https://gist.github.com/1595532

不工作语法 get_sub: NOWORD TEST NOWORD

在 '>test1$' 上?(变量 $perl_code1 )

use Modern::Perl;
use Parse::RecDescent;

my $perl_code1  = q{my >test1$ string3 = $object->oldSub(6,7);};
my $perl_code2  = q{test1$ string3 = $object->oldSub(6,7);};
my $perl_code3  = q{my test1$ string3 = $object->oldSub(6,7);};
my @perl_lines  = ( $perl_code1, $perl_code2, $perl_code3 );
my $var         = 'test1';
my $sub_grammar = q{

    get_sub: 
           NOWORD TEST NOWORD
               {print $item[2]}      
           |TEST NOWORD
              {print $item[1]}     

    get_sub2: 
           WORD TEST NOWORD
               {print $item[2]}      
           |TEST NOWORD
              {print $item[1]}     

    WORD: 
        /\w+/    
    NOWORD: 
        /\W+/            
    TEST: 
        /} . $var . q{/
    };
my $sub_parse = new Parse::RecDescent($sub_grammar);
say 'example1:';

for my $string1 (@perl_lines) {
    say "Valid sub:$string1\n" if $sub_parse->get_sub($string1);
}

say 'example2:';
for my $string2 (@perl_lines) {
    say " Valid sub:$string2\n" if $sub_parse->get_sub2($string2);
}
4

1 回答 1

2

$perl_code1以 string 开头,my规则不匹配。您需要在语法中类似以下内容:

get_sub:
       WORD NOWORD TEST NOWORD
           ...
于 2012-01-12T12:12:36.107 回答