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:
[]
= "0.1"
Example
use ToBNF;
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 ;
let grammar = "A ::= B+";
let bnf = grammar.to_bnf_with_options.unwrap;
Trait
Any type that implements Borrow<str> can be converted:
This means you can call it on:
&strString&String
License
MIT