MTH401ATheory of Computation
Lecture 17
Chomsky Normal Form for CFG’s
Akash Anand
For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either
1. S → AB , or2. B → a
Chomsky Normal Form for CFG’s
Akash Anand
For every context free language, L, the language L – {ε} has a grammar in which every production looks like, either
1. S → AB , or2. B → a
Further, the grammar has no useless symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Chomsky Normal Form for CFG’s
Akash Anand
Eliminating useless symbols :• A symbol is useful if it appears in some
derivation of some terminal string from the start symbol.
• It is useless, otherwise. Eliminate all useless symbols by :1. Eliminate symbols that derive no
terminal string.2. Eliminate unreachable symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Eliminate VariablesS → AB | C A → 0A | 0 B → 1B C → 1
• Basis : A and C are identified because of A → 0 and C → 1.
• Induction : S is identified because of S →C.
• Nothing else can be identified.
• S → C A → 0A | 0 C → 1
Chomsky Normal Form for CFG’s
Akash Anand
Algorithm to eliminate unreachable symbols :
1. Basis : We can reach S (the start symbol).
2. Induction : If we can reach A and A → αthen we can reach all symbols in α.
3. Algorithm : Remove from the grammar all symbols not reachable from S and all productions that involve these symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Eliminate Useless SymbolsS → AB | 0 A → C | 0 B → 0B C → 1
1. If we eliminate unreachable symbols first we would find that everything is reachable.
2. A, C, 1 would never get eliminated.
Chomsky Normal Form for CFG’s
Akash Anand
Discovering Nullable Symbols :1. To eliminate all ε- productions, we first
need to discover all nullable variables (variables A such that A⇒*ε).
2. Basis : If there is a production A → ε then A is nullable.
3. Induction : If there is a production A → αand all symbols in α are nullable then Ais nullable.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε
1. A, B, C, S are nullable.2. New grammar
S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1
Chomsky Normal Form for CFG’s
Akash Anand
Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε
1. A, B, C, S are nullable.2. New grammar
S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1
C is now useless. Eliminate its productions
Chomsky Normal Form for CFG’s
Akash Anand
Example : Eliminate ε- Productions S → ABC A → 0A |ε B → 1B |ε C →ε
1. A, B, C, S are nullable.2. New grammar
S → ABC | AB | AC | BC | A | B | CA → 0A | 0B → 1B | 1
C is now useless. Eliminate its productions
Chomsky Normal Form for CFG’s
Akash Anand
Unit Productions :• A unit production is one whose right side
consists of exactly one variable.• Algorithm : Find all pairs (A,B) such that
A ⇒* B by a sequence of unit productions only.
• Basis : Pairs (A,A) are there.• Induction : If we have found (A,B) and B
→ C is a unit production then add (A,C).
Chomsky Normal Form for CFG’s
Akash Anand
Example : Unit ProductionsS → …A → B | CC | DDB → BOZO | CC | ABC | 0
- - -
Chomsky Normal Form for CFG’s
Akash Anand
Example : Unit ProductionsS → …A → B | CC | DDB → BOZO | CC | ABC | 0
- - -
Unit Productions sequences
S ⇒* B
Chomsky Normal Form for CFG’s
Akash Anand
Example : Unit ProductionsS → …
A → B | CC | DD | BOZO | CC | ABC | 0B → BOZO | CC | ABC | 0
- - -S → …A → B | CC | DD
B → BOZO | CC | ABC | 0
- - -
Chomsky Normal Form for CFG’s
Akash Anand
Example : Unit ProductionsS → A | 11A → B | 1B → S | 0
Chomsky Normal Form for CFG’s
Akash Anand
Example : Unit ProductionsS → A | 11A → B | 1B → S | 0
Unit Productions sequences
S ⇒* A, S ⇒* B;
A ⇒* B, A ⇒* S;
B⇒* S, B ⇒* A;
Chomsky Normal Form for CFG’s
Akash Anand
Example 5 : Useless SymbolsS → A | 11 | 1 | 0A → B | 1 | 0 | 11B → S | 0 | 11 | 1
S → A | 11A → B | 1B → S | 0
Chomsky Normal Form for CFG’s
Akash Anand
Example 5 : Useless SymbolsS → A | 11 | 1 | 0A → B | 1 | 0 | 11B → S | 0 | 11 | 1
S → A | 11A → B | 1B → S | 0
A and B are useless symbols. No way to get
them from S.
Chomsky Normal Form for CFG’s
Akash Anand
Cleaning Up a Grammar :• Theorem : If L is a CFL, then there is a
CFG for L – {ε} that has
• No useless symbols.• No ε- productions.
• No unit productions.• In other words, every right side is either
a single terminal or has length ≥ 2.
Chomsky Normal Form for CFG’s
Akash Anand
Chomsky Normal Form Steps:Step 1 : Clean up the grammar.Step 2 : For each right side that is not a single
terminal, make it all variables.• For each terminal symbol a, create a new variable
Aa and a production Aa → a.
• Replace a by Aa in right sides of length ≥ 2.
Step 3 : Break right sides of length longer than 2 into a chain of productions with right sides of two variables.
Chomsky Normal Form for CFG’s
Akash Anand
Example : TerminalsS → 0A1B | DEFA → CC
Chomsky Normal Form for CFG’s
Akash Anand
Example : TerminalsS → 0A1B | DEFA → CCZ → 0O → 1
Chomsky Normal Form for CFG’s
Akash Anand
Example : TerminalsS → ZAOB | DEFA → CCZ → 0O → 1
Chomsky Normal Form for CFG’s
Akash Anand
Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1
Chomsky Normal Form for CFG’s
Akash Anand
Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1
Too long.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Long productionsS → ZAOB | DEFA → CCZ → 0O → 1
Too long.
New group of symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Long productionsS → ZM | DEFA → CCZ → 0O → 1M → AOB
Chomsky Normal Form for CFG’s
Akash Anand
Example : Long productionsS → ZM | DEFA → CCZ → 0O → 1M → AOB
New group of symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Long productionsS → ZM | DHA → CCZ → 0O → 1M → AN
N → OBH → EF
Chomsky Normal Form for CFG’s
Akash Anand
Chomsky Normal Form :• CFG is said to be in a Chomsky Normal Form if
the grammar has no useless symbols and every production is of one of these two forms :1. A → BC (right side is two variables),2. A → a (right side is a single terminal).
• Theorem : If L is a CFL, then L – {ε} has a CFG in Chomsky normal form.
Pushdown Machines
Akash Anand
Pushdown Machines
Akash Anand
0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Pushdown Machines
Akash Anand
Finite State Machine
0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Pushdown Machines
Akash Anand
0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Finite State Machine
Pushdown Machines
Akash Anand
0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Pushdown Machines
Akash Anand
Finite State Machine
Pushdown Machine
0 1 0 1 1 1 0 1 0 0 0 0 0 0 0
Pushdown Machines
Akash Anand
Formalism :A pushdown machine is described by:
1. A finite set of states (Q, typically).2. An input alphabet (Σ, typically).3. A stack alphabet (Γ, typically).4. A transition function (δ, typically).5. A start state (q0, in Q, typically).6. A start symbol (Z0 in Γ, typically).7. A set of final states (F ⊆ Q, typically).
Pushdown Machines
Akash Anand
Convention :• a, b, c, … are input symbols.• Sometimes, we also allow ε as a possible
value.• …, X, Y, Z are stack symbols.• …, w, x, y, z are strings of input symbols.• α, β, γ, … are strings of stack symbols.
Pushdown Machines
Akash Anand
The Transition Function :
Pushdown Machines
Akash Anand
The Transition Function :• Takes three input arguments.
1. A state, in Q.2. An input, which is either a symbol in Σ or ε.
3. A stack symbol in Γ.
Pushdown Machines
Akash Anand
The Transition Function :• Takes three input arguments.
1. A state, in Q.2. An input, which is either a symbol in Σ or ε.
3. A stack symbol in Γ.• δ(q, a, Z) is a set of zero or more actions
of the form (p, α).• p is a state and α is a string of stack
symbols.
Pushdown Machines
Akash Anand
Actions of The Pushdown Machine :• If δ(q, a, Z) contains (p, α) among its
actions, then one thing the pushdown machine can do in state q, with a at the front of the input, and Z on top of the stack is :1. Change the state to p.2. Remove a from the front of the input (a
may be ε).3. Replace Z on the top of the stack by α.
Pushdown Machines
Akash Anand
Example :• Construct a pushdown machine to
accept {0n1n : n ≥ 1}.
Pushdown Machines
Akash Anand
Example :• Construct a pushdown machine to
accept {0n1n : n ≥ 1}.• The states :
Pushdown Machines
Akash Anand
Example :• Construct a pushdown machine to
accept {0n1n : n ≥ 1}.• The states :
1. q = start state. We are in state q if we have seen only 0’s so far.
Pushdown Machines
Akash Anand
Example :• Construct a pushdown machine to
accept {0n1n : n ≥ 1}.• The states :
1. q = start state. We are in state q if we have seen only 0’s so far.
2. p = we have seen at least one 1 and may now proceed only if the inputs are 1’s.
Pushdown Machines
Akash Anand
Example :• Construct a pushdown machine to
accept {0n1n : n ≥ 1}.• The states :
1. q = start state. We are in state q if we have seen only 0’s so far.
2. p = we have seen at least one 1 and may now proceed only if the inputs are 1’s.
3. f = final state; accept.
Pushdown Machines
Akash Anand
Example (continued) :• The stack symbols :
Pushdown Machines
Akash Anand
Example (continued) :• The stack symbols :
1. Z0 = start symbol. Also marks the bottom of the stack.
Pushdown Machines
Akash Anand
Example (continued) :• The stack symbols :
1. Z0 = start symbol. Also marks the bottom of the stack.
2. X = marker, used to count the number of 0’s seen on the input.
Pushdown Machines
Akash Anand
Example (continued) :• The transitions :
Pushdown Machines
Akash Anand
Example (continued) :• The transitions :
1. δ(q, 0, Z0) = {(q, XZ0)}.
Pushdown Machines
Akash Anand
Example (continued) :• The transitions :
1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules
make sure that one X is pushed into the stack for each 0 read from the input.
Pushdown Machines
Akash Anand
Example (continued) :• The transitions :
1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules
make sure that one X is pushed into the stack for each 0 read from the input.
3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.
Pushdown Machines
Akash Anand
Example (continued) :• The transitions :
1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules
make sure that one X is pushed into the stack for each 0 read from the input.
3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.
4. δ(p, 1, X) = {(p, ε)}. Pop one X per 1.
Pushdown Machines
Akash Anand
Example (continued) :• The transitions :
1. δ(q, 0, Z0) = {(q, XZ0)}.2. δ(q, 0, X) = {(q, XX)}. These two rules
make sure that one X is pushed into the stack for each 0 read from the input.
3. δ(q, 1, X) = {(p, ε)}. When we see a 1, go to state p and pop one X.
4. δ(p, 1, X) = {(p, ε)}. Pop one X per 1.5. δ(p, ε, Z0) = {(f, Z0)}. Accept at bottom.
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
0 0 0 1 1 1
q
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
0 0 0 1 1 1
q
δ(q, 0, Z0)
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
0 0 0 1 1 1
q
δ(q, 0, Z0)δ(q, 0, Z0) = {(q, XZ0)}
Pushdown Machines
Akash Anand
Z0
X
Example (continued) : In action
0 0 1 1 1
q
Pushdown Machines
Akash Anand
Z0
X
Example (continued) : In action
0 0 1 1 1
q
δ(q, 0, X)
Pushdown Machines
Akash Anand
Z0
X
Example (continued) : In action
0 0 1 1 1
q
δ(q, 0, X)δ(q, 0, X) = {(q, XX)}
Pushdown Machines
Akash Anand
Z0
X
X
Example (continued) : In action
0 1 1 1
q
Pushdown Machines
Akash Anand
Z0
X
X
Example (continued) : In action
0 1 1 1
q
δ(q, 0, X)
Pushdown Machines
Akash Anand
Z0
X
X
Example (continued) : In action
0 1 1 1
q
δ(q, 0, X)δ(q, 0, X) = {(q, XX)}
Pushdown Machines
Akash Anand
Z0
X
X
X
Example (continued) : In action
1 1 1
q
Pushdown Machines
Akash Anand
Z0
X
X
X
Example (continued) : In action
1 1 1
q
δ(q, 1, X)
Pushdown Machines
Akash Anand
Z0
X
X
X
Example (continued) : In action
1 1 1
q
δ(q, 1, X)δ(q, 1, X) = {(p, ε)}
Pushdown Machines
Akash Anand
Z0
X
X
Example (continued) : In action
1 1
p
Pushdown Machines
Akash Anand
Z0
X
X
Example (continued) : In action
1 1
p
δ(p, 1, X)
Pushdown Machines
Akash Anand
Z0
X
X
Example (continued) : In action
1 1
p
δ(p, 1, X)δ(p, 1, X) = {(p, ε)}
Pushdown Machines
Akash Anand
Z0
X
Example (continued) : In action
1
p
Pushdown Machines
Akash Anand
Z0
X
Example (continued) : In action
1
p
δ(p, 1, X)
Pushdown Machines
Akash Anand
Z0
X
Example (continued) : In action
1
p
δ(p, 1, X)δ(p, 1, X) = {(p, ε)}
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
p
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
p
δ(p, ε, Z0)
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
p
δ(p, ε, Z0)δ(p, ε, Z0) = {(f, Z0)}
Pushdown Machines
Akash Anand
Z0
Example (continued) : In action
f
Pushdown Machines
Akash Anand
Instantaneous Descriptions :• We can formalize the pictures just seen
with an instantaneous description (ID).• An ID is a 3-tuple (q, w, α), where:
1. q is the current state.2. w is the remaining input.3. α is the stack contents, top at the left.
Pushdown Machines
Akash Anand
The “Goes-To” relation:• To say that ID I can become ID J in one
move of the pushdown machine, we write I ⊦ J.
Pushdown Machines
Akash Anand
The “Goes-To” relation:• To say that ID I can become ID J in one
move of the pushdown machine, we write I ⊦ J.
• Formally, (q, aw, Xα) ⊦ (p, w, βα), for any w and α, if δ(q, a, X) contains (p, β).
Pushdown Machines
Akash Anand
The “Goes-To” relation:• To say that ID I can become ID J in one
move of the pushdown machine, we write I ⊦ J.
• Formally, (q, aw, Xα) ⊦ (p, w, βα), for any w and α, if δ(q, a, X) contains (p, β).
• Extend ⊦ to ⊦*, meaning “zero or more moves” by • Basis : I ⊦* I.• Induction : If I ⊦* J and J ⊦ K, then I ⊦* K.
Top Related