A language that can be generated by a CFG.
A language is context-free iff there exists a CFG such that it generates . All regular languages are context-free.
Properties
If and are CFLs, then the following are also CFLs:
- Union
(using ) - Concatenation
(using ) - Kleene star
(using )
Intersection and complementation are not necessarily CFLs.
Intersection
is not a CFL (pumping lemma for CFLs), yet it equals:
Both operands are CFLs. So CFLs are not closed under intersection.
Complementation
CFLs are closed under union. If they were also closed under complementation, De Morgan’s law gives:
Closure under both union and complementation would force closure under intersection. Since intersection is not closed, complementation is not closed.
Examples
Palindrome
Language of palindromes over . are palindromes.
If is palindrome then and are palindromes.
This language is not regular, but context-free.
a^n b^n
Language of strings with equal number of a’s followed by b’s.
Grammar:
This language is not regular, but context-free.
a^n b^n c^n
Language of strings with equal number of a’s followed by b’s and c’s. Not context free. Intersection of 2 CFLs.
Syntax of Programming Languages
Programming languages (such as Python, Java) have syntax that can be described by CFGs. Used in compiler design to parse code and check for syntax errors.