A context-free language is a language that can be generated by a context-free grammar (CFG).
Definition
Context-free Language
Let be a language. Then:
Inherently Ambiguous Language
A context-free language is inherently ambiguous if every CFG that generates it is ambiguous.
Theorems
T1: Every regular language is context-free
Let be a language.
Example
Note
#todo https://williamumboh.com/comp30026-2025/cfg/#regular-languages-are-context-free
In fact, we can restrict CF languages’ production rules to ensure they have no recursion to get them to degenerate to regular languages.
T:3 Every CF language can be recognised by a PDA
Let be a context-free language. Then there exists a pushdown automata that can accept all the words in the language:
T2: Context-free Languages are closed under union ^t2
Let be two context-free languages. Then, the union (with respect to languages), is also context-free:
Proof
Let and be two context-free languages (over the same alphabet ) generated by and .
Then and . If we manage to construct a grammar that generates , then that is good enough to show that the language is context-free.
Let be this new grammar. It would need to have variables and production rules from both languages plus some additional stuff, and this would be very similar to proving the union of regular languages are regular.
Then:
- and also the new production rules and .
- Again, this is similar to using epsilon transitions when proving regularity of a union of languages.
- is the starting variable for
This grammar then generates word from both and and hence proves is context-free.
T4: Context-free languages are closed under concatenation ^t4
Let be two regular languages. Then, the operation of concatenation preserves regularity:
T5: Context-free languages are closed under Kleene star ^t5
Let be a regular language. Then, the Kleene star operation preserves regularity:
#todo https://williamumboh.com/comp30026-2025/cfg/#closure-properties
make proofs
T6: More context-free-preserving operations #TODO ^t6
The following operations all preserve regularity:
- Reversal