Lectures Examples and Solutions of CFG&RE
Lectures Examples and Solutions of CFG&RE
6- Write CFG for L(G)= { a^n b^n c^m | n,m>=0 } Over ∑={a,b,c}
L= {λ,abc,aabbc,abccc,…}
S-> AB
A->aAb|λ
B->cB|λ
7- Describe in English the following grammar
S-> aS|Sb|b
This grammar generates:
- Set of a’s followed by b
- a’s followed by set of b’s
- one b
8- Describe in English :
S-> aS|bB
B->aB|λ
bB->bb
21- Write RE for all sentences over ∑={a,b,c} , such that all sentences has one c .
In CFG
S-> NcN
N->aN|bN|λ
In regular grammar
S-> aS|bS|cA
A-> aA|bA|λ
22- Write the same sentences in the last example but contain at least 2’c adjacent.
L = {accb,bcaccb,acbbccccbac,…}
RE = ( a ∪ b ∪ c )* c c ( a ∪ b ∪ c )*
In CFG
S-> NccN
N-> aN|bN|cN|λ
23- Write a CFG over Z={a,b} , such that each b followed by a , and # of a’s are twice the # of b’s .
L={ baa,aba,aababa,…}
S-> aSba| baSa|SS|λ
To simplify
L(G)= a^m b^n bb c^m cc
S-> aSc|A
A->B bb cc
B->bB|λ
Solution2
S-> aSc|bbx
x-> bx|cc
26- L(G) = a* a+ b* b+
Simplify = L(G)= a+ b+
CFG
S->AB
A-> aA|a
B->bA|b
Converting CFGs to CNF (Chomsky Normal Form)
Richard Cole
October 17, 2007
iv. The start symbol S may appear only on the left hand side of rules.
Given a CFG G, we show how to convert it to a CNF grammar G0 generating the same
language.
We use a grammar G with the following rules as a running example.
S → ASA | aB; A → B | S; B → b |
We proceed in a series of steps which gradually enforce the above CNF criteria; each step
leaves the generated language unchanged.
Step 1 For each terminal a, we introduce a new variable, Ua say, add a rule Ua → a, and
for each occurrence of a in a string of length 2 or more on the right hand side of a rule,
replace a by Ua . Clearly, the generated language is unchanged.
Example: If we have the rule A → Ba, this is replaced by Ua → a, A → BUa .
This ensures that terminals on the right hand sides of rules obey criteria (i) above.
This step changes our example grammar G to have the rules:
S → ASA | Ua B; A → B | S; B → b | ; Ua → a
1
2
Step 2 For each rule with 3 or more variables on the righthand side, we replace it with a
new collection of rules obeying criteria (ii) above. Suppose there is a rule U → W1 W2 · · · Wk ,
for some k ≥ 3. Then we create new variables X2 , X3 , · · · , Xk−1 , and replace the prior rule
with the rules:
S → AX | Ua B; X → SA; A → B | S; B → b | ; Ua → a
Step 3 We replace each occurrence of the start symbol S with the variable S 0 and add the
rule S → S 0 . This ensures criteria (iv) above.
This step changes our example grammar G to have the rules:
S → S 0 ; S 0 → AX | Ua B; X → S 0 A; A → B | S 0 ; B → b | ; Ua → a
Step 4 This step removes rules of the form A → , as follows. First, we determine
all variables that can generate in one or more moves. We explain how to do this two
paragraphs down. Then for each such variable A, for each occurrence of A in a 2-variable
right hand side, we create a new rule with the A omitted; i.e. if there is a rule C → AB we
create the new rule C → B, and if there is a rule C → DA we create the new rule C → D
(if there is a rule C → AA, we create the new rule C → A). Then we remove all rules of the
form A → , apart from S → , if present (i.e. we keep rule S → , if present).
The new rules serve to shortcut a previously generatable instances of , i.e. if previously
we had used a rule A → BC, and then in a series of steps had generated from B, which
has the net effect of generating C from A, we could instead do this directly by applying the
new rule A → C. Consequently, the generated language is unchanged.
To find the variables that can generate , we use an iterative rule reduction procedure.
First, we make a copy of all the rules. We then modify the rules by removing from the right
hand sides all instances of variables A for which there is a rule A → . We keep iterating
this procedure so long as it creates new reduced rules with on the right hand side. (An
efficient implementation keeps track of the lengths of each right hand side, and a list of the
locations of each variable; the new rules with on the right hand side are those which have
newly obtained length 0. It is not hard to have this procedure run in time linear in the sum
of the lengths of the rules.)
This step changes our example grammar G to have the rules:
3
S → S 0 ; S 0 → AX | X | Ua B; X → S 0 A | S 0 ; A → B | S 0 ; B → b; Ua → a
Step 5 This step removes rules of the form A → B, which we call unit rules. We form
the directed graph defined by these rules, i.e. for each rule A → B, we create a directed
edge (A, B). For each strong component in this graph, we replace the variables it contains
with a single one of these variables in all the rules in which these variables occur. So if
U1 , U2 , · · · , Uk form a strong component (and so any one of these variables can be replaced,
in a sequence of applications of unit rules, by any other of these variables) then we replace
every occurrence of Ui , 2 ≤ i ≤ k with U1 in every rule in which they occur.
In the example grammar, the one non-trivial strong component contains the variables
{S , X}. We replace S 0 with X yielding the rules:
0
S → X; X → AX | X | Ua B; X → XA | X; A → B | X; B → b; Ua → a
We can remove the unnecessary rule X → X also.
Next, we traverse the resulting acyclic graph, in reverse topological order (i.e. starting at
nodes with no outedges and working back from these nodes); for each traversed edge (E, F ),
which corresponds to a rule E → F , for each rule F → CD, we add the rule E → CD,
and then remove the rule E → F . Any derivation which had used the rules E → F and
F → CD in turn can now use the rule E → CD instead. So the same strings are derived
with the new set of rules. (This can be implemented via a depth first search on the acyclic
graph.)
This step changes our example grammar G to have the rules:
Steps 4 and 5 complete the observance of criteria (ii), and thereby creates a CNF grammar
generating the same language as the original grammar.
CS 360
Naomi Nishimura
State elimination
Note: this information is meant to cover some material used in class but absent from the
textbook. This is not intended to be comprehensive or a replacement for attending lecture.
This handout is based on material developed by Jeff Shallit for CS 360, in turn based on
material developed by Eric Bach of the University of Wisconsin.
In Section 3.2.2 of the textbook, an algorithm is given for constructing a regular expression
from a DFA. The algorithm presented here (and in class) is simpler to understand, and applies
to NFA’s and -NFA’s as well.
As in the textbook, we will remove states from the automaton, replacing labels of arcs, so
that in the end a single regular expression is formed. The single regular expression will be the
label on an arc that goes from the start state to the accepting state, and this will be the only
arc in the automaton.
The algorithm forms the simpler automaton as follows. In step 1, we modify the automaton
to have a start state that is not an accepting state and has no transitions in (either self-loops or
from other states). In step 2, we create an equivalent automaton that has a single accepting state
with no transitions out. These will be the two states that remain at the end of the algorithm.
In step 3, the other states are eliminated, in any order. Details of the algorithm follow, along
with a running example, illustrated below.
b
b
a b
1 2 3 4
!
a a
Step 1
If the start state is an accepting state or has transitions in, add a new non-accepting start
state and add an -transition between the new start state and the former start state.
b
b
a b
1 2 3 4
!
a a
! a
0
CS 360: State elimination 2
Step 2
If there is more than one accepting state or if the single accepting state has transitions out,
add a new accepting state, make all other states non-accepting, and add an -transition from
each former accepting state to the new accepting state.
b
b
a b
1 2 3 4
!
a a
!
!
!
0 0
5
Step 3
For each non-start non-accepting state in turn, eliminate the state and update transitions
according to the procedure given on page 99 of the textbook, Figures 3.7 and 3.8. The following
illustrations depict the removal of states 1, 2, 3, and 4 in that order.
b
b b
aa
2 ab + ! 3 4
!
a
!
0 0
5
CS 360: State elimination 3
b
b
b + a(aa)∗ (ab + !)
3 4
a + a(aa)∗ (ab + !)
!
!
0 0
5
0 0
5
((b + a(aa)∗ (ab + !))b∗ b)((a + a(aa)∗ (ab + !))b∗ b)∗ (! + (a + a(aa)∗ (ab + !))b∗ )
0 0
5
CS 208: Automata Theory and Logic
Lecture 6: Context-Free Grammar
Ashutosh Trivedi
b a a
b
Department of Computer Science and Engineering,
Indian Institute of Technology Bombay.
Ashutosh Trivedi – 1 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Grammars
Pushdown Automata
Properties of CFLs
Ashutosh Trivedi – 2 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Grammars
Noam Chomsky
(linguist, philosopher, logician, and activist)
“ A grammar can be regarded as a device that enumerates the sentences of a language. We
study a sequence of restrictions that limit grammars first to Turing machines, then to two
types of systems from which a phrase structure description of a generated language can be
drawn, and finally to finite state Markov sources (finite automata). ”
Ashutosh Trivedi – 3 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Grammars
A (formal) grammar consists of
1. A finite set of rewriting rules of the form
φ→ψ
Ashutosh Trivedi – 4 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Grammars
A (formal) grammar consists of
1. A finite set of rewriting rules of the form
φ→ψ
S → AB (1)
A → C (2)
CB → Cb (3)
C → a (4)
Ashutosh Trivedi – 5 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Examples
S → AB (1)
A → C (2)
CB → Cb (3)
C → a (4)
Ashutosh Trivedi – 5 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Examples
We can derive the phrase “John comes” from this grammar in the
following way:
Ashutosh Trivedi – 6 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Types of Grammars
Depending on the rewriting rules we can characterize the grammars in the
following four types:
1. type 0 grammars with no restriction on rewriting rules;
2. type 1 grammars have the rules of the form
αAβ → αγβ
A→γ
A → aB or A → a
Ashutosh Trivedi – 9 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Languages: Syntax
Definition (Context-Free Grammar)
A context-free grammar is a tuple G = (V, T, P, S) where
– V is a finite set of variables (nonterminals, nonterminals vocabulary);
– T is a finite set of terminals (letters);
– P ⊆ V × (V ∪ T)∗ is a finite set of rewriting rules called productions,
– We write A → β if (A, β) ∈ P;
– S ∈ V is a distinguished start or “sentence” symbol.
Ashutosh Trivedi – 10 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Languages: Syntax
Definition (Context-Free Grammar)
A context-free grammar is a tuple G = (V, T, P, S) where
– V is a finite set of variables (nonterminals, nonterminals vocabulary);
– T is a finite set of terminals (letters);
– P ⊆ V × (V ∪ T)∗ is a finite set of rewriting rules called productions,
– We write A → β if (A, β) ∈ P;
– S ∈ V is a distinguished start or “sentence” symbol.
S → ε
S → 0S1
– S = S.
Ashutosh Trivedi – 10 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Languages: Semantics
Derivation:
– Let G = (V, T, P, S) be a context-free grammar.
– Let αAβ be a string in (V ∪ T)∗ V(V ∪ T)∗
– We say that αAβ yields the string αγβ, and we write αAβ⇒αγβ if
A → γ is a production rule in G.
α → α1 → α2 · · · αn → β.
Ashutosh Trivedi – 11 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Languages: Semantics
Derivation:
– Let G = (V, T, P, S) be a context-free grammar.
– Let αAβ be a string in (V ∪ T)∗ V(V ∪ T)∗
– We say that αAβ yields the string αγβ, and we write αAβ⇒αγβ if
A → γ is a production rule in G.
α → α1 → α2 · · · αn → β.
Ashutosh Trivedi – 11 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
CFG: Example
S → ε
S → 0S1
– S = S.
∗
The string 000111 ∈ L(G0n 1n ), i.e. S =
⇒ 000111 as
Ashutosh Trivedi – 12 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Prove that 0n 1n is accepted by the grammar G0n 1n .
Ashutosh Trivedi – 13 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
CFG: Example
Consider the following grammar G = (V, T, P, S) where
– V = {E, I}; T = {a, b, 0, 1}; S = E; and
– P is defined as
E → I | E + E | E ∗ E | (E)
I → a | Ia | Ib | I0 | I1
∗
The string (a1 + b0 ∗ a1) ∈ L(G), i.e. E =
⇒ (a1 + b0 ∗ a1) as
∗
E ⇒ (E) ⇒ (E + E) ⇒ (I + E) ⇒ (I1 + E) ⇒ (a1 + E) =
⇒ (a1 + b0 ∗ a1).
Ashutosh Trivedi – 14 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
CFG: Example
Consider the following grammar G = (V, T, P, S) where
– V = {E, I}; T = {a, b, 0, 1}; S = E; and
– P is defined as
E → I | E + E | E ∗ E | (E)
I → a | Ia | Ib | I0 | I1
∗
The string (a1 + b0 ∗ a1) ∈ L(G), i.e. E =
⇒ (a1 + b0 ∗ a1) as
∗
E ⇒ (E) ⇒ (E + E) ⇒ (I + E) ⇒ (I1 + E) ⇒ (a1 + E) =
⇒ (a1 + b0 ∗ a1).
∗
E ⇒ (E) ⇒ (E + E) ⇒ (E + E ∗ E) ⇒ (E + E ∗ I) =
⇒ (a1 + b0 ∗ a1).
Ashutosh Trivedi – 14 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
CFG: Example
Consider the following grammar G = (V, T, P, S) where
– V = {E, I}; T = {a, b, 0, 1}; S = E; and
– P is defined as
E → I | E + E | E ∗ E | (E)
I → a | Ia | Ib | I0 | I1
∗
The string (a1 + b0 ∗ a1) ∈ L(G), i.e. E =
⇒ (a1 + b0 ∗ a1) as
∗
E ⇒ (E) ⇒ (E + E) ⇒ (I + E) ⇒ (I1 + E) ⇒ (a1 + E) =
⇒ (a1 + b0 ∗ a1).
∗
E ⇒ (E) ⇒ (E + E) ⇒ (E + E ∗ E) ⇒ (E + E ∗ I) =
⇒ (a1 + b0 ∗ a1).
S → AS | ε.
S → aa | ab | ba | bb
Ashutosh Trivedi – 15 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Parse Trees
Ashutosh Trivedi – 16 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Parse Trees
Ashutosh Trivedi – 16 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Parse Trees
Ashutosh Trivedi – 16 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Parse Tree
Given a grammar G = (V, T, P, S), the parse trees associated with G has
the following properties:
1. Each interior node is labeled by a variable in V.
2. Each leaf is either a variable, terminal, or ε. However, if a leaf is ε it is
the only child of its parent.
3. If an interior node is labeled A and has children labeled X1 , X2 , . . . , Xk
from left-to-right, then
A → X1 X2 . . . Xk
Ashutosh Trivedi – 17 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Reading exercise
Ashutosh Trivedi – 18 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
In-class Quiz
Ashutosh Trivedi – 19 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Syntactic Ambiguity in English
Ashutosh Trivedi – 20 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Syntactic Ambiguity in English
—Anthony G. Oettinger
Ashutosh Trivedi – 20 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Grammars
Pushdown Automata
Properties of CFLs
Ashutosh Trivedi – 21 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pushdown Automata
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
1, X 7→ 1X 1, 1 7→ ε
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2 1
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
1
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2 1
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
0
1
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2 1
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
0
1
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2 1
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
1
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2 1
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2 1
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
1
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Example 1: L = {ww : w ∈ {0, 1}∗ }
input tape 1 1 1 0 0 1 1 1
pushdown stack
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
⊥
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 23 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pushdown Automata
0, X 7→ 0X 0, 0 7→ ε
ε, X 7→ X ε, ⊥ 7→ ⊥
start q0 q1 q2
1, X 7→ 1X 1, 1 7→ ε
Ashutosh Trivedi – 24 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Semantics of a PDA
– Let P = (Q, Σ, Γ, δ, q0 , ⊥, F) be a PDA.
– A configuration (or instantaneous description) of a PDA is a triple
(q, w, γ) where
– q is the current state,
– w is the remaining input, and
– γ ∈ Γ∗ is the stack contents, where written as concatenation of symbols
form top-to-bottom.
– We define the operator ` (derivation) such that if (p, α) ∈ δ(q, a, X)
then
(q, aw, Xβ) ` (p, w, αβ),
for all w ∈ Σ∗ and β ∈ Γ∗ . The operator ⊥∗ is defined as transitive
closure of ⊥ in straightforward manner.
– A run of a PDA P = (Q, Σ, Γ, δ, q0 , ⊥, F) over an input word w ∈ Σ∗ is
a sequence of configurations
Ashutosh Trivedi – 26 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Semantics: acceptance via final states
Ashutosh Trivedi – 26 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Semantics: acceptance via empty stack
Ashutosh Trivedi – 27 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Semantics: acceptance via empty stack
Is L(P) = N(P)?
Ashutosh Trivedi – 27 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Equivalence of both notions
Theorem
For every language defind by a PDA with empty stack semantics, there exists a
PDA that accept the same language with final state semantics, and vice-versa.
Proof.
– Final state to Empty stack
– Add a new stack symbol, say ⊥0 , as the start stack symbol, and in the
first transition replace it with ⊥⊥0 before reading any symbol.
(How? and Why?)
– From every final state make a transition to a sink state that does not read
the input but empties the stack including ⊥0 .
Ashutosh Trivedi – 28 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Equivalence of both notions
Theorem
For every language defind by a PDA with empty stack semantics, there exists a
PDA that accept the same language with final state semantics, and vice-versa.
Proof.
– Final state to Empty stack
– Add a new stack symbol, say ⊥0 , as the start stack symbol, and in the
first transition replace it with ⊥⊥0 before reading any symbol.
(How? and Why?)
– From every final state make a transition to a sink state that does not read
the input but empties the stack including ⊥0 .
– Empty Stack to Final state
– Replace the start stack symbol ⊥0 and ⊥⊥0 before reading any symbol.
(Why?)
– From every state make a transition to a new unique final state that does
not read the input but removes the symbol ⊥0 .
Ashutosh Trivedi – 28 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Formal Construction: Empty stack to Final State
Ashutosh Trivedi – 29 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Formal Construction: Final State to Empty Stack
Ashutosh Trivedi – 30 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Expressive power of CFG and PDA
Theorem
A language is context-free if and only if some pushdown automaton accepts it.
Proof.
1. For an arbitrary CFG G give a PDA PG such that L(G) = L(PG ).
Ashutosh Trivedi – 31 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Expressive power of CFG and PDA
Theorem
A language is context-free if and only if some pushdown automaton accepts it.
Proof.
1. For an arbitrary CFG G give a PDA PG such that L(G) = L(PG ).
– Leftmost derivation of a string using the stack
– One state PDA accepting by empty stack
– Proof via a simple induction over size of an accepting run of PDA
Ashutosh Trivedi – 31 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Expressive power of CFG and PDA
Theorem
A language is context-free if and only if some pushdown automaton accepts it.
Proof.
1. For an arbitrary CFG G give a PDA PG such that L(G) = L(PG ).
– Leftmost derivation of a string using the stack
– One state PDA accepting by empty stack
– Proof via a simple induction over size of an accepting run of PDA
2. For an arbitrary PDA P give a CFG GP such that L(P) = L(GP ).
Ashutosh Trivedi – 31 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Expressive power of CFG and PDA
Theorem
A language is context-free if and only if some pushdown automaton accepts it.
Proof.
1. For an arbitrary CFG G give a PDA PG such that L(G) = L(PG ).
– Leftmost derivation of a string using the stack
– One state PDA accepting by empty stack
– Proof via a simple induction over size of an accepting run of PDA
2. For an arbitrary PDA P give a CFG GP such that L(P) = L(GP ).
– Modify the PDA to have the following properties such that each step is
either a “push” or “pop”, and has a single accepting state and the stack
is emptied before accepting.
– For every state pair of P define a variable Apq in PG generating strings
such that PDA moves from state p to state q starting and ending with
empty stack.
– Three production rules
Ashutosh Trivedi – 31 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From CFGs to PDAs
Ashutosh Trivedi – 32 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From CFGs to PDAs
I → a | b | Ia | Ib | I0 | I1
E → I | E ∗ E | E + E | (E).
Ashutosh Trivedi – 32 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From CFGs to PDAs
Theorem
We have that w ∈ N(P) if and only if w ∈ L(G).
Proof.
– (If part). Suppose w ∈ L(G). Then w has a leftmost derivation
Ashutosh Trivedi – 33 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From CFGs to PDAs
Theorem
We have that w ∈ N(P) if and only if w ∈ L(G).
Proof.
– (Only If part). Suppose w ∈ N(P), i.e. (q, w, S) `∗ (q, ε, ε).
We show that if (q, x, A) `∗ (q, ε, ε) then A ⇒∗ x by induction over
number of moves taken by P.
– Base case. x = ε and (q, ε) ∈ δ(q, ε, A). It follows that A → ε is a
production in P.
– inductive step. Let the first step be A → Y1 Y2 . . . Yk . Let x1 x2 . . . xk be the
part of input to be consumed by the time Y1 . . . Yk is popped out of the
stack.
It follows that (q, xi , Yi ) `∗ (q, ε, ε), and from inductive hypothesis we
get that Yi ⇒ xi if Yi is a variable, and Yi = xi is Yi is a terminal. Hence,
we conclude that A ⇒∗ x.
Ashutosh Trivedi – 33 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From PDAs to CFGs
Ashutosh Trivedi – 34 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From PDAs to CFGs
Ashutosh Trivedi – 34 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From PDAs to CFGs
Theorem
If Ap,q ⇒∗ x then x can bring the PDA P from state p on empty stack to state q on
empty stack.
Proof.
We prove this theorem by induction on the number of steps in the
derivation of x from Ap,q .
– Base case. If Ap,q ⇒∗ x in one step, then the only rule that can
generate a variable free string in one step is Ap,p → ε.
– Inductive step. If Ap,q ⇒∗ x in n + 1 steps. The first step in the
derivation must be Ap,q → Ap,r Ar,q or Ap,q → a Ar,s b.
– If it is Ap,q → Ap,r Ar,q , then the string x can be broken into two parts x1 x2
such that Ap,r ⇒∗ x1 and Ar,q ⇒∗ x2 in at most n steps. The theorem
easily follows in this case.
– If it is Ap,q → aAr,s b, then the string x can be broken as ayb such that
Ar,s ⇒∗ y in n steps. Notice that from p on reading a the PDA pushes a
symbol X to stack, while it pops X in state s and goes to q.
Ashutosh Trivedi – 35 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
From CFGs to PDAs
Theorem
If x can bring the PDA P from state p on empty stack to state q on empty stack
then Ap,q ⇒∗ x.
Proof.
We prove this theorem by induction on the number of steps the PDA takes
on x to go from p on empty stack to q on empty stack.
– Base case. If the computation has 0 steps that it begins and ends with
the same state and reads ε from the tape. Note that Ap,p ⇒∗ ε since
Ap,p → ε is a rule in P.
– Inductive step. If the computation takes n + 1 steps. To keep the stack
empty, the first step must be a “push” move, while the last step must
be a “pop” move. There are two cases to consider:
– The symbol pushed in the first step is the symbol popped in the last step.
– The symbol pushed if the first step has been popped somewhere in the
middle.
Ashutosh Trivedi – 36 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Context-Free Grammars
Pushdown Automata
Properties of CFLs
Ashutosh Trivedi – 37 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Deterministic Pushdown Automata
Ashutosh Trivedi – 38 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Deterministic Pushdown Automata
Ashutosh Trivedi – 38 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Deterministic Pushdown Automata
Theorem
Every regular language can be accepted by a deterministic pushdown automata
that accepts by final states.
Ashutosh Trivedi – 38 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Deterministic Pushdown Automata
Theorem
Every regular language can be accepted by a deterministic pushdown automata
that accepts by final states.
Ashutosh Trivedi – 38 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Chomsky Normal Form
A → BC
A → a.
Ashutosh Trivedi – 39 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Chomsky Normal Form
A → BC
A → a.
Ashutosh Trivedi – 39 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
For every context-free language L there exists a constant p (that depends on L)
such that
for every string z ∈ L of length greater or equal to p,
there is an infinite family of strings belonging to L.
Ashutosh Trivedi – 40 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
For every context-free language L there exists a constant p (that depends on L)
such that
for every string z ∈ L of length greater or equal to p,
there is an infinite family of strings belonging to L.
Ashutosh Trivedi – 40 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
For every context-free language L there exists a constant p (that depends on L)
such that
for every string z ∈ L of length greater or equal to p,
there is an infinite family of strings belonging to L.
Ashutosh Trivedi – 40 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
Let L be a CFL. Then there exists a constant n such that if z is a string in L of
length at least n, then we can write z = uvwxy such that i) |vwx| ≤ n, ii) vx 6= ε,
and iii) for all i ≥ 0 the string uvi wxi y ∈ L.
Ashutosh Trivedi – 41 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
Let L be a CFL. Then there exists a constant n such that if z is a string in L of
length at least n, then we can write z = uvwxy such that i) |vwx| ≤ n, ii) vx 6= ε,
and iii) for all i ≥ 0 the string uvi wxi y ∈ L.
Ashutosh Trivedi – 41 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
Let L be a CFL. Then there exists a constant n such that if z is a string in L of
length at least n, then we can write z = uvwxy such that i) |vwx| ≤ n, ii) vx 6= ε,
and iii) for all i ≥ 0 the string uvi wxi y ∈ L.
Ashutosh Trivedi – 41 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Pumping Lemma for CFLs
Theorem
Let L be a CFL. Then there exists a constant n such that if z is a string in L of
length at least n, then we can write z = uvwxy such that i) |vwx| ≤ n, ii) vx 6= ε,
and iii) for all i ≥ 0 the string uvi wxi y ∈ L.
Ashutosh Trivedi – 42 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Applying Pumping Lemma
Theorem (Pumping Lemma for Context-free Languages)
L ∈ Σ∗ is a context-free language
=⇒
there exists p ≥ 1 such that
for all strings z ∈ L with |z| ≥ p we have that
there exists u, v, w, x, y ∈ Σ∗ with z = uvwxy, |vx| > 0, |vwx| ≤ p such that
for all i ≥ 0 we have that
uvi wxi y ∈ L.
Ashutosh Trivedi – 43 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Closure Properties
Theorem
Context-free languages are closed under the following operations:
1. Union
2. Concatenation
3. Kleene closure
4. Homomorphism
5. Substitution
6. Inverse-homomorphism
7. Reverse
Ashutosh Trivedi – 44 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Closure Properties
Theorem
Context-free languages are closed under the following operations:
1. Union
2. Concatenation
3. Kleene closure
4. Homomorphism
5. Substitution
6. Inverse-homomorphism
7. Reverse
Reading Assignment: Proof of closure under these operations.
Ashutosh Trivedi – 44 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Intersection and Complementaion
Theorem
Context-free languages are not closed under intersection and complementation.
Proof.
– Consider the languages
Ashutosh Trivedi – 45 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Intersection and Complementaion
Theorem
Context-free languages are not closed under intersection and complementation.
Proof.
– Consider the languages
Ashutosh Trivedi – 45 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
Intersection and Complementaion
Theorem
Context-free languages are not closed under intersection and complementation.
Proof.
– Consider the languages
Ashutosh Trivedi – 45 of 45
Ashutosh Trivedi Lecture 6: Context-Free Grammar
CSE 322 - Introduction to Formal Methods in Computer Science
Chomsky Normal Form
Dave Bacon
Department of Computer Science & Engineering, University of Washington
A useful form for dealing with context free grammars is the Chomksy normal form. This is a particular form of
writing a CFG which is useful for understanding CFGs and for proving things about them. It also makes the parse
tree for derivations using this form of the CFG a binary tree. And as a CS major, I know you really love binary trees!
So what is Chomsky normal form? A CFG is in Chomsky normal form when every rule is of the form A → BC
and A → a, where a is a terminal, and A, B, and C are variables. Further B and C are not the start variable.
Additionally we permit the rule S → ε where S is the start variable, for technical reasons. Note that this means that
we allow S → ε as one of many possible rules.
Okay, so if this is the Chomsky normal form what is it good for? Well as a first fact, note that parse trees for a
derivation using such a grammar will be a binary tree. Thats nice. It will help us down the road. Okay, so if it might
be good for something, we can ask the natural question: is it possible to convert an arbitrary CFG into an equivalent
grammar which is of the Chomsky normal form? The answer, it turns out, is yes. Lets see how such a conversion
would proceed.
The first step is simple! We just add a new start variable S0 and the rule S0 → S where S is the original start
variable. By doing this we guarantee that the start variable doesn’t occur on the right hand side of a rule.
Next we remove the ε rule. We do this as follows. Suppose we are removing the ε rule A → ε. We remove this rule.
But now we have to “fix” the rules which have an A on their right-hand side. We do this by, for each occurrence of A
on the right hand side, adding a rule (from the same starting variable) which has the A removed. Further if A is the
only thing occurring on the right hand side, we replace this A with ε. Of course this latter fact will have created a
new ε rule. So we do this unless we have previously removed A → ε. But onward we press: simply repeat the above
process over and over again until all ε rules have been removed.
For example, suppose our rules contain the rule A → ε and the rule B → uAv where u and v are not both the
empty string. First we remove A → ε. Then we add to this rule the rule B → uv. (Make sure that you don’t delete
the original rule B → uAv. If, on the other hand we had the rule A → ε and B → A, then we would remove the
A → ε and replace the rule B → A with the rule B → ε. Of course we now have to eliminate this rule via the same
procedure.
Next we need to remove the unit rules. If we have the rule A → B, then whenever the rule B → u appears, we will
add the rule A → u (unless this rule was already replaced.) Again we do this repeatedly until we eliminate all unit
rules.
At this point we have converted our CFG to one which has no ε transitions, and where all rules are either of the
form variables goes to terminal, or of the form variable goes to string of variables and terminals with two or more
symbols. These later rules are of the appropriate Chomsky normal form. To convert the remaining rules to proper
form, we introduce extra variables. In particular suppose A → u1 u2 . . . un where n > 2. Then we convert this to a
set of rules, A → u1 A1 , A1 → u2 A2 , . . ., Ak−2 → uk−1 uk . Now we need to take care of the rules with two elements
on the right hand side. If both of the elements are variables, then we are fine. But if any of them are terminals, we
2
add a new variable and a new rule to take care of these. For example, if we have A → u1 B where u1 is a terminal,
then we replace this by A → U1 B and U → u1 .
S → ASB
A → aAS|a|ε
B → SbS|A|bb
S0 → S
S → ASB
A → aAS|a|ε
B → SbS|A|bb
S0 → S
S → ASB|SB
A → aAS|a|aS
B → SbS|A|bb|ε
S0 → S
S → ASB|SB|S|AS
A → aAS|a|aS
B → SbS|A|bb
S0 → S
S → ASB|SB|S|AS
A → aAS|a|aS
B → SbS|bb|aAS|a|aS
S0 → S
S → ASB|SB|AS
A → aAS|a|aS
B → SbS|bb|aAs|a|aS
S0 → ASB|SB|AS
S → ASB|SB|AS
A → aAS|a|aS
B → SbS|bb|aAs|a|aS
3
Now we need to take care of the rules with more than three symbols. First replace S0 → ASB by S0 → AU1 and
U1 → SB:
S0 → AU1 |SB|AS
S → ASB|SB|AS
A → aAS|a|aS
B → SbS|bb|aAs|a|aS
U1 → SB
Next eliminate S → ASB in a similar form (technically we could reuse U1 , but lets not):
S0 → AU1 |SB|AS
S → AU2 |SB|AS
A → aAS|a|aS
B → SbS|bb|aAs|a|aS
U1 → SB
U2 → SB
Onward and upward, now fix A → aAS by introducing A → aU3 and U3 → AS.
S0 → AU1 |SB|AS
S → AU2 |SB|AS
A → aU3 |a|aS
B → SbS|bb|aAs|a|aS
U1 → SB
U2 → SB
U3 → AS
Finally, fix the two B → rules:
S0 → AU1 |SB|AS
S → AU2 |SB|AS
A → aU3 |a|aS
B → SU4 |bb|aU5 |a|aS
U1 → SB
U2 → SB
U3 → AS
U4 → bS
U5 → AS
Finally we need to work with the rules which have terminals and variables or two terminals. We need to introduce
new variables for these. Let these be V1 → a and V2 → b:
S0 → AU1 |SB|AS
S → AU2 |SB|AS
A → V1 U3 |a|V1 S
B → SU4 |V2 V2 |V1 U5 |a|V1 S
U1 → SB
U2 → SB
U3 → AS
U4 → V2 S
U5 → AS
V1 → a
V2 → b
4
A quick examination shows us that we have ended up with a grammar in Chomsky normal form. (This can, of course,
be simplified.)
CS 301
Lecture 10 – Chomsky Normal Form
1 / 23
More CFLs
i j k
• A = {a b c ∣ i ≤ j or i = k}
• B = {w ∣ w ∈ {a, b, c} contains the same number of as as bs and cs combined}
∗
m n m+n
• C = {1 +1 =1 ∣ m, n ≥ 1}; Σ = {1, +, =}
• D = (abb ∣ bbaa)
∗ ∗
R
• E = {w ∣ w ∈ {0, 1} and w
∗
is a binary number not divisible by 5}
2 / 23
Another proof that regular languages are context-free
We can encode the computation of a DFA on a string using a CFG
3 / 23
Another proof that regular languages are context-free
We can encode the computation of a DFA on a string using a CFG
3 / 23
Another proof that regular languages are context-free
We can encode the computation of a DFA on a string using a CFG
3 / 23
Another proof that regular languages are context-free
We can encode the computation of a DFA on a string using a CFG
3 / 23
Another proof that regular languages are context-free
We can encode the computation of a DFA on a string using a CFG
r0 ⇒ w1 r1 ⇒ w1 w2 r2 ⇒ ⋯ ⇒ w1 w2 ⋯wn rn
3 / 23
Another proof that regular languages are context-free
We can encode the computation of a DFA on a string using a CFG
r0 ⇒ w1 r1 ⇒ w1 w2 r2 ⇒ ⋯ ⇒ w1 w2 ⋯wn rn
So G has derived the string wrn but this still has a variable
3 / 23
Formally
Proof.
Given a DFA M = (Q, Σ, δ, q0 , F ), we can construct an equivalent CFG
G = (V, Σ, R, S) where
V =Q
S = q0
R = {q → tr ∶ δ(q, t) = r} ∪ {q → ε ∶ q ∈ F }
4 / 23
Formally
Proof.
Given a DFA M = (Q, Σ, δ, q0 , F ), we can construct an equivalent CFG
G = (V, Σ, R, S) where
V =Q
S = q0
R = {q → tr ∶ δ(q, t) = r} ∪ {q → ε ∶ q ∈ F }
∗
By construction r0 ⇒ w1 r1 ⇒ w1 w2 r2 ⇒ w1 w2 ⋯wn rn
∗
Therefore, w ∈ L(M ) iff rn ∈ F iff rn ⇒ ε iff q0 ⇒ w iff w ∈ L(G)
4 / 23
Returning to our language
R
E = {w ∣ w ∈ {0,1} and w
∗
is a binary number not divisible by 5}
0 Q1
1 0
Q0 1 Q2
0
1
1
Q4 Q3
1 0
5 / 23
Returning to our language
R
E = {w ∣ w ∈ {0,1} and w
∗
is a binary number not divisible by 5}
Q1
0
1 0 Q0 → 0Q0 ∣ 1Q2
Q1 → 0Q3 ∣ 1Q0 ∣ ε
Q0 1 Q2 Q2 → 0Q1 ∣ 1Q3 ∣ ε
0 Q3 → 0Q4 ∣ 1Q1 ∣ ε
1
Q4 → 0Q2 ∣ 1Q4 ∣ ε
1
Q4 Q3
1 0
5 / 23
Chomsky Normal Form (CNF)
A CFG G = (V, Σ, R, S) is in Chomsky Normal Form if all rules have one of these
forms
• S→ε where S is the start variable
• A → BC where A ∈ V and B, C ∈ V ∖ {S}
• A→t where A ∈ V and t ∈ Σ
Note
• The only rule with ε on the right has the start variable on the left
• The start variable doesn’t appear on the right hand side of any rule
6 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S
T → AU ∣ BV ∣ a ∣ b
U → TA
V → TB
A→a
B→b
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b
U → TA
V → TB
A→a
B→b
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA
V → TB
A→a
B→b
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB
A→a
B→b
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB ⇒ bAU B
A→a
B→b
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB ⇒ bAU B
A→a ⇒ baU B
B→b
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB ⇒ bAU B
A→a ⇒ baU B
B→b ⇒ baT AB
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB ⇒ bAU B
A→a ⇒ baU B
B→b ⇒ baT AB
⇒ baaAB
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB ⇒ bAU B
A→a ⇒ baU B
B→b ⇒ baT AB
⇒ baaAB
⇒ baaaB
7 / 23
CNF example
R
Let A = {w ∣ w ∈ {a, b} and w = w }.
∗
S → AU ∣ BV ∣ a ∣ b ∣ ε S ⇒ BV
T → AU ∣ BV ∣ a ∣ b ⇒ bV
U → TA ⇒ bT B
V → TB ⇒ bAU B
A→a ⇒ baU B
B→b ⇒ baT AB
⇒ baaAB
⇒ baaaB
⇒ baaab
7 / 23
Converting to CNF
Theorem
Every context-free language A is generated by some CFG in CNF.
Proof.
Given a CFG G = (V, Σ, R, S) generating A, we construct a new CFG
G = (V , Σ, R , S ) in CNF generating A.
′ ′ ′ ′
8 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
• B → A. Add rule B → ε unless B → ε has already been removed
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
• B → A. Add rule B → ε unless B → ε has already been removed
• B → AA. Add rule B → A and if B → ε has not already been
removed, add it
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
• B → A. Add rule B → ε unless B → ε has already been removed
• B → AA. Add rule B → A and if B → ε has not already been
removed, add it
• B → xA or B → Ax. Add rule B → x
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
• B → A. Add rule B → ε unless B → ε has already been removed
• B → AA. Add rule B → A and if B → ε has not already been
removed, add it
• B → xA or B → Ax. Add rule B → x
UNIT For each rule A → B, remove it and add rules A → u for each B → u
unless A → u is a unit rule already removed
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
• B → A. Add rule B → ε unless B → ε has already been removed
• B → AA. Add rule B → A and if B → ε has not already been
removed, add it
• B → xA or B → Ax. Add rule B → x
UNIT For each rule A → B, remove it and add rules A → u for each B → u
unless A → u is a unit rule already removed
TERM For each t ∈ Σ, add a new variable T and a rule T → t; replace each t in
the RHS of nonunit rules with T
9 / 23
Proof continued
In the following x ∈ V ∪ Σ and u ∈ (Σ ∪ V )
+
′ ′
START Add a new start variable S and a rule S → S
BIN Replace each rule A → xu with the rules A → xA1 and A1 → u and
repeat until the RHS of every rule has length at most two
′
DEL-ε For each rule of the form A → ε other than S → ε remove A → ε and
update all rules with A in the RHS
• B → A. Add rule B → ε unless B → ε has already been removed
• B → AA. Add rule B → A and if B → ε has not already been
removed, add it
• B → xA or B → Ax. Add rule B → x
UNIT For each rule A → B, remove it and add rules A → u for each B → u
unless A → u is a unit rule already removed
TERM For each t ∈ Σ, add a new variable T and a rule T → t; replace each t in
the RHS of nonunit rules with T
Each of the five steps preserves the language generated by the grammar so
L(G ) = A.
′
9 / 23
Example
Convert to CNF
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
START:
10 / 23
Example
Convert to CNF
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
START:
S→A
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
10 / 23
Example
Convert to CNF
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
START:
S→A
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
10 / 23
Example
Convert to CNF
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
START:
S→A
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε:
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
START:
S→A
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε:
A → BAB ∣ B ∣ ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B
B → 00 ∣ ε
START:
S→A A1 → AB ∣ B
A → BAB ∣ B ∣ ε
B → 00 ∣ ε
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε:
A → BAB ∣ B ∣ ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B
B → 00 ∣ ε
START:
S→A A1 → AB ∣ B
A → BAB ∣ B ∣ ε Remove B → ε:
B → 00 ∣ ε
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε:
A → BAB ∣ B ∣ ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B
B → 00 ∣ ε
START:
S→A A1 → AB ∣ B
A → BAB ∣ B ∣ ε Remove B → ε:
B → 00 ∣ ε S→A∣ε
A → BA1 ∣ B ∣ A1
BIN: Replace A → BAB:
S→A B → 00
A → BA1 ∣ B ∣ ε A1 → AB ∣ B ∣ A ∣ ε
B → 00 ∣ ε Don’t add A → ε because we
A1 → AB already removed it
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε: Remove A1 → ε:
A → BAB ∣ B ∣ ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B
B → 00 ∣ ε
START:
S→A A1 → AB ∣ B
A → BAB ∣ B ∣ ε Remove B → ε:
B → 00 ∣ ε S→A∣ε
A → BA1 ∣ B ∣ A1
BIN: Replace A → BAB:
S→A B → 00
A → BA1 ∣ B ∣ ε A1 → AB ∣ B ∣ A ∣ ε
B → 00 ∣ ε Don’t add A → ε because we
A1 → AB already removed it
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε: Remove A1 → ε:
A → BAB ∣ B ∣ ε S→A∣ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B A → BA1 ∣ B ∣ A1
B → 00 ∣ ε B → 00
START:
S→A A1 → AB ∣ B A1 → AB ∣ B ∣ A
A → BAB ∣ B ∣ ε Remove B → ε: Don’t add A → ε because we
B → 00 ∣ ε S→A∣ε already removed it
A → BA1 ∣ B ∣ A1
BIN: Replace A → BAB:
S→A B → 00
A → BA1 ∣ B ∣ ε A1 → AB ∣ B ∣ A ∣ ε
B → 00 ∣ ε Don’t add A → ε because we
A1 → AB already removed it
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε: Remove A1 → ε:
A → BAB ∣ B ∣ ε S→A∣ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B A → BA1 ∣ B ∣ A1
B → 00 ∣ ε B → 00
START:
S→A A1 → AB ∣ B A1 → AB ∣ B ∣ A
A → BAB ∣ B ∣ ε Remove B → ε: Don’t add A → ε because we
B → 00 ∣ ε S→A∣ε already removed it
A → BA1 ∣ B ∣ A1
BIN: Replace A → BAB: UNIT: Remove S → A
S→A B → 00
A → BA1 ∣ B ∣ ε A1 → AB ∣ B ∣ A ∣ ε
B → 00 ∣ ε Don’t add A → ε because we
A1 → AB already removed it
10 / 23
Example
Convert to CNF DEL-ε: Remove A → ε: Remove A1 → ε:
A → BAB ∣ B ∣ ε S→A∣ε S→A∣ε
B → 00 ∣ ε A → BA1 ∣ B A → BA1 ∣ B ∣ A1
B → 00 ∣ ε B → 00
START:
S→A A1 → AB ∣ B A1 → AB ∣ B ∣ A
A → BAB ∣ B ∣ ε Remove B → ε: Don’t add A → ε because we
B → 00 ∣ ε S→A∣ε already removed it
A → BA1 ∣ B ∣ A1
BIN: Replace A → BAB: UNIT: Remove S → A
B → 00
S→A S → BA1 ∣ B ∣ A1 ∣ ε
A → BA1 ∣ B ∣ ε A1 → AB ∣ B ∣ A ∣ ε
A → BA1 ∣ B ∣ A1
B → 00 ∣ ε Don’t add A → ε because we B → 00
A1 → AB already removed it A1 → AB ∣ B ∣ A
10 / 23
Example continued
From previous slide
S → BA1 ∣ B ∣ A1 ∣ ε
A → BA1 ∣ B ∣ A1
B → 00
A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide
S → BA1 ∣ B ∣ A1 ∣ ε
A → BA1 ∣ B ∣ A1
B → 00
A1 → AB ∣ B ∣ A
Remove S → B
11 / 23
Example continued
From previous slide
S → BA1 ∣ B ∣ A1 ∣ ε
A → BA1 ∣ B ∣ A1
B → 00
A1 → AB ∣ B ∣ A
Remove S → B
S → BA1 ∣ A1 ∣ ε ∣ 00
A → BA1 ∣ B ∣ A1
B → 00
A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1
S → BA1 ∣ B ∣ A1 ∣ ε
A → BA1 ∣ B ∣ A1
B → 00
A1 → AB ∣ B ∣ A
Remove S → B
S → BA1 ∣ A1 ∣ ε ∣ 00
A → BA1 ∣ B ∣ A1
B → 00
A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1
B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1
B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1
B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1 Remove A → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1
B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1 Remove A → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1 A → BA1 ∣ 00 ∣ AB
B → 00 B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1 Remove A → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1 A → BA1 ∣ 00 ∣ AB
B → 00 B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
11 / 23
Example continued
From previous slide Remove S → A1 Remove A → A1
S → BA1 ∣ B ∣ A1 ∣ ε S → BA1 ∣ ε ∣ 00 ∣ AB S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ B ∣ A1 A → BA1 ∣ B ∣ A1 A → BA1 ∣ 00 ∣ AB
B → 00 B → 00 B → 00
A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A A1 → AB ∣ B ∣ A
Remove A1 → A
12 / 23
Example continued
Copied from the previous slide
S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ 00 ∣ AB
B → 00
A1 → AB ∣ A ∣ 00
Remove A1 → A
S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ 00 ∣ AB
B → 00
A1 → AB ∣ 00 ∣ BA1
12 / 23
Example continued
Copied from the previous slide TERM: Add Z → 0
S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ 00 ∣ AB
B → 00
A1 → AB ∣ A ∣ 00
Remove A1 → A
S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ 00 ∣ AB
B → 00
A1 → AB ∣ 00 ∣ BA1
12 / 23
Example continued
Copied from the previous slide TERM: Add Z → 0
S → BA1 ∣ ε ∣ 00 ∣ AB S → BA1 ∣ ε ∣ ZZ ∣ AB
A → BA1 ∣ 00 ∣ AB A → BA1 ∣ ZZ ∣ AB
B → 00 B → ZZ
A1 → AB ∣ A ∣ 00 A1 → AB ∣ ZZ ∣ BA1
Z→0
Remove A1 → A
S → BA1 ∣ ε ∣ 00 ∣ AB
A → BA1 ∣ 00 ∣ AB
B → 00
A1 → AB ∣ 00 ∣ BA1
12 / 23
Caution
Sipser gives a different procedure
1 START
2 DEL-ε
3 UNIT
4 BIN
5 TERM
This procedure works but can lead to an exponential blow up in the number of rules!
2∣G∣
In general, if DEL-ε comes before BIN, then ∣G ∣ is O(2 );
′
2
if BIN comes before DEL-ε, then ∣G ∣ is O(∣G∣ )
′
So use whichever procedure you’d like, but Sipser’s can be very bad
(Sipser’s is bad if you have long rules with lots of variables with ε-rules)
13 / 23
Example blow up
A → BCDEEDCB ∣ CBEDDEBC
B→0∣ε
C→1∣ε
D→2∣ε
E→3∣ε
Converting using START, BIN, DEL-ε, UNIT, TERM gives a CFG with 18 variables
and 125 rules
14 / 23
Example blow up
A → BCDEEDCB ∣ CBEDDEBC
B→0∣ε
C→1∣ε
D→2∣ε
E→3∣ε
Converting using START, BIN, DEL-ε, UNIT, TERM gives a CFG with 18 variables
and 125 rules
Converting using START, DEL-ε, UNIT, BIN, TERM gives a CFG with 1394 variables
and 1953 rules
14 / 23
Prefix
Recall Prefix(L) = {w ∣ for some x ∈ Σ , wx ∈ L}
∗
Theorem
The class of context-free languages is closed under Prefix.
15 / 23
Prefix
Recall Prefix(L) = {w ∣ for some x ∈ Σ , wx ∈ L}
∗
Theorem
The class of context-free languages is closed under Prefix.
Proof idea
R
Consider the language {w#w ∣ w ∈ {a, b} } generated by
∗
T → aT a ∣ bT b ∣ #
15 / 23
Prefix
Recall Prefix(L) = {w ∣ for some x ∈ Σ , wx ∈ L}
∗
Theorem
The class of context-free languages is closed under Prefix.
Proof idea
R
Consider the language {w#w ∣ w ∈ {a, b} } generated by
∗
T → aT a ∣ bT b ∣ #
Let’s convert to CNF
15 / 23
Prefix
Recall Prefix(L) = {w ∣ for some x ∈ Σ , wx ∈ L}
∗
Theorem
The class of context-free languages is closed under Prefix.
Proof idea
R
Consider the language {w#w ∣ w ∈ {a, b} } generated by
∗
T → aT a ∣ bT b ∣ #
Let’s convert to CNF
S → AU ∣ BV ∣ #
T → AU ∣ BV ∣ #
U → TA
V → TB
A→a
B→b
15 / 23
Derivation of ab#ba
S
S ⇒ AU
⇒ aU
⇒ aT A A U
⇒ aBV A
⇒ abV A a T A
⇒ abT BA
⇒ ab#BA
⇒ ab#bA B V a
⇒ ab#ba
We want to construct a CFG that keeps track of whether a given variable in the
derivation is
L left of the split,
S part of the split, or
R right of the split
We can construct a new CFG whose variables are ⟨A, L⟩, ⟨A, S⟩, or ⟨A, R⟩ where A is
a variable in the original CFG
′ ′
Now we just need to specify R . We’ll start with R = ∅ and add rules to it
19 / 23
Proof continued
Since L is nonempty, ε ∈ Prefix(L) so add the rule ⟨S, S⟩ → ε to R
′
′
For each rule of the form A → BC in R, add the following rules to R
⟨A, L⟩ → ⟨B, L⟩⟨C, L⟩ left of the split
⟨A, S⟩ → ⟨B, L⟩⟨C, S⟩ ∣ ⟨B, S⟩⟨C, R⟩ one of B or C is on the split
⟨A, R⟩ → ⟨B, R⟩⟨C, R⟩ right of the split
′
For each rule of the form A → t in R, add the following rules to R
⟨A, L⟩ → t
⟨A, S⟩ → t
⟨A, R⟩ → ε
20 / 23
Proof continued
∗
For each w = w1 w2 ⋯wn ∈ L, S ⇒ A1 A2 ⋯An where Ai ⇒ wi
By construction,
∗
⟨S, S⟩ ⇒ ⟨A1 , L⟩⋯⟨Ai−1 , L⟩⟨Ai , S⟩⟨Ai+1 , R⟩⋯⟨An , R⟩
∗
⇒ w1 w2 ⋯wi
for each 1 ≤ i ≤ n
′
I.e., G derives the prefix of every string in L
′
A similar argument works to show that if G derives a string then it’s a prefix of some
string in L
21 / 23
Applying the construction
Deriving ab# ⟨S, S⟩
⟨S, S⟩ ⇒ ⟨A, L⟩⟨U, S⟩
⇒ a⟨U, S⟩
⇒ a⟨T, S⟩⟨A, R⟩ ⟨A, L⟩ ⟨U, S⟩
⇒ a⟨B, L⟩⟨V, S⟩⟨A, R⟩
⇒ ab⟨V, S⟩⟨A, R⟩
a ⟨T, S⟩ ⟨A, R⟩
⇒ ab⟨T, S⟩⟨BA, R⟩
⇒ ab#⟨B, R⟩⟨A, R⟩
⇒ ab#⟨A, R⟩ ⟨B, L⟩ ⟨V, S⟩ ε
⇒ ab#
b ⟨T, S⟩⟨B, R⟩
# ε
22 / 23
Similarities with regular expression
Proving things about
• Regular languages. Assume there exists a regular expression that generates the
language and consider the six cases
• Context-free languages. Assume there exists a CFG that generates the language
and consider the three types of rules
23 / 23
Pushdown Automata (PDA)
( )
Reading: Chapter 6
1
PDA - the automata for CFLs
What is?
FA to Reg Lang,
Lang PDA is to CFL
PDA == [ -NFA + “a stack” ]
Wh a stack?
Why t k?
3
old state input symb. Stack top new state(s) new Stack top(s)
δ : Q x ∑ x => Q x
4
Example
Let Lwwr = {wwR | w is in (0+1)*}
CFG for Lwwr : S==> 0S0 | 1S1 |
PDA for Lwwr :
P := ( Q,∑, , δ,q0,Z0,F )
5
Initial state of the PDA:
Stackk
St q0
1. δ(q0,0, Z0)={(q0,0Z0)}
First symbol push on stack
2. δ(q0,1, Z0)={(q0,1Z0)}
3. δ(q0,0,
0 0)={(q
0) {(q0,00)}
00)}
4. δ(q0,0, 1)={(q0,01)}
5. δ(q0,1, 0)={(q0,10)} Grow the stack by pushing
6. δ(q0,1, 1)={(q0,11)} new symbols on top of old
(
(w-part)
t)
7. δ(q0, , 0)={(q1, 0)}
8. δ(q0, , 1)={(q1, 1)} Switch to popping mode, nondeterministically
9
9. δ(q0, ,
Z0))={(q
{(q1, Z0)} (boundary between w and wR)
a, X / Y Next
qi qj state
7
PDA for Lwwr: Transition Diagram
, Z0/Z0 q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 G to acceptance
Go
Switch to
popping mode
q0 q1 q2
, Z0 / Z0 ), ( / , Z0 / Z0
, Z0 / Z0 Go to acceptance (by
G (b fi
finall state))
Switch to when you see the stack bottom symbo
(, ( / ( (
popping mode
(, Z0 / ( Z0
To allow adjacent
blocks of nested paranthesis 9
Example 2: language of balanced
paranthesis (another design)
∑ = { (, ) }
(,Z0 / ( Z0 = {Z0, ( }
(,( / ( (
), ( /
Q = {q0,qq1}
,Z0/ Z0
start
q0 q1
,Z0/ Z0
10
PDA’s Instantaneous
Description (ID)
A PDA has a configuration at any given instance:
(q,w,y)
q - current state
w - remainder of the input (i.e., unconsumed part)
y - current stack contents as a string from top to bottom
off stack
t k
If δ(q,a, X)={(p, A)} is a transition, then the following are also true:
(q, a, X ) |--- (p,,A)
(q,
( aw, XB ) ||--- (p,w,AB)
( )
|--- sign is called a “turnstile notation” and represents
one move
|---* sign represents a sequence of moves
11
How does the PDA for Lwwr
work on input “1111”?
All moves made by the non-deterministic PDA
(q0,1111,Z0)
(q0,11,11Z
11 11Z0) (q1,111,1Z
111 1Z0) Path dies
dies…
(q0,1,111Z0) (q1,11,11Z0)
( 1, ,1111Z
(q 1111Z0) (q1, ,11Z
11Z0) (q1, ,Z
Z0) = emptyt input
i t
Path dies… AND
Path dies…
(q2, ,Z0) final state
12
Principles about IDs
Theorem 1: If for a PDA,
(q, x, A) |---* (p, y, B), then for any string
w ∑* and d *,
* it iis also
l ttrue th
that:
t
(q, x w, A ) |---* (p, y w, B )
13
There are two types of PDAs that one can design:
those that accept by final state or by empty stack
Acceptance by…
PDAs that accept by final state:
For a PDA P, the language accepted by P,
d
denoted
t db
by L(P) by
b final t t is:
fi l state, i Checklist:
{w | (q0,w,Z0) |---* (q,, A) }, s.t., q F - input exhausted?
- in a final state?
,Z0/ Z0 start
start
q0 q1 q0
Z0/ Z0
,Z Z0/ Z0
,Z
(only-if part)
Proof by induction on |x|
16
PDAs accepting by final state and empty
stack are equivalent
PF <= PDA accepting by final state
PF = (QF,∑, , δF,q0,Z0,F)
PN <= PDA accepting by empty stack
PN = (QN,∑, , δN,q0,Z0)
Th
Theorem:
(PN==> PF) For every PN, there exists a PF s.t. L(PF)=L(PN)
17
How to convert an empty stack PDA into a final state PDA?
PN==> PF construction
Whenever PN’s stack becomes empty, make PF go to
a final state without consuming any addition symbol
To detect empty stack in PN: PF pushes a new stack
symbol X0 (not in of PN) initially before simultating
PN
start
q0
start
,X /Z X
0 0 0
,X / X
0 0
p0 q0 pf
PF==> PN construction
Main idea:
Whenever PF reaches a final state, just make an -transition into a
new end state, clear out the stack and accept
Danger: What if PF design is such that it clears the stack midway
without entering a final state?
to address this,, add a new start symbol
y X0 ((not in of PF)
PN = (Q U {p0,pe}, ∑, U {X0}, δN, p0, X0)
PN:
, X0/Z0X0 , any/ , any/
New
start p0 q0 , any/ pe
…
, any/
PF
20
Equivalence of PDAs and
CFGs
21
CFGs == PDAs ==> CFLs
PDA by PDA by
≡
final state empty stack
?
CFG
22
This is same as: “implementing a CFG using a PDA”
accept
PUT
PDA
UT
INPU
w (
(acceptance
OUTP
by empty
stack) reject
implements
CFG
23
This is same as: “implementing a CFG using a PDA”
24
Formal construction of PDA
from CFG Note: Initial stack symbol (S)
same as the start variable
i th
in the grammar
Given: G= (V,T,P,S)
Output: PN = ({q}
({q}, T
T, V U T
T, δ,
δ q,
q S)
δ:
Before:
For allll A V , add
F dd th
the ffollowing
ll i After:
A
transition(s) in the PDA:
…
…
δ(q ,A)
δ(q, (q, ) | “A
A) = { (q ==>” P}
A ==>
Before:
For all a T, add the following After: a…
a transition(s) in the PDA: a pop
…
δ(q,a,a)= { (q, ) }
…
25
Example: CFG to PDA
1,1 /
G = ( {S,A}, {0,1}, P, S) 0,0 /
,A / 01
,A / A1
P: ,A / 0A1
S/
,S
S ==> AS | ,S / AS
0 0
A A 1 1
A 1 1 1 1 1 Accept by
S S S S S S S S
empty stack
0 0 1 1
(if part)
If (q,
( wx, S) |--
| * (q,x,B)
( B) then
th S =>*lm wB
B
28
Converting a PDA into a CFG
Main idea: Reverse engineer the
productions from transitions
If δ(q,a,Z) => (p, Y1Y2Y3…Yk):
1. State is changed from q to p;
2. Terminal a is consumed;
3. Stack top symbol Z is popped and replaced with a
sequence of k variables.
Action: Create a g
grammar variable called
“[qZp]” which includes the following
production:
[qZp] => a[pY1q1] [q1Y2q2] [q2Y3q3]…
] [qk-1Ykqk]
30
Two ways to build a CFG
Build a PDA Construct (indirect)
CFG from PDA
31
Deterministic PDAs
32
This PDA for Lwwr is non-deterministic
Grow stack
0, Z0/0Z0
Why does it have
1, Z0/1Z0 Pop stack for to be non-
0, 0/00
matching symbols deterministic?
0, 1/01
1, 0/10 0, 0/
1, 1/11
1,, 1/
q0 q1 q2
, Z0/Z0 , Z0/Z0
, 0/0
, 1/1 A
Accepts b
by fi
finall state
Switch to To remove
popping mode guessing,
impose the user
to insert c in the
middle 33
Example shows that: Nondeterministic PDAs ≠ D-PDAs
q0 q1 q2
c, Z0/Z0 , Z0/Z0
c, 0/0
c, 1/1 Accepts b
A by
Switch to final state
popping mode
34
Deterministic PDA: Definition
A PDA is deterministic if and only if:
1
1. δ(q,a,X)
δ(q a X) has at most one member for any
a ∑ U {}
35
PDA vs DPDA vs Regular
g
languages
Lwcwr Lwwr
non-deterministic PDA
36
Summary
PDAs for CFLs and CFGs
Non-deterministic
Deterministic
PDA acceptance types
1. By final state
2. By empty stack
PDA
IDs, Transition diagram
Equivalence of CFG and PDA
CFG => PDA construction
PDA => CFG construction 37
THE PUMPING LEMMA
THE PUMPING LEMMA
Theorem. For any regular language L there exists an integer
n, such that for all x ∈ L with |x| ≥ n, there exist u, v, w ∈ Σ∗ ,
such that
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
x
THE PUMPING LEMMA
Theorem. For any regular language L there exists an integer
n, such that for all x ∈ L with |x| ≥ n, there exist u, v, w ∈ Σ∗ ,
such that
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
u vx w
THE PUMPING LEMMA
Theorem. For any regular language L there exists an integer
n, such that for all x ∈ L with |x| ≥ n, there exist u, v, w ∈ Σ∗ ,
such that
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
u
v
w
PROOF OF P.L. (SKETCH)
Let M be a DFA for L. Take n be the number of states of M
plus 1.
Take any x ∈ L with |x| ≥ n. Consider the path (from start
state to an accepting state) in M that corresponds to x. The
length of this path is |x| ≥ n.
u uw ∈ L
x = uvw ∈ L
v uvvw ∈ L
w uvvvw ∈ L
...
USING PUMPING LEMMA TO PROVE NON-REGULARITY
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(4) ∀ i ≥ 0: uv i w ∈ L.
USING PUMPING LEMMA TO PROVE NON-REGULARITY
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(4) ∀ i ≥ 0: uv i w ∈ L.
USING PUMPING LEMMA TO PROVE NON-REGULARITY
(4) ∀ i ≥ 0: uv i w ∈ L. (4) ∀ i ≥ 0: uv i w ∈ L.
USING PUMPING LEMMA TO PROVE NON-REGULARITY
(1) x = uvw
(2) |uv| ≤ n
(3) |v| ≥ 1
(4) ∀ i ∈ N : uv i w ∈ L.
IN PICTURE
∃ u, v, w such that
(1) x = uvw
u v w
(2) |uv| ≤ n � �� � ���� � �� �
(3) |v| ≥ 1
00000 0 . . . 01111 . . . 1 ∈ L
(4) ∀ i ∈ N : uv i w ∈ L.
IN PICTURE
∃ u, v, w such that non-empty
(1) x = uvw
u v w
(2) |uv| ≤ n � �� � ���� � �� �
(3) |v| ≥ 1
00000 0 . . . 01111 . . . 1 ∈ L
(4) ∀ i ∈ N : uv i w ∈ L.
IN PICTURE
∃ u, v, w such that non-empty
(1) x = uvw
u v w
(2) |uv| ≤ n � �� � ���� � �� �
(3) |v| ≥ 1
00000 0 . . . 01111 . . . 1 ∈ L
(4) ∀ i ∈ N : uv i w ∈ L.
If (1), (2), (3) hold then (4) fails: it is not the case that for all
i, uv i w is in L.
In particular, let i = 0. uw �∈ L.
EXAMPLE 2
Prove that L = {0i : i is a prime} is NOT regular.
EXAMPLE 2
Prove that L = {0i : i is a prime} is NOT regular.
uv wi
= 0 0 |v| |v|(|v|+|w|) |w|
0
= 0 (|v|+1)(|v|+|w|)
�∈ L
EXAMPLE 2
Prove that L = {0i : i is a prime} is NOT regular.
uv wi
= 0 0 |v| |v|(|v|+|w|) |w|
0
= 0 (|v|+1)(|v|+|w|)
�∈ L
Both factors ≥ 2
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
If L is regular, then by P.L. ∃ n such that . . .
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
If L is regular, then by P.L. ∃ n such that . . .
Let us consider x = 0n 0n ∈ L. Obviously |x| ≥ n.
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
If L is regular, then by P.L. ∃ n such that . . .
Let us consider x = 0n 0n ∈ L. Obviously |x| ≥ n.
Can 0n 0n be written as 0n 0n = uvw such that |uv| ≤ n |v| ≥ 1
and that for all i: uv i w ∈ L?
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
If L is regular, then by P.L. ∃ n such that . . .
Let us consider x = 0n 0n ∈ L. Obviously |x| ≥ n.
Can 0n 0n be written as 0n 0n = uvw such that |uv| ≤ n |v| ≥ 1
and that for all i: uv i w ∈ L?
YES! Let u = �, v = 00, and w = 02n−2 .
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
If L is regular, then by P.L. ∃ n such that . . .
Let us consider x = 0n 0n ∈ L. Obviously |x| ≥ n.
Can 0n 0n be written as 0n 0n = uvw such that |uv| ≤ n |v| ≥ 1
and that for all i: uv i w ∈ L?
YES! Let u = �, v = 00, and w = 02n−2 .
Then ∀i , uv i w is of the form 02k = 0k 0k .
EXAMPLE 3
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Again we try to show that P.L. doesn’t hold.
If L is regular, then by P.L. ∃ n such that . . .
Let us consider x = 0n 0n ∈ L. Obviously |x| ≥ n.
Can 0n 0n be written as 0n 0n = uvw such that |uv| ≤ n |v| ≥ 1
and that for all i: uv i w ∈ L?
YES! Let u = �, v = 00, and w = 02n−2 .
Then ∀i , uv i w is of the form 02k = 0k 0k .
uv w = 0
0 n−|v|
10 1.n
EXAMPLE 3, 3RD ATTEMPT
Prove that L = {yy : y ∈ {0, 1}∗ } is NOT regular.
Given n from the P.L., let x = 0n 10n 1. Again x ∈ L and
|x| ≥ n.
∀u, v, w such that 0n 10n 1 = uvw and |uv| ≤ n and |v| ≥ 1:
must have uv contained in the first group of 0n . Thus consider
uv w = 0
0 n−|v|
10 1.n
Pumping Lemma
1. L = { akbk | k $ 0}
see notes
ap 0 L and
ap + k 0 L as well as
ap+2k 0 L, etc.
It should be relatively clear that p + k, p + 2k, etc., cannot all be prime but let us add
k p times, then we must have:
ap + pk 0 L, of course ap + pk = ap (k + 1)
so this would imply that (k + 1)p is prime, which it is not since it is divisible by both p
and k + 1.
3. L = {anbn+1}
Assume L is regular. From the pumping lemma there exists a p such that every w 0
L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let us
choose apbp+1. Its length is 2p + 1 $ p. Since the length of xy cannot exceed p, y
must be of the form ak for some k > 0. From the pumping lemma ap-kbp+1 must also
be in L but it is not of the right form. Hence the language is not regular.
Note that the repeatable string needs to appear in the first n symbols to avoid the
following situation:
assume, for the sake of argument that n = 20 and you choose the string a10 b11
which is of length larger than 20, but |xy| # 20 allows xy to extend past b, which
means that y could contain some b’s. In such case, removing y (or adding more y’s)
could lead to strings which still belong to L.
4. L = {anb2n }
Assume L is regular. From the pumping lemma there exists a p such that every w 0
L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let us
choose apb2p. Its length is 3p $ p. Since the length of xy cannot exceed p, y must
be of the form ak for some k > 0. From the pumping lemma ap-kb2p must also be in L
but it is not of the right form. Hence the language is not regular.
From the pumping lemma there exists an n such that every w 0 L longer than n can
be represented as x y z with |y|
0 and |x y| # n.
12. L = { 0n | n is a power of 2 }
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose
n = 2p. Since the length of xy cannot exceed p, y must be of the form 0k for
some 0 < k #p. From the pumping lemma 0m where m = 2p+ k must also
be in L. We have
2p < 2p + k # 2 p + p < 2 p + 1
Hence this string is not of the right form. Hence the language is not regular.
15. L = {anbl | n # l}
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose apbp. Its length is 2p $ p. Since the length of xy cannot exceed p, y
must be of the form ak for some k > 0. From the pumping lemma ap+k bp must
also be in L but it is not of the right form since the number of a’s exceeds the
number of b’s (Note that you must add not subtract, otherwise the string would
be OK). Hence the language is not regular.
16. L = {anblak | k = n + l}
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose apbap+1. Its length is 2p+2 $ p. Since the length of xy cannot exceed
p, y must be of the form am for some m > 0. From the pumping lemma ap-mbap+1
must also be in L but it is not of the right form. Hence the language is not
regular.
20. L = { an! | n $ 0}
Proof by contradiction:
Let us assume L is regular. From the pumping lemma, there exists a number p
such that any string w of length greater than p has a “repeatable” substring
generating more strings in the language L. Let us consider ap! (unless p < 3 in
which case we chose a3! ). From the pumping lemma the string w has a
“repeatable” substring. We will assume that this substring is of length k $ 1.
From the pumping lemma ap!-k must also be in L. For this to be true there must
be j such that j! = m! - k But this is not possible since when p > 2 and k # m we
have
m! - k > (m - 1)!
Hence L is not regular.
21. L = { anbl | n
l}
Proof by contradiction:
Let us assume L is regular. From the pumping lemma, there exists a number p
such that any string w of length greater than p has a “repeatable” substring
generating more strings in the language L. Let us consider n = p! and l = (p+1)!
From the pumping lemma the resulting string is of length larger than p and has a
“repeatable” substring. We will assume that this substring is of length k $ 1.
From the pumping lemma we can add y i-1 times for a total of i ys. If we can find
an i such that the resulting number of a’s is the same as the number of b’s we
have won. This means we must find i such that:
m! + (i - 1)*k = (m + 1)! or
(i - 1) k = (m + 1) m! - m! = m * m! or
i = (m * m!) / k +1
but since k < m we know that k must divide m! and that (m * m!) / k must be an
integer. This proves that we can choose i to obtain the above equality.
Hence L is not regular.
23. L = {anblck | k
n + l}
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose ap! bp! a(p+1)! . Its length is 2p!+(p+1)! $ p. Since the length of xy cannot
exceed p, y must be of the form am for some m > 0. From the pumping lemma
any string of the form xyi. z must always be in L. If we can show that it is always
possible to choose i in such a way that we will have k = n + l for one such string
we will have shown a contradiction. Indeed we can have
p!+(i-1)m + p! = (p+1)!
if we have i = 1 + ((p+1)! - 2 p!)/ m Is that possible? only if m divides
((p+1)! -2 p!
((p + 1)! - 2 * (p)! = (p + 1 - 2) p! and since m # p m is guaranteed to divide p!.
24. L = {anblak | n = l or l
k}
Proof by contradiction:
Let us assume L is regular. From the pumping lemma, there exists a number p
such that any string w of length greater than p has a “repeatable” substring
generating more strings in the language L. Let us consider w = apbpap. From
the pumping lemma the string w, of length larger than p has a “repeatable”
substring. We will assume that this substring is of length m $ 1. From the
pumping lemma we can remove y and the resulting string should be in L.
However, if we remove y we get ap - mbpap. But this string is not in L since p-m
p
and p = p.
Hence L is not regular.
25. L = {anba3n | n $ 0}
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose apba3p. Its length is 4p+1 $ p. Since the length of xy cannot exceed
p, y must be of the form ak for some k > 0. From the pumping lemma ap-kba3p
must also be in L but it is not of the right form. Hence the language is not
regular.
26. L = {anbncn | n $ 0}
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose apbpcp. Its length is 3p $ p. Since the length of xy cannot exceed p,
y must be of the form ak for some k > 0. From the pumping lemma ap-kbpap must
also be in L but it is not of the right form. Hence the language is not regular.
also be in L but it is not of the right form. Hence the language is not regular.
28. L = {0k10k | k $ 0 }
Assume L is regular. From the pumping lemma there exists an n such that every
w 0 L such that |w| $ n can be represented as x y z with |y|
0 and |xy| # n. Let
us choose 0n10n. Its length is 2n+1 $ n. Since the length of xy cannot exceed n,
y must be of the form 0p for some p > 0. From the pumping lemma 0n-p10n must
also be in L but it is not of the right form. Hence the language is not regular.
29. L = {0n1m2n | n, m $ 0 }
Assume L is regular. From the pumping lemma there exists a p such that every
w 0 L such that |w| $ p can be represented as x y z with |y|
0 and |xy| # p. Let
us choose 0p12p. Its length is 2p+1 $ p. Since the length of xy cannot exceed p,
y must be of the form 0p for some p > 0. From the pumping lemma 0n-p12n must
also be in L but it is not of the right form. Hence the language is not regular.