7 个不稳定版本
0.4.2 | 2023年8月20日 |
---|---|
0.4.1 |
|
0.4.0 | 2022年9月5日 |
0.3.2 | 2022年9月4日 |
0.1.0 | 2022年9月3日 |
#174 in 测试
49 每月下载量
用于 mexe
20KB
308 行
glc
该 Crates 的目标是生成基于上下文无关文法的随机表达式。
该缩写代表 "gramática livre de contexto" (上下文无关文法)。
如何使用
use glc::{Grammar, t_or_rule, nt_seq_rule};
let grammar = Grammar(
// starting symbol
"S".into(),
// vector of rules
vec![
// a rule that generates a sequence of non-terminals: "A B"
nt_seq_rule!("S" => "A", "B"),
nt_seq_rule!("B" => "A", "B", "N"),
nt_seq_rule!("B" => "E"),
t_or_rule!("E" => ""),
// a rule that is an "or" of terminals: any letter from a-z
t_or_rule!(
"A" => "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z"
),
t_or_rule!("N" => "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"),
],
)
// generate a random string with this grammar
println!("{}", grammar.gen());
上面宏的简化版是可用的
// You may need to tune this parameter depending on how large is your grammar,
// since the `grammar` macro is recursive.
#![recursion_limit = "256"]
use glc::grammar;
let _grammar = grammar!{
// The first non-terminal seen (head of the 1st rule) will be
// the starting symbol (in this case: `S`).
S => A B;
B => A B N;
B => E;
E => "";
// Or transform a non-terminal in one among many terminals
A => "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z";
N => "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
};
对于现实生活中的示例,请参阅 mexe。
链接
依赖
~315KB