BNF example generator

The Generator

Enter BNF and generate examples

The BNF language

In this simple generator, a variety of BNF is used which is defined as follows:

Distributions

To provide better control on the generated data, you can also control the random distribution used. This is specified on the same line as the rule, following a #

The BNF definition of BNF

The BNF definition

The random generator program

The program bnf2sent.cgi generates sentences randomly using a grammar defined using BNF. Starting with the first rule in the sequence, rules are expanded. Where there is a selection, a choice is made randomly between the alternatives. Where there is an iteration, the iteration will be repeated a random number of times (actually Binomial with a probability of 0.5 by default). As the sentence is expanded, words are matched to rules. If there is no matching rule for a word, the word itself is printed (as a terminal). If a rule is found (a non-terminal) the associated rule is then used in further expansion.

A version of this program can run in batch to generate a text file. This enables test data to be generated, perhaps to load a database to test for performance.

Examples

Some example BNF definitions