Formal Language Expander

After laughing at TheSpark's BoneEasy(TM), I thought I'd play with this idea myself.

This script expands an unrestricted grammar (although you can make it recurse infinitely until it quits with an exception). My girlfriend and I had a lot of fun making rule files for it.

The file format is very easy:

A --> x
B --> y
.
.
.

Where A and B are non-terminals, and x and y are arbitrary combinations of terminals and non-terminals. In plain English, A and B are words to be replaced by the strings x and y respectively. To mark something to be replaced, you surround it with curly braces.

e.g.

SENTENCE --> {QUESTION}
SENTENCE --> {STATEMENT}
QUESTION --> Where's the {CONSUMABLE}?
STATEMENT --> It's in the {STORAGE_UNIT}.
CONSUMABLE --> cheese
CONSUMABLE --> sauce
STORAGE_UNIT --> fridge
STORAGE_UNIT --> cupboard

Here's an example execution:

% ./expand.rb -7 spark.txt
Don't make me come over there.
If my wart is radiant, will you eagerly penetrate me?
Do you have a sister who isn't so much like you?
You'll do.
I know you get this a lot, but where do those armpits of yours end?
I'll think about you when I massage my nipples tonight.
Do you have a name, or just a hernia, baby?

Download

expand.rb (Ruby script)
spark.txt (TheSpark rule file)
love.txt (Gooey romance rule file)

Please Sir...

I'm busy working on a new version of this, suitable for inclusion in Shade. The new version features the ability to assign probabilities to each expansion and associate each with a set of qualifiers to codify consistency across iterations in the expansion.