A Rust library for parsing Backus–Naur form context-free grammars and generating random sentences from them.
bnf is a Rust library for parsing Backus–Naur form (BNF) context-free grammar definitions. It converts BNF text into a structured grammar representation, allowing developers to generate random sentences that follow the grammar's rules and validate input strings against those rules. The library supports extended syntax like groups and optionals, which are normalized into standard BNF form.
Developers and researchers working with formal grammars, language parsing, compiler construction, or procedural content generation who need a Rust-based tool to manipulate and test BNF grammars.
bnf offers a pure Rust implementation with a simple API for parsing, generating, and validating BNF grammars, including support for extended syntax. It provides deterministic normalization and built-in infinite loop detection, making it reliable for grammar experimentation and testing.
Parse BNF grammar definitions
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Parses parenthesized groups and optionals, normalizing them into standard BNF with automatic infinite loop detection, as shown in the README examples.
Provides idiomatic Rust interfaces with straightforward methods like parse, generate, and build_parser, making integration seamless.
Allows parsing to start from any nonterminal using parse_input_starting_with, enabling targeted validation beyond the first rule.
Generates random sentences from grammar rules with built-in checks for infinite loops, useful for testing and procedural content.
Formatting the grammar does not preserve original parentheses or optionals, using anonymous nonterminals like __anon0, which obscures the intended grammar design.
Only supports BNF and basic extensions; lacks full EBNF features or support for other grammar types, as admitted in the normalization approach.
Focuses on parsing and generation without tools for grammar optimization, semantic actions, or performance tuning for large-scale use.