7 unstable releases
0.4.2  Aug 20, 2023 

0.4.1 

0.4.0  Sep 5, 2022 
0.3.2  Sep 4, 2022 
0.1.0  Sep 3, 2022 
#137 in Testing
52 downloads per month
Used in mexe
20KB
308 lines
glc
This crate's aim is to generate random expressions based on a contextfree grammar.
The acronym stands for "gramática livre de contexto" (contextfree grammar).
How to Use
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 nonterminals: "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 az
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());
A simplified version of the macro above is available:
// 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 nonterminal 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 nonterminal 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"
};
For a reallife example take a look at mexe.
Links
Dependencies
~300KB