CYK algorithm 2026-10-03
The CYK algorithm decides membership in a context-free language given by a grammar in Chomsky normal form. It records, for every substring, the nonterminals that derive that substring and combines adjacent substrings using binary productions. For a word of length and a fixed grammar, it runs in time.
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 ii Solution Created 2026-09-24 Updated 2026-10-05
Use . A halting algorithm checks that a word consists of three consecutive blocks in the correct order and compares their lengths, so is a decidable language. If it were a context-free language, apply the pumping lemma for context-free languages to . The segment containing both pumped pieces has length at most , so it cannot meet both the and blocks. Pumping down therefore changes at least one block count but leaves another equal to , or destroys the required block order. Either way the resulting word is not in , a contradiction. Thus is a decidable language but not a context-free language.
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 a Solution Created 2026-09-24 Updated 2026-10-03
The pumping lemma for context-free languages states that every context-free language has a pumping length such that every with can be writtenwith , , and for every integer .
Past exam of the mathematics course of the University of Cambridge 2019 ii Paper 1 4H a Solution Created 2026-09-24 Updated 2026-09-29
The pumping lemma for context-free languages states that for every context-free language there is a pumping length such that every with can be writtenwith
Past exam of the mathematics course of the University of Cambridge 2020 ii Paper 3 4F ii Solution Created 2026-09-24 Updated 2026-09-29
The language is not context-free. Suppose it had pumping length in the pumping lemma for context-free languages, and apply the lemma to . Write with , , and for every .
Let and be the respective numbers of s and s in . If , pumping with or destroys equality of the two block lengths. Hence a valid decomposition would require . Pumping with would then produce , where . Butso is not a square number. This contradiction proves the square-count language is not context-free.
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 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 2026 ii Paper 1 12I b Solution Created 2026-09-24 Updated 2026-09-25
A type-2 rule is with one nonterminal on the left; a type-2 grammar and its language are context-free languages. The required repetition criterion is the pumping lemma for context-free languages.
Past exam of the mathematics course of the University of Cambridge 2026 ii Paper 1 12I d Solution Created 2026-09-24 Updated 2026-10-03
No production introduces , so the two rules containing are unreachable. The effective derivations areThus the language is , which is not context-free language, although the displayed grammar itself also visibly has non-type-2 left sides.
Unary context-free language is regular 2026-10-03
Every context-free language over a one-symbol alphabet is a regular language. Equivalently, the set of word lengths of a unary context-free language is ultimately periodic. This follows, for example, from the one-dimensional case of Parikh's theorem.