Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 检查幻数_Regex_Perl - Fatal编程技术网

Regex 检查幻数

Regex 检查幻数,regex,perl,Regex,Perl,对于找到的每个幻数,我必须以带有“magic number found”消息的行号形式给出输出 我正在分析一个.C文件 幻数基本上是 if(list == 5) // here 5 is magic number for(int i = 0; i<6; i++) //here 6 is magic number if(list==5)//这里5是幻数 对于(int i=0;i,再次编辑。这次对于if条件,它首先匹配成对的括号,然后在=之后查找数字 我已经

对于找到的每个幻数,我必须以带有“magic number found”消息的行号形式给出输出

我正在分析一个.C文件

幻数基本上是

if(list == 5)          // here 5 is magic number

for(int i = 0; i<6; i++)        //here 6 is magic number
if(list==5)//这里5是幻数

对于(int i=0;i,再次编辑。这次对于
if
条件,它首先匹配成对的括号,然后在
=
之后查找数字

我已经使它更加健壮,因为它将识别多行条件测试。然而,正如其他人所说,这可能仍然不能覆盖100%的可能情况

use 5.14.0;
use warnings;

my $data = join '', <DATA>;

my $if = qr/if \s* ( \( (?: [^()]++ | (?-1) )*+ \) ) /spx; # matching paired parenthesis
my $for = qr/for (\s*\(.*?;.*?) (\d+) \s*? ; /spx; #(\d+) is for the Magic Number

for($data){
    while (/$if/g){
        my $pat = ${^MATCH};
        my $line =()= ${^PREMATCH} =~ /\n/g;
        # assumes a magic number exists only when '==' is present
        if ($pat =~ /(.* == \s* )([0-9a-fA-F.]+)\s*\)/x){
            my $mn = $2;
            my $condition = $1;
            $line += () = $condition =~ /\n/g;
            say "Line ", $line + 1," has magic number $mn";
        }
    }
    while (/$for/g){
        my $mn = $2; #Magic Number
        my $condition = $1; #For counting \n in the MATCH.
        my $line =()= ${^PREMATCH} =~ /\n/g; #Counting \n in the PREMATCH.
        $line += () = $condition =~ /\n/g;
        say "Line ", $line + 1," has magic number $mn";
    }
}


__DATA__
if(list ==
5)          // here 5 is magic number

for(int i = 0; i<6
 ;
i++
)        //here 6 is magic number

if(list ==
8)          // here 8 is magic number

if (IsMirrorSubChainEnable())
{
    err = ChainCtrlSetMirrorSC(pChainCtrl, pNewRouteInfo);
}

ModCallCallbacks((ModT *)pChainCtrl, kDoneVinResetCallback, NULL, 0);

我认为它远比
regex
检查复杂得多。如果每个子句都在不同的行中,那么用
for
如何?或者用一个名为
i345
的变量代替
I
?只说几个问题。@Birei是的,是的……请不要想这个问题。那么有没有其他方法让我弄清楚,你想解析C代码,但是你从来没有在你的问题中提到过它?也许你应该?Perl::Critic在这方面有一个策略,也许它会有所帮助:如果你正在用正则表达式解析C源代码,现在就停止。你正在进入一个充满bug和痛苦的世界。使用一个合适的解析器,并在AST上操作。从长远来看,这应该是值得的。我不能推荐任何库,但是我知道–但是,它在解析代码之前运行预处理器。它工作得很好。但是正如用户birei所指出的,如果条件语句是多行的,那么它不会给出期望output@Ad-维克:我想说的是,这对于发现一些代码问题仍然是非常有用的。如果这就是它的目的,那么请考虑一下对我的回报这段代码很简单,可能会找到75%你想要的。此外,你可能会启动一个编辑器,开始修复,并会找到更多。在你的“魔法数字查找器”中获得更高的准确性你的修缮需要时间…@jing谢谢。它正是我想要的。你帮了我很多。十六进制也可以是神奇的,所以它可以包含十六进制吗number@Ad-维克:您可以将
$if
正则表达式更改为
我的$if=qr/if(\s*\(.*)([0-9a-fA-F.]+)\s*\)/spx;
,在
[0-9a-fA-F.]+
可以识别十进制数和十六进制数。我在里面放了一个
,以防你有类似
if(list==8.8)
的东西。如果不需要,你可以从正则表达式中删除点,使类
[0-9a-fA-F]+
for
循环通常没有这样的问题,我想你可以把
$for
正则表达式放在一边。@jing在运行演示c文件时,代码给出了很多错误的结果
use 5.14.0;
use warnings;

my $data = join '', <DATA>;

my $if = qr/if \s* ( \( (?: [^()]++ | (?-1) )*+ \) ) /spx; # matching paired parenthesis
my $for = qr/for (\s*\(.*?;.*?) (\d+) \s*? ; /spx; #(\d+) is for the Magic Number

for($data){
    while (/$if/g){
        my $pat = ${^MATCH};
        my $line =()= ${^PREMATCH} =~ /\n/g;
        # assumes a magic number exists only when '==' is present
        if ($pat =~ /(.* == \s* )([0-9a-fA-F.]+)\s*\)/x){
            my $mn = $2;
            my $condition = $1;
            $line += () = $condition =~ /\n/g;
            say "Line ", $line + 1," has magic number $mn";
        }
    }
    while (/$for/g){
        my $mn = $2; #Magic Number
        my $condition = $1; #For counting \n in the MATCH.
        my $line =()= ${^PREMATCH} =~ /\n/g; #Counting \n in the PREMATCH.
        $line += () = $condition =~ /\n/g;
        say "Line ", $line + 1," has magic number $mn";
    }
}


__DATA__
if(list ==
5)          // here 5 is magic number

for(int i = 0; i<6
 ;
i++
)        //here 6 is magic number

if(list ==
8)          // here 8 is magic number

if (IsMirrorSubChainEnable())
{
    err = ChainCtrlSetMirrorSC(pChainCtrl, pNewRouteInfo);
}

ModCallCallbacks((ModT *)pChainCtrl, kDoneVinResetCallback, NULL, 0);
Line 2 has magic number 5
Line 10 has magic number 8
Line 4 has magic number 6