Context-Free Grammar Sentence Generator
A context-free grammar says nothing about meaning, only about shape — which is why one will happily produce a sentence that is perfectly grammatical and completely absurd. That gap is the point, and you get at it by writing the rules yourself. Type them here, one to a line, and every sentence derived from them is then parsed back through the same rules before it is shown: a parser has to find a way of expanding your start symbol that consumes exactly those words and nothing more.
What this generator does
Expands your start symbol by repeatedly replacing each rule name with one of its alternatives until only words remain, then runs a recogniser over the finished sentence to confirm your grammar derives exactly it. Before any of that it reads the rules and reports what would go wrong: a name used but never defined, a rule nothing can reach, and above all a rule that can never finish because every way out of it leads back in.
How to use this tool
- Write your rules, one to a line: a name, an arrow, and alternatives separated by a bar.
- Start a rule name with a capital letter. Anything else is a literal word.
- Derive some sentences. If a rule cannot work, the page says which one and why before it runs.
- Read the expansions under each sentence to see how it was built.
Understanding the controls
- Your rules
- One a line, as a name, an arrow and alternatives separated by a bar. Three arrows are accepted and anything after a # is ignored. A rule written twice adds alternatives rather than replacing it.
- How many sentences
- Between 1 and 12, each derived independently from the same rules.
- Repeat word
- The word the sentences were built from. It goes into the address bar, so the link gives the same sentences back — alongside the same rules, which the address does not carry and you would paste in yourself.
Common use cases
- Trying out a grammar you are writing, and finding out immediately when a rule can never finish
- Producing example sentences for a grammar, parsing or linguistics lesson
- Showing the difference between syntax and meaning with rules a class wrote themselves
- Generating placeholder prose with a structure you control
- Producing test input for a parser from a grammar you already have
How this generator works
Deriving and parsing are opposite operations, so doing both is the only honest way to say the rules really produce what you are looking at. Three things about a grammar somebody wrote are worked out before it runs, and each of them is decidable rather than a matter of judgement. Whether a rule can ever finish is a least fixed point — some alternative of it is all words, or all its names finish — and a rule that never appears can never become a sentence. Whether a name is used but never defined follows from the capital-letter convention. And a rule that can begin with itself is refused rather than handled, because the recogniser that checks the sentences would go round for ever on one, and a checker that says “not derivable” about a derivable sentence would be worse than a page that declines the grammar.
Randomness and fairness
Alternatives are chosen from a repeat word taken from your browser’s secure generator unless you type one of your own, and that word goes into the address bar so a link brings the same sentences back. A repeat word is for reproducing a set of examples; it carries no cryptographic strength and nothing here belongs anywhere that needs one.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- A rule that can begin with itself is refused. Write the plain case first and the repeat at the end — Phrase -> word | word and Phrase — which produces the same sentences and can be checked.
- There are no agreement rules, because a context-free grammar cannot express them. A grammar that produces “a kettles” is behaving correctly and is telling you something true about what this kind of grammar can say.
- Empty alternatives are not supported, so a rule cannot expand to nothing.
- A recursive grammar has no longest sentence, so past a couple of dozen words this page starts taking the quickest way out of every remaining rule. That is a limit on the printing rather than on the grammar.
- Rules stay in your browser and are not carried in the link, so sharing a set of sentences means sharing the rules alongside it.
- Twenty rules, eight alternatives each, eight symbols an alternative.
Privacy and your data
The rules you write and the sentences derived from them stay in your browser: nothing you type is transmitted, stored or measured. The repeat word is a short label this page minted or you typed, and it is the only thing that appears in the address.
Related generators
- Sentence Starter GeneratorA sentence-starter fragment to kick-start a piece of creative writing.
- Random Word GeneratorA word (or several) for writing prompts, brainstorming sessions and word games.
- Constructed Language Word GeneratorDeclare your own consonants, vowels, endings and syllable shapes, and get a starter vocabulary in which every word can be taken apart to prove it obeys them.
- Grammar Practice Sentence GeneratorA sentence containing a grammar mistake, ready to spot and correct.