2014-02-25 48 views
1

感謝您發佈在鏈接Parse::ABNF perl usage處的問題輸入。我仍然面臨着解決我的問題的困難。請求下面檢查我的問題,並提供解決方案的指針。使用perl解析abnf語法

現在我已經在ABNF格式的文件中創建了sip語法(命名爲sip.abnf)。 我有報頭的SIP消息在一個文件中像下面(recd_message.txt):

From: <sip:[email protected]:5060;user=phone>;tag=1526438727-1338998848384- 
To: "govoice-lab2-aokanlawon"<sip:[email protected]> 
Contact: <sip:[email protected]:5070;transport=udp> 
Allow: ACK,BYE,CANCEL,INFO,INVITE,OPTIONS,PRACK,REFER,NOTIFY 
Accept: multipart/mixed,application/media_control+xml,application/sdp 

我創建一個Perl程序使用的ABNF語法與驗證它命名爲上述頭信息作爲testSipHeader.pl以下內容:

use strict; use warnings; 
use File::Slurp; 
use Parse::ABNF ; 
use Data::Dumper; 

my $grammar_file = shift; 
my $messages = shift; 
my @header_name; 
my $header_status; 

my $grammar = scalar read_file($grammar_file); 
$grammar =~ s/\x0d\x0a/\n/g; 
$grammar =~ s/^\s+(?=[\w-]+\s*=)//mg; 

$grammar = Parse::ABNF->new->parse( scalar read_file($grammar_file)); 
if (defined $grammar) { 
    print "Grammar is now defined...\n"; 
    my $header; 
    open ($header , "<", $messages) or die "Could not open $messages file"; 
    while(<$header>) { 
     print "Processing the message $_\n"; 
     @header_name = split(': ', $_); 
     if ($header_name[0] eq "From") { 
      $header_status = $grammar->From($_) ; 
     } elsif ($header_name[0] eq "To") { 
      $header_status = $grammar->To($_) ; 
     } elsif ($header_name[0] eq "Contact") { 
      $header_status = $grammar->Contact($_) ; 
     } elsif ($header_name[0] eq "Allow") { 
      $header_status = $grammar->Allow($_) ; 
     } elsif ($header_name[0] eq "Accept") { 
      $header_status = $grammar->Accept($_) ; 
     } else { 
      print "Error: Unsupported header $header_name[0] received\n"; 
     } 
    } 
} else { 
     print "Error: grammar is not defined\n"; 
} 

的Perl invokation和輸出/誤差小於

$> perl -I. testSipHeader.pl sip.abnf recd_messages.txt 
Grammar is now defined... 
Processing the message From: <sip:[email protected]:5060;user=phone>;tag=1526438727- 

Can't call method "From" on unblessed reference at testSipHeader.pl line 21, <$header> line 1. 

注:目前我有根兒的C程序吃了SIP頭,我想通過這個Perl函數驗證頭的內容。我想使用類似用法在鏈接給Parse::RecDescent grammar not working as expected

我不得不修改一點點在你的腳本,包括解析:: ABNF和處理輸入文件,之後接收的輸出出現在路徑https://drive.google.com/file/d/0B8KDQDXsCKR_ZERzV3IyY1M2NW8/edit?usp=sharing

+0

爲什麼在很多地方使用'標量'? – user1126070

回答

0

我把你的腳本重寫了一下,輸出是什麼?

use Data::Dumper; 
my $grammar_rules; 
{ 
    local $/=undef; 
    open(my $fh,'<',$grammar_file) or die $grammar_file,$!; 
    $grammar_rules = <$fh>; 
    $grammar_rules =~ s/\x0d\x0a/\n/g; 
    $grammar_rules =~ s/^\s+(?=[\w-]+\s*=)//mg; 
} 
print Dumper('rules',$grammar_rules); 
my $grammar = Parse::ABNF->new->parse( $grammar_rules); 
print Dumper('grammar',$grammar); 
die "Error: grammar is not defined" if ! defined $grammer; 
print "Grammar is now defined...\n"; 

open (my $header_fh , "<", $messages) or die "Could not open $messages file, $!"; 
while(my $line = <$header_fh>) { 
    chomp($line); 
    print "Processing the message $line\n"; 
    @header_name = split(': ', $line); 
    if ($header_name[0] eq "From") { 
     $header_status = $grammar->From($line) ; 
    } elsif ($header_name[0] eq "To") { 
     $header_status = $grammar->To($line) ; 
    } elsif ($header_name[0] eq "Contact") { 
     $header_status = $grammar->Contact($line) ; 
    } elsif ($header_name[0] eq "Allow") { 
     $header_status = $grammar->Allow($line) ; 
    } elsif ($header_name[0] eq "Accept") { 
     $header_status = $grammar->Accept($line) ; 
    } else { 
     print "Error: Unsupported header $header_name[0] received\n"; 
    } 
}