/** Here is another examle of generating something from a grammar, but this * time it'll be a story patterned after "Old Mother Hubbard". Here is our * grammar: * S --> "Old Mother Hubbard went to the cupboard to fetch her poor dog * a bone. But when she came there the cupboard was bare, and so * the poor dog had none." A * A --> e | BA * B --> "She went to the" P "to buy him a" T * P --> "baker's" | "tavern" | "grocer's" | "tailor's" | "cobbler's" | * "chemist's" * T --> "shoe" | "hat" | "cigar" | "beer" | "cake" | "suit" | "tire" */ public class Story1 { public static void main(String [] args) { String S = "Old Mother Hubbard went to the cupboard\n" + "To fetch her poor dog a bone;\n" + "But when she came there the cupboard was bare,\n" + "And so the poor dog had none.\n\n" + A(); System.out.println(S); } // Let's have a 80% chance of continuing the story! public static String A() { if (Math.random() < .2) return ""; else return B() + A(); } public static String B() { return ""; } public static String P() { return ""; } public static String T() { return ""; } }