ebnf-to-bnf 0.1.0

A customizable converter from EBNF into BNF grammars
Documentation
  • Coverage
  • 0%
    0 out of 17 items documented0 out of 8 items with examples
  • Size
  • Source code size: 14.88 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.43 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 26s Average build duration of successful builds.
  • all releases: 26s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • alejandro-vaz/ebnf-to-bnf
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • alejandro-vaz

ebnf-to-bnf

A small Rust library that converts EBNF grammars into BNF grammars.

The crate expands common EBNF constructs such as:

  • Parentheses ( … )
  • Repetition *
  • One-or-more +
  • Optional ?

into equivalent BNF rules.

Installation

Add the crate to your Cargo.toml:

[dependencies]
ebnf-to-bnf = "0.1"

Example

use ebnf_to_bnf::ToBNF;

fn main() {
    let grammar = r#"
Expr ::= Term (Sign Term)*
Term ::= Factor (Operator Factor)*
"#;

    let bnf = grammar.to_bnf().unwrap();

    println!("{bnf}");
}

Example output (simplified):

Term_op_Sign_Term_cl_mul ::= Sign Term
Term_Term_op_Sign_Term_cl_mul_mul ::= Term_op_Sign_Term_cl_mul Term_Term_op_Sign_Term_cl_mul_mul |
Expr ::= Term Term_Term_op_Sign_Term_cl_mul_mul
Factor_op_Operator_Factor_cl_mul ::= Operator Factor
Factor_Factor_op_Operator_Factor_cl_mul_mul ::= Factor_op_Operator_Factor_cl_mul Factor_Factor_op_Operator_Factor_cl_mul_mul |
Term ::= Factor Factor_Factor_op_Operator_Factor_cl_mul_mul

Naming of new productions can be modified (if that seems too long to you):

$1 ::= Sign Term
$2 ::= $1 $2 |
Expr ::= Term $2
$3 ::= Operator Factor
$4 ::= $3 $4 |
Term ::= Factor $4

Options

The conversion can be customized using Options.

Supported configuration includes:

  • input rule delimiter (::=, :, or ->)
  • output rule delimiter
  • comment style (// or #)
  • generated rule naming scheme

Example:

use ebnf_to_bnf::{Options, ToBNF};

let grammar = "A ::= B+";

let bnf = grammar.to_bnf_with_options(Options {..}).unwrap();

Trait

Any type that implements Borrow<str> can be converted:

trait ToBNF {
    fn to_bnf(&self) -> Option<String>;
    fn to_bnf_with_options(&self, options: Options) -> Option<String>;
}

This means you can call it on:

  • &str
  • String
  • &String

License

MIT