I've a grammar that contains mutually recursive rules. For example:
S -> A
A -> B | C | "d"
B -> "e" | A
C -> A | C | "f"
I want to generate random strings from this grammar by recurisvely following its rules. When I encounter an alternative, I pick at random. The problem is, I want to stop after generating some number of A/B/C productions. I think I need to throw an exception at some point to backtrack and collect the partially unfolded loop, but how do I do that exactly? What information do I need to keep track of and when to throw?