def generate_random(grammar=E_, s='<STR_LIT:S>'):
import random<EOL>def rewrite(tokens, into):<EOL><INDENT>for token in tokens:<EOL><INDENT>if token in grammar.rules:<EOL><INDENT>rewrite(random.choice(grammar.rules[token]), into)<EOL><DEDENT>elif token in grammar.lexicon:<EOL><INDENT>into.append(random.choice(grammar.lexicon[token]))<EOL><DEDENT>else:<EOL><INDENT>into.append(token)<EOL><DEDENT><DEDENT>return into<EOL><DEDENT>return '<STR_LIT:U+0020>'.join(rewrite(s.split(), []))<EOL>
Replace each token in s by a random entry in grammar (recursively).
    This is useful for testing a grammar, e.g. generate_random(E_)