#expression #random #grammar #generator #context-free

glc

Generate a random expression based on a Context Free Grammar

6 releases (3 breaking)

Uses new Rust 2021

0.4.0 Sep 5, 2022
0.3.2 Sep 4, 2022
0.2.0 Sep 4, 2022
0.1.0 Sep 3, 2022

#143 in Testing

Download history 100/week @ 2022-08-31 54/week @ 2022-09-07 22/week @ 2022-09-14

176 downloads per month
Used in mexe

MIT license

13KB
191 lines

glc

This crate's aim is to generate random expressions based on a context-free grammar.

The acronym stands for "gramática livre de contexto" (context-free grammar).

How to Use

use glc::{Grammar, nt, t_or_rule, nt_seq_rule};

fn main() {
    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());
}

For a real-life example take a look at mexe.

Links

Dependencies

~320KB