Registry / serialization / abnf
library2.9.0pypypi✓ verified 23d ago

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.

pip install abnf
INSTALL
IMPORT
SIG · ABNF
A
abnf
serializationpythonv2.9.0
Install
1.6s avg
Import
116ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.9.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.118s · 18.3MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.114s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Rule
from abnf import Rule
rfc7232
from abnf.grammars import rfc7232
Example of importing a pre-defined RFC grammar.

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}")
abnf --version
Debug
Known issues
breakingVersion 2.0.0 introduced significant changes to backtracking implementation, which constituted a breaking change in behavior. Code relying on previous backtracking mechanisms may need adjustment.
fix
Review grammar definitions and parsing logic for compatibility with the new backtracking implementation. Test thoroughly.
affects: >=2.0.0
gotchaAs of version 2.0.0, `Repetition` objects utilize an LRU cache (`ParseCache`) to store parse results, but the initial implementation does not set maximum sizes for these caches. This can lead to unbounded memory growth and long-term performance issues in applications with extensive or long-running parsing tasks.
fix
Monitor memory usage and consider contributing feedback or implementing custom cache management if performance degradation is observed. Consult the library's GitHub for potential updates or configuration options related to cache sizing.
affects: >=2.0.0
gotchaWhen loading grammars from text files using `Rule.from_file`, ABNF strictly expects CRLF (Carriage Return Line Feed) as a delimiter for rules. Many text editors silently convert CRLF to LF (Line Feed only), which can cause parsing failures or unexpected behavior when the grammar file is read by the library.
fix
Ensure that grammar files provided to `Rule.from_file` explicitly use CRLF line endings. Verify line endings with a text editor that can display them (e.g., VS Code, Notepad++).
affects: All versions
gotchaThe library caches `Rule` objects (e.g., `Rule('rule-name')` will return the same object if the rule was previously created). While this behavior typically ensures consistency, the documentation advises against depending on it, suggesting that future versions or edge cases might alter this behavior.
fix
Avoid relying on `Rule('rule-name')` always returning the identical Python object. Treat rule instantiation as potentially returning a new, but functionally equivalent, object.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'abnf'
The 'abnf' package is not installed in the Python environment.
fix
Install the package using 'pip install abnf'.
NameError: name 'ABNF' is not defined
The 'ABNF' class or module is not imported or defined in the code.
fix
Ensure that 'ABNF' is correctly imported or defined before use.
ImportError: cannot import name 'Rule' from 'abnf'
The 'Rule' class is not available in the 'abnf' module, possibly due to version differences.
fix
Verify the installed version of 'abnf' and consult the documentation for the correct import statement.
from abnf import Rule
This is not an error string, but a common correct import. A common error associated would be `ImportError: cannot import name 'Rule' from 'abnf'` if the package is not found or installed incorrectly, or `NameError: name 'Rule' is not defined` if not imported.
fix
Ensure the 'abnf' package is installed and use `from abnf import Rule` to access the main Rule class.
ParseError: Parsing failed
The input string provided to the parser does not conform to the ABNF grammar defined, or the grammar itself contains errors.
fix
Review your ABNF grammar definition for correctness and ensure the input string strictly adheres to the specified grammar rules.
Upgrade
Version history
2.9.0latest on PyPI · released Aug 27, 2026
Audit
Dependencies
pythonrequiredRequired Python interpreter version.
Agent activity
57 hits · last 30 days
node
48
OpenAI (training)
1
Resources