0.1.0 |
|
---|
#16 in #cnf
18KB
238 行
-
proc_use [https://crates.io/crates/proc_use[[https://img.shields.io/crates/v/rusty_grammar.svg]]] [https://docs.rs/proc_use[[https://docs.rs/rusty_grammar/badge.svg]]] [https://blog.rust-lang.net.cn/2020/05/07/Rust.1.43.1.html[[https://img.shields.io/badge/proc_use-rustc_1.46.0+-lightgray.svg]]] :PROPERTIES: :CUSTOM_ID: proc_use-latest-versioncrates.io-apidocumentation-proc_use-rustc-1.43.1rust-1.43.1 :END
=rusty_grammar= 是一个库,它使用修改过的CYK算法来定义语法和理解语言。
** 使用场景 :PROPERTIES: :CUSTOM_ID: use-case :END
#+BEGIN_EXAMPLE struct G {}
impl<'grammar> Grammar<'grammar> for G { fn convert(&self) -> Vec<GrammarRule<'grammar>> { let mut rules = Vec::new(); rules.push(GrammarRule{ left_symbol: "ActionSentence", right_symbol: "Verb NounClause | Verb NounClause PrepClause" }); rules.push(GrammarRule{ left_symbol: "NounClause", right_symbol: "Count ANoun | Adjective Noun" }); rules.push(GrammarRule{ left_symbol: "PrepClause", right_symbol: "Prep NounClause" }); rules.push(GrammarRule{ left_symbol: "ANoun", right_symbol: "Adjective Noun" }); rules.push(GrammarRule{ left_symbol: "Adjective", right_symbol: "adjective" }); rules.push(GrammarRule{ left_symbol: "Prep", right_symbol: "prep" }); rules.push(GrammarRule{ left_symbol: "Verb", right_symbol: "verb" }); rules.push(GrammarRule{ left_symbol: "Noun", right_symbol: "noun" }); rules.push(GrammarRule{ left_symbol: "Count", right_symbol: "definiteArticle | indefiniteArticle | number" }); rules } }
struct WB {}
impl WordBank for WB { fn lookup(&self, word: &str) -> &str { match word { "examine" => "verb", "sword" => "noun", "rusty" => "adjective", _ => "dne" } } }
fn main() { let g = G{}; let wb = WB{}; let input = "examine rusty sword"; let cyk: CYK = CYK::new(g, wb); let res = cyk.memoized_parse(input); println!("{}", res); println!("final_res: {:?}", res.get_final()); } #+END_EXAMPLE
虽然一开始需要更多的代码,但现在模块和使用的流程是自动的。将你想要的Rust文件添加到 =util= 中;=use_glob= 方法将自动拾取并导入所有这些文件。要查看使用此目录结构的示例,请参阅 [[https://github.com/Shizcow/proc_use/tree/master/examples/globbing][globbing]]。
依赖关系
~520KB
~10K SLoC