Struct parol::grammar::cfg::Cfg [−][src]
Expand description
Context free grammar type
Fields
st: String
Start symbol of the grammar
pr: Vec<Pr>
Set of productions
Implementations
Set of Non-terminals, ordered alphabetically.
Set of Non-terminals, ordered by occurrence Start symbol comes first
Set of Terminals - ordered by occurrence. Used for Lexer generation.
Terminal positions within the grammar Used for Nt grammar graphs
Non-terminal positions within the grammar Used for Nt grammar graphs
Calculates all productive non-terminals.
use parol::{Cfg, Pr, Symbol};
use std::collections::BTreeSet;
let mut g = Cfg::default();
assert!(g.productive_non_terminals().is_empty());
let g = Cfg::with_start_symbol("S'")
.add_pr(Pr::new("S'", vec![Symbol::n("S")]))
.add_pr(Pr::new("S", vec![Symbol::t("a", vec![0]), Symbol::n("X")]))
.add_pr(Pr::new("X", vec![Symbol::t("b", vec![0]), Symbol::n("S")]))
.add_pr(Pr::new("X", vec![Symbol::t("a", vec![0]), Symbol::n("Y"), Symbol::t("b", vec![0]), Symbol::n("Y")]))
.add_pr(Pr::new("Y", vec![Symbol::t("b", vec![0]), Symbol::t("a", vec![0])]))
.add_pr(Pr::new("Y", vec![Symbol::t("a", vec![0]), Symbol::n("Z")]))
.add_pr(Pr::new("Z", vec![Symbol::t("a", vec![0]), Symbol::n("Z"), Symbol::n("X")]));
let productive = g.productive_non_terminals();
assert_eq!(["S'".to_owned(), "S".to_owned(), "X".to_owned(), "Y".to_owned()].iter().cloned().collect::<BTreeSet<String>>(), productive);
Calculates all non-productive non-terminals.
use parol::{Cfg, Pr, Symbol};
use std::collections::BTreeSet;
use std::convert::From;
let mut g = Cfg::default();
assert!(g.productive_non_terminals().is_empty());
let g = Cfg::with_start_symbol("S'")
.add_pr(Pr::new("S'", vec![Symbol::n("S")]))
.add_pr(Pr::new("S", vec![Symbol::t("a", vec![0]), Symbol::n("X")]))
.add_pr(Pr::new("X", vec![Symbol::t("b", vec![0]), Symbol::n("S")]))
.add_pr(Pr::new("X", vec![Symbol::t("a", vec![0]), Symbol::n("Y"), Symbol::t("b", vec![0]), Symbol::n("Y")]))
.add_pr(Pr::new("Y", vec![Symbol::t("b", vec![0]), Symbol::t("a", vec![0])]))
.add_pr(Pr::new("Y", vec![Symbol::t("a", vec![0]), Symbol::n("Z")]))
.add_pr(Pr::new("Z", vec![Symbol::t("a", vec![0]), Symbol::n("Z"), Symbol::n("X")]));
let productive = g.unproductive_non_terminals();
assert_eq!(["Z".to_owned()].iter().cloned().collect::<BTreeSet<String>>(), productive);
Detects whether all non-terminals are productive
Returns a vector of production references with the LHS matching the given non-terminal n
Calculates all nullable non-terminals.
use parol::{Cfg, Pr, Symbol};
use std::collections::BTreeSet;
use std::convert::From;
let g = Cfg::with_start_symbol("S")
.add_pr(Pr::new("S", vec![Symbol::n("Y")]))
.add_pr(Pr::new("Y", vec![Symbol::n("U"), Symbol::n("Z")]))
.add_pr(Pr::new("Y", vec![Symbol::n("X"), Symbol::t("a", vec![0])]))
.add_pr(Pr::new("Y", vec![Symbol::t("b", vec![0])]))
.add_pr(Pr::new("U", vec![Symbol::n("V")]))
.add_pr(Pr::new("U", vec![]))
.add_pr(Pr::new("X", vec![Symbol::t("c", vec![0])]))
.add_pr(Pr::new("V", vec![Symbol::n("V"), Symbol::t("d", vec![0])]))
.add_pr(Pr::new("V", vec![Symbol::t("d", vec![0])]))
.add_pr(Pr::new("Z", vec![]))
.add_pr(Pr::new("Z", vec![Symbol::n("Z"), Symbol::n("X")]));
let productive = g.calculate_nullable_non_terminals();
assert_eq!(
[
"S".to_owned(),
"U".to_owned(),
"Y".to_owned(),
"Z".to_owned()
].iter().cloned().collect::<BTreeSet<String>>(),
productive);
Trait Implementations
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
Auto Trait Implementations
impl RefUnwindSafe for Cfg
impl UnwindSafe for Cfg
Blanket Implementations
Mutably borrows from an owned value. Read more
Compare self to key
and return true
if they are equal.