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:
- each rule is written on a separate line.
- each rule is in the form of :
term = expression
- an expression can be :
- a sequence separated by spaces, representing a sequence
- a sequence separated by | , representing a selection
- a single word enclosed in { }, representing an iteration.
- a word used in a expression which has a matching rule is called a non-terminal. A non-terminal may have only one matching rule.
- a word used in an expression which does NOT have a matching rule, or is enclosed in ' ' or " " is a terminal.
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