I'm messing about with generative context-free grammars in Python.
The generator itself is in gen_grammar.py
, and the test grammar used is
in grammar.json
.
$ ./gen_grammar.py grammar.json
will generate a single sentence from
the given grammar.
The grammar format is JSON, and is fairly simple. Look at the
included grammr.json
file for an example.
Grammars must have an S
symbol, which is used to start the process. After
that, you can name your symbols how you like. Considering this simple grammar:
{
"S": [
{"weight": 0.6, "production": ["a", {"var": "S"}, "a"]},
{"weight": 0.4, "production": ["b"]}
]
}
Formally, it may be defined as:
S → a S a | b
The weight parameter affects how likely a given rule is to be chosen. All rules
for a given symbol must have weights in order for weighting to be used, else
a simple random.choice()
is used.
Please note that there are no safeguards as to recursion depth.