--[[ Input (grammar) looks like this: Apology ::= my (cat|mouse|monkey) ate my homework. Apology ::= our (basement|attic|third floor) collapsed from an overload of school books. ]] -- Put the grammar into array "rules". rules = splitIntoLines(getSnippet("#229")) -- Choose one of the rules (randomly). rule = chooseOne(rules) -- Parse rule: Apology ::= bla = match(rule, "Apology ::= ") -- Parse bla into: -- #1 (...) -- #2 everything else i = rule:find("([^)]*)" With every s in #1 do: -- Split inner part of s at "|" into "options". options = split(inner, "|") -- Choose one of the options (randomly). option = chooseOne(options) -- Replace s with option. -- Print bla (with the replacements made). print(join(bla))