0

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?

Paul Smith
  • 103
  • 3
  • 2
    What language are you writing this in? Perhaps you might include a partial solution so we know what you are starting with; otherwise, the question is very very broad. – rici Apr 13 '17 at 16:19
  • Also, that grammar generates a language of exactly three strings ("d", "e", and "f"), so it is not a very interesting example. A more interesting example might be a standard expression grammar (with explicit precedence), which raises some interesting questions about what is meant by "random" (or, more accurately, what the expected distribution of results might be). – rici Apr 13 '17 at 18:18

0 Answers0