Parse::ABNF perl usage [closed]

2019-09-07 03:21发布

问题:

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));