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. Pseudo-code: Put the grammar into array "rules". Choose one of the rules (randomly). Parse rule: Apology ::= Parse bla into: #1 (...) #2 everything else With every s in #1 do: Split inner part of s at "|" into "options". Choose one of the options (randomly). Replace s with option. Print bla (with the replacements made).