MTH401ATheory of Computation
Lecture 16
Outside Context Free Languages
Akash Anand
Context sensitive grammar for 0n1n0n !• S → LDABCR
• LDA → LAAD• ADA → AAD
• ADB → ABBD
• BDB → BBD
• BDC → BCCD• CDC → CCD
• DR → ER
• CE → EC
• BE → EB
• AE → EA• LE → LD• A → 0• B → 1• C → 0
• R → ε• LD → ε
Union
Akash Anand
Union
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Union
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Union
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Union
• S → S1 | S2
• S1 → …• S2 → …
• - - - -
Union
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Union
• S → S1 | S2
• S1 → …• S2 → …
• - - - -CFL : Closed under union.
Concatenation
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Concatenation
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Concatenation
• S → S1S2
• S1 → …
• S2 → …
• - - - -
Concatenation
Akash Anand
Grammar 1
• S1 → …
• - - - -• - - - -
• - - - -
Grammar 2
• S2 → …
• - - - -• - - - -
• - - - -
Concatenation
• S → S1S2
• S1 → …
• S2 → …
• - - - -CFL : Closed under concatenation.
Intersection
Akash Anand
Intersection
Akash Anand
L1 = {0p1m0m : m ≥ 0, p ≥ 0}
Intersection
Akash Anand
L1 = {0p1m0m : m ≥ 0, p ≥ 0}
• Concatenation of {0}* and {1m0m : m ≥ 0}!
Intersection
Akash Anand
L1 = {0p1m0m : m ≥ 0, p ≥ 0}
• Concatenation of {0}* and {1m0m : m ≥ 0}!
• S1 → 0S1 | ε
Intersection
Akash Anand
L1 = {0p1m0m : m ≥ 0, p ≥ 0}
• Concatenation of {0}* and {1m0m : m ≥ 0}!
• S1 → 0S1 | ε• S2 → 1S20 | ε
Intersection
Akash Anand
L1 = {0p1m0m : m ≥ 0, p ≥ 0}
• Concatenation of {0}* and {1m0m : m ≥ 0}!
• S → S1S2
• S1 → 0S1 | ε• S2 → 1S20 | ε
Intersection
Akash Anand
L2 = {0n1n0q : n ≥ 0, q ≥ 0}
Intersection
Akash Anand
L2 = {0n1n0q : n ≥ 0, q ≥ 0}
• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !
Intersection
Akash Anand
L2 = {0n1n0q : n ≥ 0, q ≥ 0}
• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !
• S1 → 0S11 | ε
Intersection
Akash Anand
L2 = {0n1n0q : n ≥ 0, q ≥ 0}
• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !
• S1 → 0S11 | ε• S2 → 0S2 | ε
Intersection
Akash Anand
L2 = {0n1n0q : n ≥ 0, q ≥ 0}
• Again a concatenation! Of {0n1n : n ≥ 0}and {0}* !
• S → S1S2
• S1 → 0S11 | ε• S2 → 0S2 | ε
Intersection
Akash Anand
L1 ∩ L2
Intersection
Akash Anand
L1 ∩ L2
• L1 ∩ L2 = {0k1k0k: k ≥ 0} !
• Not context free !
Intersection
Akash Anand
L1 ∩ L2
• L1 ∩ L2 = {0k1k0k: k ≥ 0} !
• Not context free !
CFL : Not closed under intersection.
Complement
Akash Anand
Complement
Akash Anand
• Not closed under intersection ⇒ Complement may or may not be context free !
Complement
Akash Anand
• Not closed under intersection ⇒ Complement may or may not be context free !
CFL : Not closed under complement.
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
Chomsky Normal Form for CFG’s
Akash Anand
Body has exactly two non- terminal
symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Body has exactly two non- terminal
symbols.Body has a single terminal symbol.
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
Chomsky Normal Form for CFG’s
Akash Anand
Algorithm to eliminate variables that derive nothing :
1. Discover all variables that derive terminal strings.
2. For all other variables, remove all productions in which they appear either on the left or the right.
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
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. Eliminating symbols that derive no
terminal string.2. Eliminate unreachable symbols.
Chomsky Normal Form for CFG’s
Akash Anand
Example : Eliminate Useless SymbolsS → AB A → C | 0 B → 0B C → 1
1. If we eliminate unreachable symbols first we would find that everything is reachable.
2. A, C, 0, 1 would never get eliminated.
Chomsky Normal Form for CFG’s
Akash Anand
ε- Productions :
1. We can almost avoid productions of the form A → ε.• The problem is that ε can not be in the
language of any grammar that has no ε-productions.
2. Theorem : If L is a CFL, then L – {ε} has a CFG with no ε- productions.
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
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