Crates.io | bnf |
lib.rs | bnf |
version | 0.5.0 |
source | src |
created_at | 2017-10-28 21:19:51.186022 |
updated_at | 2023-03-31 13:00:25.299857 |
description | A library for parsing Backus–Naur form context-free grammars |
homepage | https://github.com/shnewto/bnf |
repository | https://github.com/shnewto/bnf |
max_upload_size | |
id | 37278 |
size | 124,748 |
A library for parsing Backus–Naur form context-free grammars.
The following grammar from the Wikipedia page on Backus-Naur form exemplifies a compatible grammar. (*Note: parser allows for an optional ';' to indicate the end of a production)
<postal-address> ::= <name-part> <street-address> <zip-part>
<name-part> ::= <personal-part> <last-name> <opt-suffix-part> <EOL>
| <personal-part> <name-part>
<personal-part> ::= <initial> "." | <first-name>
<street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL>
<zip-part> ::= <town-name> "," <state-code> <ZIP-code> <EOL>
<opt-suffix-part> ::= "Sr." | "Jr." | <roman-numeral> | ""
<opt-apt-num> ::= <apt-num> | ""
Take the following grammar for DNA sequences to be input to this library's
parse
function.
<dna> ::= <base> | <base> <dna>
<base> ::= "A" | "C" | "G" | "T"
The output is a Grammar
object representing a tree that looks like this:
Grammar {
productions: [
Production {
lhs: Nonterminal(
"dna"
),
rhs: [
Expression {
terms: [
Nonterminal(
"base"
)
]
},
Expression {
terms: [
Nonterminal(
"base"
),
Nonterminal(
"dna"
)
]
}
]
},
Production {
lhs: Nonterminal(
"base"
),
rhs: [
Expression {
terms: [
Terminal(
"A"
)
]
},
Expression {
terms: [
Terminal(
"C"
)
]
},
Expression {
terms: [
Terminal(
"G"
)
]
},
Expression {
terms: [
Terminal(
"T"
)
]
}
]
}
]
}
Once the Grammar
object is populated, to generate a random sentence from it
call the object's generate function. grammar.generate()
. For the above grammar
you could expect something like TGGC
or AG
.
If the generate function can't find a production for a nonterminal it tries
to evaluate it will print the identifer as a nonterminal, i.e. <identifier>
.
The generate function will return an error if it detects an infinite loop caused
by a production such as <PATTERN> ::= <PATTERN>
.
use bnf::Grammar;
let input =
"<postal-address> ::= <name-part> <street-address> <zip-part>
<name-part> ::= <personal-part> <last-name> <opt-suffix-part> <EOL>
| <personal-part> <name-part>
<personal-part> ::= <initial> '.' | <first-name>
<street-address> ::= <house-num> <street-name> <opt-apt-num> <EOL>
<zip-part> ::= <town-name> ',' <state-code> <ZIP-code> <EOL>
<opt-suffix-part> ::= 'Sr.' | 'Jr.' | <roman-numeral> | ''
<opt-apt-num> ::= <apt-num> | ''";
let grammar: Result<Grammar, _> = input.parse();
match grammar {
Ok(g) => println!("{:#?}", g),
Err(e) => println!("Failed to make grammar from String: {}", e),
}
use bnf::Grammar;
let input =
"<dna> ::= <base> | <base> <dna>
<base> ::= 'A' | 'C' | 'G' | 'T'";
let grammar: Grammar = input.parse().unwrap();
let sentence = grammar.generate();
match sentence {
Ok(s) => println!("random sentence: {}", s),
Err(e) => println!("something went wrong: {}!", e)
}
use bnf::Grammar;
let input =
"<dna> ::= <base> | <base> <dna>
<base> ::= 'A' | 'C' | 'G' | 'T'";
let grammar: Grammar = input.parse().unwrap();
let sentence = "GATTACA";
let mut parse_trees = grammar.parse_input(sentence);
match parse_trees.next() {
Some(parse_tree) => println!("{}", parse_tree),
_ => println!("Grammar could not parse sentence"),
}