使用perl解析abnf语法



感谢您在链接Parse::ABNF perl用法中提供的问题输入。我在解决问题方面仍然面临困难。请求检查我下面的问题,并提供解决方案的指针。

现在,我已经在一个文件中创建了ABNF格式的sip语法(命名为sip.abnf)。我在下面的文件中有带有标题的sip消息(recd_message.txt):

From: <sip:07455900064@212.136.178.216:5060;user=phone>;tag=1526438727-1338998848384-
To: "govoice-lab2-aokanlawon"<sip:441127653485@isf.fire.ipgcom.com>
Contact: <sip:07455900064@10.12.33.29: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/x0dx0a/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] receivedn";
}
}
} else {
print "Error: grammar is not definedn";
}

Perl调用和输出/错误低于

$> perl -I. testSipHeader.pl sip.abnf recd_messages.txt
Grammar is now defined...
Processing the message From: <sip:07455900064@212.136.178.216:5060;user=phone>;tag=1526438727-
Can't call method "From" on unblessed reference at testSipHeader.pl line 21, <$header> line 1.

注意:目前我有一个生成SIP头的C程序,我正试图通过这个perl函数验证头的内容。我正在尝试使用类似于链接Parse::RecDescent语法中给出的用法,但没有像预期的那样工作

我不得不在你的脚本中做一些修改,以包括Parse::ABNF和handle输入文件,之后收到的输出出现在路径中https://drive.google.com/file/d/0B8KDQDXsCKR_ZERzV3IyY1M2NW8/edit?usp=sharing

我已经重写了您的脚本,输出是什么?

use Data::Dumper;
my $grammar_rules;
{
local $/=undef;
open(my $fh,'<',$grammar_file) or die $grammar_file,$!;
$grammar_rules = <$fh>;
$grammar_rules =~ s/x0dx0a/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 $linen";
@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] receivedn";
}
}

相关内容

  • 没有找到相关文章

最新更新