0% found this document useful (0 votes)
110 views3 pages

Notes CFL PDF

This document contains notes from a theory of computation class about context-free languages. It provides an overview of context-free grammars and the model of computation for context-free languages. It also discusses how to prove that a language is non-context-free using the pumping lemma for context-free languages. The schedule for upcoming class meetings and exams is listed at the top.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views3 pages

Notes CFL PDF

This document contains notes from a theory of computation class about context-free languages. It provides an overview of context-free grammars and the model of computation for context-free languages. It also discusses how to prove that a language is non-context-free using the pumping lemma for context-free languages. The schedule for upcoming class meetings and exams is listed at the top.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

cs302 Theory of Computation UVa Spring 2008

Notes: Context-Free Languages


Thursday, 14 February

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


grammar. Show why aaabbc could not be derived by your grammar.

Model of Computation for CFGs


First, we describe the model of computation for CFGs using a function notation (not the
traditional ⇒ notation).
Note that the grammar rules may have the same variable on the left side of may rules
in R, so we cannot interpret R as a function. Instead, we define the function δ which
captures the set of all right sides of rules for a given variable. The transition function
δ : V → P((V ∪ Σ)∗ ) (note the powerset operator - the output is a set of (V ∪ Σ)∗ strings)
is defined by:
δ(A) = {α|α ∈ (V ∪ Σ)∗ ∧ A → α ∈ R}

NCFL-1
Then, as with DFAs, we can define the extended transition function δ ∗ recursively:
[
δ ∗ (α) = {α} ∪ δ ∗ (β)
β∈δ(α)

A string w is in G = (V, Σ, R, S) iff w ∈ δ ∗ (S).


Derivation. A more traditional way to define the model of computation for CFGs is using
derivation. A grammar G derives a string w if there is a way to produce w starting from S
following the rules in R. S ⇒∗ w means G derives w. We define the ⇒∗ function somewhat
similarly to the δ ∗ .
First, we define ⇒, the one step derivation function in terms of R, the rules of the CFG:

If A → γ is in R, then αAβ ⇒ αγβ for α, β, γ ∈ (V ∪ Σ)∗ .

That is, if there is a rule A → γ in R, anywhere A appears in a sequence of variables and


symbols, we can replace the A with γ, leaving the rest of the string unchanged.
Now, we can define ⇒∗ : (V ∪ Σ)∗ → (V ∪ Σ)∗ , to mean that there is some way to produce
the right side following zero of more steps starting from the input string (we can think of
⇒∗ as outputing a set of strings, but define it using just single strings on the output side;
the actual value of derives(α) is the set of all strings:

α ⇒∗ α — any string derives itself (no replacements done).


α ⇒∗ γ if α ⇒∗ β and β ⇒ γ — if we can go from α to β in zero or more steps,
and from β to γ in one step, then we can derive γ from α.

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:

S ⇒∗ uRz ⇒∗ uvRyz ⇒∗ uvxyz

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}∗ }.

Assume D is a context-free language. Then, there must be a CFG G that pro-


duces D, and the pumping lemma for context-free languages applies with
pumping length p. As with pumping lemma for regular languages, we need
to find one string w where |w| ≥ p, and show that it cannot be pumped.

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

You might also like