Struct bnf::Grammar [] [src]

pub struct Grammar { /* fields omitted */ }

A Grammar is comprised of any number of Productions

Methods

impl Grammar
[src]

[src]

Construct a new Grammar

[src]

Construct an Grammar from Productions

[src]

[src]

Add Production to the Grammar

[src]

Remove Production from the Grammar

[src]

Get iterator of the Grammar's Productions

[src]

Get mutable iterator of the Grammar's Productions

[src]

Generate a random sentence from self and seed for random. Use if interested in reproducing the output generated. Begins from lhs of first production.

Example

extern crate bnf;
extern crate rand;
use rand::{SeedableRng, StdRng};
use bnf::Grammar;

fn main() {
    let input =
        "<dna> ::= <base> | <base> <dna>
        <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
    let grammar = Grammar::from_str(input).unwrap();
    let seed: &[_] = &[1,2,3,4];
    let mut rng: StdRng = SeedableRng::from_seed(seed);
    let sentence = grammar.generate_seeded(&mut rng);
    match sentence {
        Ok(s) => println!("random sentence: {}", s),
        Err(e) => println!("something went wrong: {}!", e)
    }

}

[src]

Generate a random sentence from self. Begins from lhs of first production.

Example

extern crate bnf;
use bnf::Grammar;

fn main() {
    let input =
        "<dna> ::= <base> | <base> <dna>
        <base> ::= \"A\" | \"C\" | \"G\" | \"T\"";
    let grammar = Grammar::from_str(input).unwrap();
    let sentence = grammar.generate();
    match sentence {
        Ok(s) => println!("random sentence: {}", s),
        Err(e) => println!("something went wrong: {}!", e)
    }

}

Trait Implementations

impl PartialEq for Grammar
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

[src]

This method tests for !=.

impl Debug for Grammar
[src]

[src]

Formats the value using the given formatter.

impl Clone for Grammar
[src]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl Display for Grammar
[src]

[src]

Formats the value using the given formatter. Read more

impl FromStr for Grammar
[src]

The associated error which can be returned from parsing.

[src]

Parses a string s to return a value of this type. Read more