Parse::ABNF perl usage [closed]

柔情痞子 提交于 2019-12-25 02:08:47

问题


I need to parse the SIP headers (grammar in ABNF format) and verify if my Header strings are ok or not.

(Example: check strings like "Accept: application/sdp,application/3gpp-imp+xml" to provide testcase pass/fail).

Currently I am trying to use perl Parse::ABNF. Now I am not able to understand the sample usage in this context.


回答1:


My Parse::ABNF module reads ABNF grammars and gives you access to the rules in the grammar. It tells you things like "The floating-point-number rule references the digit rule", but it does not generate a parser for floating point numbers. You can use the module to convert a proper ABNF grammar into a format that can be used by a parser generator like Parse::RecDescent or Marpa2. An example script for such a conversion is included in the distribution as eg/abnf2xlx.pl. Note however that the grammar on the page you link to is not quite the standards-compliant format expected by Parse::ABNF.




回答2:


You could use this module in this way:

  use Parse::ABNF;
  use Test::More;
  use Data::Dumper;
  my $parser = Parse::ABNF->new;
  my $rules = $parser->parse($sip_message);
  ok(defined $rules,'The SIP messgae is parseable') or diag(Dumper($sip_message));

The easier way for parsing just the header:

  use Test::More;
  use Data::Dumper;
  ok($sip_message =~ m!Accept: application/sdp,application/3gpp-imp+xml!,'The SIP header looks found') or diag(Dumper($sip_message));


来源:https://stackoverflow.com/questions/21928268/parseabnf-perl-usage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!