Parse::ABNF - Parse IETF Augmented BNF (ABNF) grammars.
use Parse::ABNF; my $parser = Parse::ABNF->new; my $rules = $parser->parse($grammar); my $core = $Parse::ABNF::CoreRules;
This module parses IETF ABNF (STD 68, RFC 5234, 4234, 2234) grammars into a Perl data structure, a list of rules as specified below. It does not generate a parser for the language described by some ABNF grammar, but makes it easier to turn an ABNF grammar into a grammar suitable for use with a parser generator that does not natively support ABNF grammars.
Artifacts are mapped into hash references as follows:
A = B ~ { class => 'Rule', value => B, name => A } A /= B ~ { class => 'Rule', value => B, ... combine => 'choice' } A / B ~ { class => 'Choice', value => [A, B] } A B ~ { class => 'Group', value => [A, B] } A ~ { class => 'Reference', name => A } n*mA ~ { class => 'Repetition', value => A, min => n, max => m } [ A ] ~ { class => 'Repetition', value => A, min => 0, max => 1 } *A ~ { class => 'Repetition', value => A, min => 0, max => undef } "A" ~ { class => 'Literal', value => A } <A> ~ { class => 'ProseValue', value => A } %xA.B ~ { class => 'String', value => [A, B], type => 'hex' } %bA.B ~ { class => 'String', value => [A, B], type => 'binary' } %dA.B ~ { class => 'String', value => [A, B], type => 'decimal' } %xA-B ~ { class => 'Range', type => 'hex', min => A, max => B }
Forms not listed here are mapped in an analogous manner.
As an example, the ABNF grammar
A = (B C) / *D
is parsed into
[ { 'value' => { 'value' => [ { 'value' => [ { 'name' => 'B', 'class' => 'Reference' }, { 'name' => 'C', 'class' => 'Reference' } ], 'class' => 'Group' }, { 'min' => 0, 'value' => { 'name' => 'D', 'class' => 'Reference' }, 'max' => undef, 'class' => 'Repetition' } ], 'class' => 'Choice' }, 'name' => 'A', 'class' => 'Rule' } ]
Until this module matures, this format is subject to change. Contact the author if you would like to depend on this module.
Creates a new Parse::ABNF object.
Parse::ABNF
Parses a string into a structure as described above.
When the Grammar::Formal module is available, this function will parse $string into a new Grammar::Formal object. To obtain the ABNF Core rules apply it to the string $Parse::ABNF::CoreRulesGrammar. Alternatively, pass core as option and the method will do it for you.
$string
$Parse::ABNF::CoreRulesGrammar
core
As before, but the result is encoded as simple JSON-Encoding for Trees.
The parse method will retun undef if there is an error in the grammar and Parse::RecDescent will automatically print an error message. Future versions might throw an exception instead.
parse
undef
Parse::RecDescent
The ABNF specification defines some Core Rules that are used without defining them locally in many ABNF grammars. You can access these rules as parsed by this module via $Parse::ABNF::CoreRules.
$Parse::ABNF::CoreRules
Instead of CRLF line endings this module expects "\n" as line terminator. If necessary, convert the line endings e.g. using
$grammar =~ s/\x0d\x0a/\n/g;
The ABNF specification disallows white space preceding the left hand side, and so does this module. Remove it prior to passing the grammar e.g. using
$grammar =~ s/^\s+(?=[\w-]+\s*=)//mg;
This module does not do that for you in order to preserve line and column numbers. Patches adapting the grammar to allow leading white space welcome.
The ABNF specification allows non-terminals to be enclosed inside <...>. That is the same syntax as used for prose values, and this module makes no attempt to differentiate the two.
Comments are not currently made available, this may change in future versions.
Please report bugs in this module via http://rt.cpan.org/NoAuth/Bugs.html?Dist=Parser-ABNF
* http://www.ietf.org/rfc/rfc5234.txt * Parse::RecDescent
Copyright (c) 2008-2018 Bjoern Hoehrmann <bjoern@hoehrmann.de>. This module is licensed under the same terms as Perl itself.
To install Parse::ABNF, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Parse::ABNF
CPAN shell
perl -MCPAN -e shell install Parse::ABNF
For more information on module installation, please visit the detailed CPAN module installation guide.