Notes CFL PDF
Notes CFL PDF
Upcoming Schedule
Monday, 18 February: Office Hours (Olsson 236A, 2-3pm); Help Session (Ols-
son 228E, 5:30-6:30pm)
Monday, 18 February (3:30pm): Annie Anton, North Carolina State University,
Designing Legally Compliant Software Systems that Contain Sensitive Informa-
tion (Department Colloquim in Olsson 009)
Tuesday, 19 February (2:02pm): Problem Set 3
Thursday, 28 February: Exam 1 (in class)
Context-Free Grammars
A grammar is context-free if all production rules have the form: A → αγβ (that is, the left
side of a rule can only be a single variable; the right side is unrestricted and can be any
sequence of terminals and variables).
We can define a grammar as a 4-tuple (V, Σ, R, S) where V is a finite set (variables), Σ is a
finite set (terminals), S is the start variable, and R is a finite set of rules, each of which is a
mapping V → (V ∪ Σ)∗ .
Example. (Similar to Sipser’s Exercise 2.9) Give a context-free grammar that generates
the language ai bj ck |i = j or i = k where i, j, k ≥ 0 . Show how abbc is derived by your
NCFL-1
Then, as with DFAs, we can define the extended transition function δ ∗ recursively:
[
δ ∗ (α) = {α} ∪ δ ∗ (β)
β∈δ(α)
The string w is in the language defined by the context-free grammar G = (V, Σ, R, S) iff:
S ⇒∗ w
Proving Non-Context-Freeness
To show a language is not context-free, we need to prove there is no Context-Free Grammar
that can generate the language. The strategy is similar to how we used the pumping lemma
to show a language is non-regular. The pumping lemma for context-free languages gives
us a property that must be true of any context-free language. We get a contradiction, but
showing that there is no way to satisfy the properties of the pumping lemma for the given
(non-context-free) language.
NCFL-2
Pumping lemma for context-free languages. For any context-free language A, there is
a pumping length p where all strings s ∈ A with |s| ≥ p may be divided into 5 pieces,
s = uvxyz satsifying these conditions:
1. for each i ≥ 0, uv i xy i z ∈ A
2. |vy| > 0
3. |vxy| ≤ p
Suppose there is a CFG G that generates A. Then any string s ∈ A can be derived using
G. Since G is a context-free grammar, each production rule has a single variable on the
left side. That means in a derivation of k steps (where each step involves replacing one
variable with the right hand side of a corresponding rule) if k ≥ |V | then some variable
R ∈ V must be replaced twice:
The first replacement is R ⇒∗ vRy, which can be repeated any number of times, producing
v i Ry i .
Example. D = {ww|w ∈ {0, 1}∗ }.
Pick w =
Show that all possible ways of dividing w = uvxyz fail to satisfy the pumping
lemma for CFLs requirements.
Tricky Example. Is X = {w|w ∈ {0, 1}∗ ∧ there is no z ∈ {0, 1}∗ such that w = zz} context-
free?
NCFL-3