ABNF Parsers
raw JSON → 2.4.1 verified Thu May 14 auth: no python
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 Common errors
error ModuleNotFoundError: No module named 'abnf' ↓
cause The 'abnf' package is not installed in the Python environment.
fix
Install the package using 'pip install abnf'.
error NameError: name 'ABNF' is not defined ↓
cause The 'ABNF' class or module is not imported or defined in the code.
fix
Ensure that 'ABNF' is correctly imported or defined before use.
error ImportError: cannot import name 'Rule' from 'abnf' ↓
cause 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.
error from abnf import Rule ↓
cause 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. error ParseError: Parsing failed ↓
cause 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.
Warnings
breaking Version 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.
gotcha As 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.
gotcha When 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++).
gotcha The 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.
Install compatibility last tested: 2026-05-14 v2.4.0 installed · v2.5.0 latest
python os / libc status wheel install import disk mem side effects
3.10 alpine (musl) wheel - 0.11s 18.2M 4.0M clean
3.10 alpine (musl) - - 0.13s 18.2M 3.9M -
3.10 slim (glibc) wheel 1.5s 0.08s 19M 4.0M clean
3.10 slim (glibc) - - 0.09s 19M 3.9M -
3.11 alpine (musl) wheel - 0.14s 20.1M 4.6M clean
3.11 alpine (musl) - - 0.18s 20.1M 4.5M -
3.11 slim (glibc) wheel 1.6s 0.11s 21M 4.6M clean
3.11 slim (glibc) - - 0.14s 21M 4.5M -
3.12 alpine (musl) wheel - 0.15s 12.0M 5.2M clean
3.12 alpine (musl) - - 0.16s 11.9M 5.1M -
3.12 slim (glibc) wheel 1.5s 0.15s 12M 5.2M clean
3.12 slim (glibc) - - 0.20s 12M 5.1M -
3.13 alpine (musl) wheel - 0.12s 11.7M 5.2M clean
3.13 alpine (musl) - - 0.15s 11.6M 5.1M -
3.13 slim (glibc) wheel 1.5s 0.12s 12M 5.2M clean
3.13 slim (glibc) - - 0.16s 12M 5.1M -
3.9 alpine (musl) wheel - 0.10s 17.7M 3.8M clean
3.9 alpine (musl) - - 0.12s 17.7M 3.8M -
3.9 slim (glibc) wheel 1.8s 0.08s 18M 3.8M clean
3.9 slim (glibc) - - 0.11s 18M 3.8M -
Imports
- Rule
from abnf import Rule - rfc7232
from abnf.grammars import rfc7232
Quickstart
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}")