[−][src]Struct liblet::grammar::Grammar
A grammar is the main type of this module
It allows to work on grammar over an easy abstraction. A grammar is a quadruple of:
- n, a set of non terminal symbols
- t, a set of terminal symbols
- p, an ordered collection of productions
- s, a start symbol
You can easily create a grammar from a raw string by specifing the productions involved. The remaining n, t and s elements of the quadruple will be automatically extracted from the productions. The s (start symbol) element will be the left hand side of the first production encountered, which has to be a 1 symbol long length. For further details about the formatting of the raw string, you can have a look at how to specify productions from a raw string in the Production documentation.
Implementations
impl Grammar
[src]
pub fn new<S, P>(n: S, t: S, p: P, s: Symbol) -> Result<Grammar, GrammarError> where
S: IntoIterator<Item = Symbol>,
P: IntoIterator<Item = Production>,
[src]
S: IntoIterator<Item = Symbol>,
P: IntoIterator<Item = Production>,
Return a new grammar based on the quadruple elements passed as arguments.
Errors
Returns an error if any of the strings from n, t collections or s are not valid symbols, specifically:
- WrongNonTerminals if any of the n symbols aren't non terminals
- WrongTerminals if any of the t symbols aren't terminals
- SymbolError if the s symbol is not a non terminal
Examples
use liblet::grammar::Grammar; use liblet::production::production; use liblet::symbol::symbol; // create a grammar let g = Grammar::new( vec![symbol("A")], // one non terminal "A" vec![symbol("a")], // one terminal, "a" vec![production("A","a")], // only one production, A -> a symbol("A") // the start symbol "A" ); assert!(g.is_ok());
pub fn new_from_string<'a, I>(
n: I,
t: I,
p: I,
s: &str
) -> Result<Grammar, GrammarError> where
I: IntoIterator<Item = &'a str>,
[src]
n: I,
t: I,
p: I,
s: &str
) -> Result<Grammar, GrammarError> where
I: IntoIterator<Item = &'a str>,
Return a new grammar based on the quadruple elements passed as arguments. This differ from the new constructor because it allows you to use plain string instead of already built Symbols.
Errors
Returns an error if any of the strings from n, t collections or s can't be transformed into Symbol, specifically:
- WrongNonTerminals if an error occurred in the n collection
- WrongTerminals if an error occurred in the t collection
- SymbolError if an error occurred in the s
Returns a ProductionError if any of the strings from the productions collection can't be transformed into Production.
Examples
use liblet::grammar::Grammar; // create a grammar (same as new constructor) let g = Grammar::new_from_string( vec!["A"], vec!["a"], vec!["A -> a"], "A" ); assert!(g.is_ok());
pub fn n(&self) -> HashSet<Symbol>
[src]
Return the set of non terminal symbols representing the n of the grammar quadruple.
Examples
use liblet::grammar::Grammar; use liblet::production::production; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a grammar let g = Grammar::new_from_string( vec!["A"], vec!["a"], vec!["A -> a"], "A" )?; let n: HashSet<Symbol> = vec![symbol("A")].into_iter().collect(); assert_eq!(g.n(), n);
pub fn t(&self) -> HashSet<Symbol>
[src]
Return the set of terminal symbols representing the t of the grammar quadruple.
Examples
use liblet::grammar::Grammar; use liblet::production::production; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a grammar let g = Grammar::new_from_string( vec!["A"], vec!["a"], vec!["A -> a"], "A" )?; let t: HashSet<Symbol> = vec![symbol("a")].into_iter().collect(); assert_eq!(g.t(), t);
pub fn p(&self) -> Vec<Production>
[src]
Return the ordered collection of productions representing the p of the grammar quadruple.
Examples
use liblet::grammar::Grammar; use liblet::production::{Production,production}; // create a grammar let g = Grammar::new_from_string( vec!["A"], vec!["a"], vec!["A -> a"], "A" )?; let p: Vec<Production> = vec![production("A","a")]; assert_eq!(g.p(), p);
pub fn s(&self) -> Symbol
[src]
Return the start symbol, that is the s of the grammar quadruple.
Examples
use liblet::grammar::Grammar; use liblet::symbol::{Symbol,symbol}; // create a grammar let g = Grammar::new_from_string( vec!["A"], vec!["a"], vec!["A -> a"], "A" )?; let s: Symbol = symbol("A"); assert_eq!(g.s(), s);
pub fn from_string(string: &str) -> Result<Grammar, GrammarError>
[src]
Return a new grammar constructed from the raw string passed as argument.
The process of converting from a raw string to a grammar is defined at the grammar struct documentation level.
Errors
- Returns a NoStartSymbol error if there are no productions in the raw string (since it can not derive a valid start symbol automatically) or if the first production doesn't have a left hand side
- Returns a MultipleStartSymbols error if there are multiple symbols in the first production left hand side
Examples
use liblet::grammar::Grammar; // create a grammar let g = Grammar::from_string("A -> a"); // n = {A} // t = {a} // p = [A -> a] // s = {A} assert!(g.is_ok());
pub fn alternatives<I>(&self, symbols: I) -> Vec<Vec<Symbol>> where
I: IntoIterator<Item = Symbol>,
[src]
I: IntoIterator<Item = Symbol>,
Return a collection of productions right hand side for each production which left hand side matches the given ordered collection of symbols.
Examples
use liblet::grammar::Grammar; use liblet::symbol::{Symbol,symbol}; // create a grammar let g = Grammar::from_string("A -> a | b")?; // find alternatives for left hand side "A" let alternatives = g.alternatives(vec![symbol("A")]); assert_eq!(alternatives.len(), 2); assert_eq!(alternatives[0], vec![symbol("a")]); assert_eq!(alternatives[1], vec![symbol("b")]);
pub fn restrict_to<I>(&self, symbols: I) -> Result<Grammar, GrammarError> where
I: IntoIterator<Item = Symbol>,
[src]
I: IntoIterator<Item = Symbol>,
Return a new grammar restricted to have only the symbols in the collection passed as argument.
So every element in the new grammar quadruple can only have the passed symbols inside, everything else is deleted. This does not affect the original grammar.
Errors
Returns a NoStartSymbol error if the restrict operation would delete the start symbol from the original grammar, since it can't automatically choose another start symbol.
Examples
use liblet::grammar::Grammar; use liblet::production::production; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a grammar let g = Grammar::from_string("A -> a | b")?; // restrict the grammar to a new one with only the "A" and "a" symbols let g_restricted = g.restrict_to(vec![symbol("A"),symbol("a")])?; // we except a single non terminal symbol "A" let expected_n: HashSet<Symbol> = vec![symbol("A")].into_iter().collect(); // we except the terminal symbols "A" and "a", but not "b" let expected_t: HashSet<Symbol> = vec![symbol("a")].into_iter().collect(); // we except the only production "A -> a", but not "A -> b" let expected_p = vec![production("A","a")]; assert_eq!(g_restricted.n(), expected_n); assert_eq!(g_restricted.t(), expected_t); assert_eq!(g_restricted.p(), expected_p); assert_eq!(g_restricted.s(), symbol("A")); // the start symbol is still the same
pub fn productives(&self) -> HashSet<Symbol>
[src]
Return a set of symbols (from the grammar symbols) which are productives.
That is, only the symbols that have some production rule (or production rule chains) that transform that symbol to a non terminal one. The set of productives symbols contains the terminal ones, since they are the products themself.
Examples
use liblet::grammar::Grammar; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a grammar let g = Grammar::from_string(" A -> a | C B -> b ")?; // let's define the expected productives, "C" should not be productive let expected: HashSet<Symbol> = vec![ symbol("A"), symbol("B"), symbol("a"), symbol("b"), ].into_iter().collect(); assert_eq!(g.productives(), expected);
pub fn reachable(&self) -> HashSet<Symbol>
[src]
Return a set of symbols (from the grammar symbols) which are reachable from the start symbol of the grammar.
That is, only symbols that can be reached by some production rule (or production rule chains).
Examples
use liblet::grammar::Grammar; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a grammar let g = Grammar::from_string(" A -> a | b B -> b ")?; // let's define the expected rachable, "B" should not be reachable let expected: HashSet<Symbol> = vec![ symbol("A"), symbol("a"), symbol("b"), ].into_iter().collect(); assert_eq!(g.reachable(), expected);
pub fn reachable_from<I>(&self, reached: I) -> HashSet<Symbol> where
I: IntoIterator<Item = Symbol>,
[src]
I: IntoIterator<Item = Symbol>,
Return a set of symbols (from the grammar symbols) which are reachable from the set of symbols passed as arguments.
It has the same behaviour of the reachable method but the symbols to search from are here passed as input.
Examples
use liblet::grammar::Grammar; use liblet::symbol::{Symbol,symbol}; use std::collections::HashSet; // create a grammar let g = Grammar::from_string(" A -> a | b B -> b ")?; // let's define the expected rachable, "A" and "a" should not be reachable // if we start from "B" let expected: HashSet<Symbol> = vec![ symbol("B"), symbol("b"), ].into_iter().collect(); assert_eq!(g.reachable_from(vec![symbol("B")]), expected);
Trait Implementations
impl Clone for Grammar
[src]
impl Debug for Grammar
[src]
impl<'de> Deserialize<'de> for Grammar
[src]
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
[src]
__D: Deserializer<'de>,
impl Display for Grammar
[src]
impl Eq for Grammar
[src]
impl PartialEq<Grammar> for Grammar
[src]
impl Serialize for Grammar
[src]
fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error> where
__S: Serializer,
[src]
__S: Serializer,
impl StructuralEq for Grammar
[src]
impl StructuralPartialEq for Grammar
[src]
impl<'_> TryFrom<&'_ Grammar> for Automaton<Symbol>
[src]
type Error = AutomatonError
The type returned in the event of a conversion error.
fn try_from(g: &Grammar) -> Result<Self, Self::Error>
[src]
impl<'_> TryFrom<&'_ str> for Grammar
[src]
Auto Trait Implementations
impl RefUnwindSafe for Grammar
impl Send for Grammar
impl Sync for Grammar
impl Unpin for Grammar
impl UnwindSafe for Grammar
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,