ABNF Parsers

2.4.1 · active · verified Sat Apr 11

ABNF is a Python package designed to generate parsers from Augmented Backus-Naur Form (ABNF) grammars, as specified in RFC 5234 and RFC 7405. While its primary use is to parse data defined in RFCs, it is capable of handling any valid ABNF grammar. The library originated from parsing HTTP headers in a web framework and has since been utilized in production environments. The current version is 2.4.1.

Warnings

Install

Imports

Quickstart

Demonstrates how to create a custom ABNF rule and parse a string using a pre-defined grammar from the `abnf.grammars` module, such as RFC 7232.

from abnf import Rule
from abnf.grammars import rfc7232

# Example 1: Creating a custom rule and parsing (simplified)
# This demonstrates the structure, actual parsing needs a Rule instance.
custom_rule_grammar_str = 'double-quoted-string = DQUOTE *(%x20-21 / %x23-7E / %x22.22) DQUOTE'
custom_rule_instance = Rule.create(custom_rule_grammar_str)
print(f"Created custom rule: {custom_rule_instance.name}")

# Example 2: Parsing with a pre-defined RFC grammar
src = 'W/"moof"'
# Instantiate the ETag rule from rfc7232 grammar
etag_rule = rfc7232.Rule('ETag')

# Parse the source string
node, start = etag_rule.parse(src)

print(f"\nParsed ETag: {src}")
print(f"Resulting AST node: {node}")
print(f"Parsing started at index: {start}")

view raw JSON →