If a context-free grammar for has start symbol , adjoining a new start symbol and productions gives a context-free grammar for the Kleene star .
To convert a context-free grammar to Chomsky normal form, first add a fresh start symbol. Remove epsilon productions, retaining the exceptional start production only when the empty word is to remain in the language; then remove unit productions and symbols that cannot participate in a terminal derivation. Replace each terminal occurring in a right-hand side of length at least two by a new nonterminal that produces that terminal. Finally split every right-hand side of length greater than two into binary productions using fresh nonterminals.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 1 4H a Solution Created 2026-09-24 Updated 2026-10-05
Let a deterministic finite automaton recognizing the regular language have states , start state , accepting states , and transition function . Construct a context-free grammar with one nonterminal for every state, start symbol , and productionsInduction on the length of a word shows that derives exactly when the automaton reaches from after reading . The terminal production can then end the derivation precisely at an accepting state. Thus the grammar generates exactly the recognized language, proving every regular language is a context-free language. This is actually a right-linear grammar, a special case of a context-free grammar.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 1 4H b Solution Created 2026-09-24 Updated 2026-10-05
Treat the fixed letters , the empty-language and empty-word symbols, and the punctuation of expressions as a finite token alphabet. In the usual syntax allowing parentheses, union, concatenation, and star, the following context-free grammar generates exactly the syntactically valid regular expressions:Structural induction proves that every generated string is an expression and that every expression has a derivation. Grammar ambiguity does not affect the context-free language assertion.
If the language of these expressions were a regular language, its intersection with the regular token language consisting of some opening parentheses, then , then some closing parentheses would be regular. That intersection is exactlyThe pumping lemma for regular languages contradicts regularity: a sufficiently long opening-parenthesis prefix forces a pumpable segment wholly within that prefix, and pumping it changes the two parenthesis counts unequally. Hence the expression syntax is context-free but not regular. This conclusion concerns the strings describing expressions, not the languages those expressions denote; it uses the usual concrete syntax with arbitrary nested parentheses.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 2 4H a i Solution Created 2026-09-24 Updated 2026-10-05
Use . The context-free grammar generates exactly , so it is a context-free language. It is not a regular language: if is a pumping length, any allowed decomposition of has a nonempty pumped segment consisting only of 's. Pumping down leaves fewer 's than 's, contradicting the pumping lemma for regular languages. Thus is a context-free language but not a regular language.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 3 4H a Solution Created 2026-09-24 Updated 2026-10-05
In strict Chomsky normal form, every production of a context-free grammar is or , with nonterminal symbols and a terminal symbol. Such a derivation cannot produce the empty word: it starts with one nonterminal symbol, and every step either increases the symbol count or replaces one symbol by one symbol. The context-free language , generated by , is therefore not generated by any strict CNF grammar.
The convention matters: extended Chomsky normal form permits with excluded from right-hand sides. Under that convention every context-free language has such a grammar, so the requested example would not exist.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 3 4H b Solution Created 2026-09-24 Updated 2026-10-05
Their intersection is . Suppose satisfied the pumping lemma for context-free languages with length . In a decomposition with and , the window cannot meet both the and blocks. Pumping down changes at least one count and leaves at least one count unchanged. Thus the three counts are unequal, or the word order is invalid, contrary to the pumping lemma for context-free languages. Hence CFLs are not closed under intersection.
Past exam of the mathematics course of the University of Cambridge 2017 ii Paper 3 4H c Solution Created 2026-09-24 Updated 2026-10-05
Context-free languages are closed under union: use a new start symbol with alternatives for two renamed context-free grammars. If they were closed under complement in , De Morgan's laws would givecontradicting the preceding intersection example. Therefore some CFL has a non-context-free complement. This is an existence argument; it does not assert that both complements of the particular are non-context-free.
A concrete choice is . Words outside form a regular language. Among words inside it, membership in means the first two counts or the last two counts differ. Each unequal-count comparison has a context-free grammar: for with , use , ; for , replace by . Append any number of 's, or prefix any number of 's for the analogous comparison. Taking these unions gives a context-free language whose complement is the non-context-free equal-three-count language. These productions are definitions of the example, not a restriction to strict Chomsky normal form.
Past exam of the mathematics course of the University of Cambridge 2018 ii Paper 1 4G b ii Solution Created 2026-09-24 Updated 2026-10-03
Past exam of the mathematics course of the University of Cambridge 2018 ii Paper 1 4G c Solution Created 2026-09-24 Updated 2026-10-03
Take a context-free grammar for with start symbol , rename nonterminals if necessary, and add a fresh start symbol withEvery derivation concatenates finitely many words of , and every finite concatenation can be derived. The resulting grammar generates , proving closure of context-free languages under Kleene star.
Past exam of the mathematics course of the University of Cambridge 2018 ii Paper 3 4G a Solution Created 2026-09-24 Updated 2026-10-03
A context-free grammar is in Chomsky normal form when every production rule has one of the formswhere are nonterminal symbols and is a terminal symbol. If the language contains the empty word, one may additionally permit , with the start symbol absent from all right-hand sides.
Past exam of the mathematics course of the University of Cambridge 2019 ii Paper 1 4H b i Solution Created 2026-09-24 Updated 2026-10-03
This language is context-free. It is the language of even-length palindromes over , generated by the context-free grammarEvery derivation adds matching outer symbols, and removing the matching first and last symbols gives the converse induction.
Past exam of the mathematics course of the University of Cambridge 2019 ii Paper 1 4H c Solution Created 2026-09-24 Updated 2026-10-03
Take context-free grammars for and rename their nonterminals so that they are disjoint. If their start symbols are , add a new start symbol and the productionAll productions remain context-free. A derivation from produces a word from followed by a word from , and every such concatenation has a derivation. Thus the new grammar generates , proving closure of context-free languages under concatenation.
Past exam of the mathematics course of the University of Cambridge 2019 ii Paper 3 4H a Solution Created 2026-09-24 Updated 2026-10-03
A context-free grammar is in Chomsky normal form when every production is of one of the formswhere are nonterminal symbols and is a terminal symbol. Under this strict convention there is no epsilon production, so such a grammar cannot generate the empty word. Conversion of an arbitrary grammar therefore givesIf , the two languages are equal. An alternative extended convention permits the exceptional start production ; under that convention conversion preserves the whole language.
Past exam of the mathematics course of the University of Cambridge 2020 ii Paper 3 4F i Solution Created 2026-09-24 Updated 2026-09-29
A context-free grammar is a quadruple consisting of a finite set of nonterminal symbols, a disjoint finite alphabet of terminal symbols, a start symbol , and a finite set of productions with and . A sentence is a terminal word over an alphabet with , and the generated formal language is .
The first language is context-free. For example, the productionsgenerate exactly : each recursive production adds two s to the left and two s to the right.
Past exam of the mathematics course of the University of Cambridge 2020 ii Paper 4 4F Solution Created 2026-09-24 Updated 2026-09-29
A context-free grammar is in Chomsky normal form when every production has the form or , where are nonterminals and is a terminal symbol; one may additionally allow the new start production when the language contains the empty string, provided never appears on a right-hand side.
The standard conversion proceeds as follows.
- Introduce a fresh start symbol and . This changes only when the old start symbol occurs on a right-hand side or when a protected start symbol is needed to preserve .
- Find all nullable nonterminals, add productions obtained by omitting nullable occurrences, and remove the original -productions except the permitted . This leaves unchanged.
- Eliminate each unit production by adding to the non-unit productions reachable through its transitive closure. This leaves unchanged.
- Delete non-generating symbols and then unreachable symbols. This is the only simplification stage that can shrink .
- In every right-hand side of length at least two, replace a terminal by a fresh nonterminal with . New nonterminals are needed exactly for terminals that occur in such mixed or long productions.
- Break every right-hand side of length at least three into binary productions, introducing fresh nonterminals for successive suffixes. New nonterminals are needed exactly when such long productions remain.
The terminal alphabet may be kept unchanged throughout, although unused terminals may be discarded by convention. Every stage preserves the generated language, with the explicit protection of at the start-symbol stage. For example,is already in Chomsky normal form and generates the infinite language , so its conversion may be the grammar itself.
Past exam of the mathematics course of the University of Cambridge 2022 ii Paper 3 4I i Solution Created 2026-09-24 Updated 2026-09-25
A context-free grammar is a quadruple consisting of finite nonterminal and terminal alphabets, a set of productions with one nonterminal on the left, and a start symbol . A context-free language is a language generated by such a grammar.
The pumping lemma for context-free languages says that there is a pumping length such that every in the language with can be writtenwith , , and in the language for every integer .
The language in (i) is not context-free. If it were, pumpThe substring , having length at most , cannot meet both the - and -blocks. If pumping changes the number of 's or 's, those two counts cease to agree. If it changes neither, it changes the number of 's while the outer counts remain , so the middle count ceases to be . Every permitted decomposition therefore fails, contradicting the pumping lemma.
Past exam of the mathematics course of the University of Cambridge 2022 ii Paper 4 4I Solution Created 2026-09-23 Updated 2026-09-25
A context-free grammar is in Chomsky normal form when every production has one of the formswhere are nonterminals and is a terminal. One may additionally allow when the empty word belongs to the language, usually with the restriction that the start symbol does not occur on a right-hand side.
In , the productions for giveConsequentlyIn , and generate and , while again givesSince , the two start productions generate exactlyThusEvery production of has the required binary-nonterminal or single-terminal form, so is the Chomsky-normal-form grammar for the nonempty part of .