0% found this document useful (0 votes)
3 views

Module 4 [Autosaved]1

The document covers the theory of context-free grammars (CFG), including definitions, derivations, parse trees, ambiguity, and simplification techniques. It discusses the CYK algorithm for membership testing in context-free languages and provides examples of ambiguous and unambiguous grammars. Additionally, it addresses decidability results related to context-free languages and methods for eliminating useless symbols and productions in CFGs.

Uploaded by

Manipriyaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Module 4 [Autosaved]1

The document covers the theory of context-free grammars (CFG), including definitions, derivations, parse trees, ambiguity, and simplification techniques. It discusses the CYK algorithm for membership testing in context-free languages and provides examples of ambiguous and unambiguous grammars. Additionally, it addresses decidability results related to context-free languages and methods for eliminating useless symbols and productions in CFGs.

Uploaded by

Manipriyaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 110

CSE2013

Theory of Computation
MODULE - 4
Dr. T.M. Navamani
Associate Professor
School of Computer Science and Engineering (SCOPE)
Module -4
Context Free Grammars:
• Context-Free Grammar (CFG)
• Derivations - Parse Trees
• Ambiguity in CFG
• CYK algorithm
• Simplification of CFG
• Elimination of Useless symbols, Unit productions, Null productions
• Normal forms for CFG: CNF and GNF
• Pumping Lemma for CFL
• Closure Properties of CFL
• Context-sensitive grammars: Definition and Examples
Context Free Grammar (CFG)
A grammar G = (N, T, P, S) is said to be context-free if all productions in
P have the form
A →

Where A ∈ N and  ∈ (N U T)*

A language L is said to be context-free if and only if there is a context-


free grammar G such that L = L(G).
Examples
1) L = {a b / n  1}
n n

G = ({S}, {a, b}, P, S), where


P: S → aSb
S → ab
Examples
2) L = {wwR / w {a, b}*}

G = ({S}, {a, b}, P, S), where


P: S → aSa
S → bSb
S → ε
Examples
3) The grammar G with productions

S → abB
A → aaBb
B → bbAa
A → ε

L = {ab(bbaa) bba(ba) / n  0}
n n
Examples
4) The language L = {a n b m / m  n} is context-free.

G = ({S, S1 , A, B }, {a, b}, P, S), where


P: S → AS1 / S1B ,
S1 → aS1b /ab 
A → aA / a
B → bB / b
Derivations - Parse Trees
G= ({E}, {+, *, a, b, c }, P, E), where
P: E → E + E
E→E*E
E→a/b/c
Leftmost derivation (Lmd): w = a + b * c
E  E + E  a + E a + E * E a + b * E  a + b * c
Rightmost derivation (Rmd)
E E+E  E+E*E E+E*c  E+b *c  a+b *c
Derivation:
E  E+E  E+E*E E+b*E  a+b *E  a+b *c
Derivations
w=a+b*c

Leftmost derivation (Lmd): 1


E  E+E  a+ E a+ E*E  a+b *E  a+b *c

Leftmost derivation (Lmd): 2


E  E*E  E+E*E a+E*E a+b *E a+b *c
Parse Trees
Let G = (N, T, P, S) be a context-free grammar. An ordered tree is a
derivation tree (or Parse tree) for G if and only if it has the following
properties:
1. The root node labeled as S.
2. Every leaf has a label from TU{ε}.
3. Every interior vertex (vertex which is not a leaf) has labelled from N.
4. If a vertex has label A ∈ N and its children are labelled (from left to
right) a1 , a2 , a3 , . . . , an then P must contain a production of the
form
A → a1a2a3 , . . . , an
5. A leaf labelled has no siblings, that is, a vertex with a child labelled 
can have no other children.
E E

E + E E * E

a E * E E E c
+

b c a b
a=2
b=3 a + b * c = 14 a + b * c = 20
c=4 yield of the tree a + b * c
A tree that has properties 3, 4 and 5, but in which 1 does not
necessarily hold and in which property 2 is replaced by
2a) Every leaf has label from NUTU{ε}.
is said to be a Partial derivation tree.
E

E + E

a
yield of the tree is a + E
Ambiguity in CFG
Let G = (N, T, P, S) be a context-free grammar (CFG). A word w ∈ L(G) is
said to be ambiguously derivable if there exists more than one
derivation tree (or more than one left most derivation or more than
one right most derivation) for w in L(G).

A CFG G is said to be ambiguous if there is at least one word w in L(G)


which is ambiguously derivable. Otherwise it is said to be
unambiguous.
Examples
1) Consider grammar G with productions
S → SaSbS (rule 1)
S → SbSaS (rule 2)
S→ε (rule 3)

L(G ) = {w  {a, b} * / na ( w) = nb ( w)}


G is ambiguous, since w = aabbab has two derivation trees.
S  SaSbS  aSbS  aSaSbSbS  aaSbSbS  aabSbS
aabbS aabbSaSbS aabbaSbS aabbabS
aabbab=aabbab
S SaSbS aSbS  aSaSbSbS  aaSbSbS  aabSbS
aabbS aabbSaSbS aabbaSbS aabbabS
 aabbab=aabbab
1 3 1 3 3 3 1 3 3 3
S  SaSbS aSbS  aSaSbSbS  aaSbSbS  aabSbS
 aabSbSaSbS aabbSaSbS aabbaSbS aabbabS
aabbab=aabbab
1 3 1 3 3 2 3 3 3 3
S S

S a S b S S a S b S

ε ε 
ε

S a S b S S a S b S S a S b S

ε ε 
ε 
ε ε 
ε ε ε
S b S a S

ε ε ε


Examples
2) Consider grammar G with productions
S → aSb
S → SS
S → ε
G is ambiguous, since w = aabb has two derivation trees.
S S

S S S S

ε ε

a S b a S b

a S b a S b

ε ε
Examples S
3) Consider the grammar G,
G = ({S}, {a, b}, P, S), where
a S b
P: S → aSb
S → ab
a S b
G is unambiguous grammar.
a b
A CFL L can be generated by many grammars G1 , G2 , G3 , . . .
L is said to be unambiguous if there is an unambiguous grammar
generating L.

If all the grammars generating L are ambiguous, i.e. there is no


unambiguous grammar generating L, then L is said to be inherently
ambiguous.
Example:
L(G) = { an / n ≥ 1 }
S → aS
S→a
S S S S

a S a S
a a S
a S
a S
a
a S
a
unambiguous grammar.
a
L(G) = { an / n ≥ 1 }
S → SS
S→a
S S
S S

a S S S S
S S

a a S S a a S S

a a a a
ambiguous grammar
Example: (Inherently ambiguous)
L(G) = { anbn cm / n, m ≥ 1 } U { anbm cm / n, m ≥ 1 } = L1 U L2
G= ({S, S1, S2 , A, B, C, D}, {a, b, c}, P, S), where
P: S → S1 / S2
S1 → AB S2 → CD
A → aAb / ab C → aC / a
B → cB / c D → bDc / bc

L1∩L2 = { anbn cn / n ≥ 1 }
L1 = { anbn cm / n, m ≥ 1 }
abc , abc2 , abc3 , abc4 , abc5 , abc6 , . . . .
a2b2c , a2b2c2 , a2b2c3 , a2b2c4 , a2b2c5 , a2b2c6 , . . . .
a3b3c , a3b3c2 , a3b3c3 , a3b3c4 , a3b3c5 , a3b3c6 , . . . .
................................................
L2 = { anbm cm / n, m ≥ 1 }
abc , ab2c2 , ab3c3 , ab4c4 , ab5c5 , ab6c6 , . . . .
a2bc , a2b2c2 , a2b3 c3 , a2b4c4 , a2b5c5 , a2b6c6 , . . . .
a3bc , a3b2c2 , a3b3 c3 , a3b4c4 , a3b5c5 , a3b6c6 , . . . .
................................................
L1∩L2 = { anbn cn / n ≥ 1 }
S
S
S2
S1

C D
A B

a b c
a b c

It is undecidable to find whether a CFG is ambiguous or not.


Decidability Results for CFL
• Membership Problem for CFL
Given a CFL L and a string w.
Test whether w  L or wL.
One approach is using derivation trees. That is if |w| = n and the
grammar generating L is in CNF,
A → BC or A → a, A, B, C  N, a T
Then the derivation tree for w uses exactly (2n – 1) nodes labeled by
non-terminals of G.
Suppose |w| = 3, then n=3,
So (2 . 3 -1) = (6 -1) = 5 nodes
Membership Problem for CFL
The number of possible trees for w and nodes labelling leads to
exponential time an exponential algorithm.
There is another efficient algorithm which uses the idea of dynamic
programming. The algorithm is known as
CYK (Cocke, Younger and Kasami) algorithm which is O(n3) algorithm.
CYK (Cocke, Younger and Kasami) algorithm
We fill a triangular table where the horizontal axis corresponds to the
positions of an input string
w = a1a2 . . . an
An entry Xij which is an ith column entry will be filled by a set of
variables A such that
A * aia(i+1) . . . aj
The triangular table will be filled row wise in upward fashion.
CYK (Cocke, Younger and Kasami) algorithm
CYK (Cocke, Younger and Kasami) algorithm
By definition of Xij bottom row corresponds to a string of length one
and top row corresponds to a string of length n , if |w| = n.

Theorem: The algorithm described above correctly computes Xij for all
i and j. Hence w L(G), for a CFL L = L(G) if and only if S  X1n
Problems
Consider the CFG G with the following productions:
S0 → AB / SA
S → AB / SA / a
A → AB / SA / a / b
B → SA
Test the membership of aba in L(G).
S a A a
A b
Problems
ab ba
S0 SA aA  ab S0  AB  bB
S  SA  aA ab S  AB  bB
A SA  aA ab A  AB  bB
B SA  aA ab
aba
S0  SA SAA  aAA  abA  aba
S SA SAA aAA  abA  aba
A  SA SAA aAA  abA  aba
B  SA SAA  aAA  abA  aba
Problems

Since X13 contains S0 , aba  L(G)


Given the following CFG in Chomsky normal form, check whether the
membership of the following strings (using CYK algorithm)
i) ababa ii)aabb
S → AB / BC
A → BA / a
B→ CC / b
C → AB / a
Simplification of CFG
Simplification of CFG
Simplification of CFG-Phase 1
Simplification of CFG-Phase 2
Elimination of Null Production
Elimination of Null Production
Elimination of Unit Production
Elimination of Unit Production

Finally, Remove the Unreachable productions


Simplification of CFG
• Elimination of Unit productions
• Elimination of Null productions
• Elimination of Useless symbols
G  G1  G2  G3 (Reduced Grammar)

Def: Let wL(G), then the sequence


S  w1  w 2  w 3  . . . .  w n  w
is a derivation of the string (terminal) w. The strings S, w1 , w2 . . . , wn
which contain non-terminals as well as terminals, are called sentential
forms of the derivation.
Elimination of Useless symbols
Def: Let G = (N, T, S, P) be a CFG. A symbol X is useful if there is a
derivation S  * αXβ  * w for some α, β and w. Where w  T*.
Otherwise X is useless.

1) It should be possible to derive a terminal string from X.


αXβ  w
2) It should be possible to derive a sentential form containing X from S.
S αXβ
or we can say X is reachable from S.
These two conditions are only the necessary conditions but not
sufficient conditions. That is any non-terminal satisfies both the
conditions does not imply it is useful.
Example:
S → aABbc
A → ac
S → cDd
D → cDd
D → cd
Here A is reachable from S and a terminal string can be derived from A
But this derivation does not produce a terminal string.
S aABbc  aacBbc , A is useless symbol.
Lemma 1: Given a CFG G = (N, T, S, P), with L(G) ≠ φ, we can effectively
find an equivalent CFG G’ = (N’, T, S, P’) such that for each A in N’ there
is some w in T* for which A  * w.
begin
1. OLDN = {φ}
2. NEWN = { A / A → w for some w in T* }
3. While OLDN ≠ NEWN
begin
4. OLDN = NEWN
5. NEWN = OLDN U {A / A → α for some α in (T U OLDN)* }
end
6. N’ = NEWN
end
Example: G =({S, A, B, C, E}, { b, c, d }, S, P)
P: S → AB 1) OLDN = φ NEWN = { B, E}
S → AC
S → BC 2) OLDN = { B, E} NEWN = { B, E} U { C, E }
B→b = {B, C, E}
C → cEd 3) OLDN = { B, C, E} NEWN = {B, C, E} U { S, B, C, E }
E→ cEd = {S, B, C, E}
E → cd 4) OLDN = { S, B, C, E} NEWN = {S, B, C, E} U { S, B, C, E}
= {S, B, C, E}
G’ =({S, B, C, E}, { b, c, d }, S, P’)
P’: S → BC , B → b , C → cEd , E→ cEd , E → cd
L(G) = L(G’) , N’  N , P’ P
Lemma 2: Given a CFG G = (N, T, S, P), we can effectively find an equivalent
CFG G’ = (N’, T’, S, P’) such that for each X in N’UT’ there exist an α and β 
(N’UT’) for which S  *
αXβ.
Example: G =({S, A, B, C}, { a, b, c, d }, S, P)
S → AB N T N’ T’
A → aAb S a S a
A → ab A b A b
B → bB B c B
B→b C d
C→ cCd
C → cd
G’ =({S, A, B}, { a, b}, S, P’)
P’: S → AB , A → aAb , A → ab , B→ bB , B → b
L(G) = L(G’) , N’  N , T’ T , P’ P
Example: G =({S, B, C, E}, { b, c, d }, S, P)
P: S → BC , B → b , C → cEd , E→ cEd , E → cd
N T N’ T’
S b S b
B c B c
C d C d
E E
Theorem: Every non-empty CFL generated by a CFG with no useless
productions.
Lemma 1 Lemma 2
G = (N, T, S, P)  G’ = (N’, T, S, P’)  G’’ = (N’’, T’’, S, P’’)
To eliminate the useless symbols from a CFG,
1) apply Lemma 1
2) apply Lemma 2 (order is must)
Example:
Consider the grammar with productions
S → AB , S → a , A→ a L(G) = { a }
Apply Lemma 2 S → AB , S → a , A→ a
Apply Lemma 1 S → a , A→ a

Apply Lemma 1 S → a , A→ a
Apply Lemma 2 S → a
Elimination of Null Productions
Def: Any production of a CFG G = (N, T, S, P) is of the form
A→ε
is called a ε-production or null production. A non-terminal A for which
a derivation
*
A ε
is possible is called nullable or nullable non-terminal.

Theorem: If L=L(G) for some CFG G = (N, T, S, P), then L – {ε} is L(G’) for
a CFG G’ with no useless symbols or ε-productions (or null production).
G = (N, T, S, P)  G’ = (N, T, S, P’)
Step 1: Find all nullable non-terminals

Step 2: If A→ B1B2. . . Bm is a rule in P , then


A→ C1C2. . . Cm will be a rule in P’, where
Ci = Bi if Bi is not nullable
or
Ci = ε or Bi if Bi is nullable
but not all Ci can be ε
Example: 1
S → SaSbS (rule 1)
S→ε
Nullable = { S }
S → SaSbS
S → aSbS
S → SabS
S → SaSb
S → abS
S → Sab
S → aSb
S → ab
Example: 2
S → ABaC Nullable φ
A → BC Nullable B, C
B→b/ε Nullable A, B, C
C → D /ε 
D→d
S → ABaC replaced as S → ABaC , S → BaC , S → AaC , S → ABa
S → aC , S → Aa , S → Ba , S → a
A → BC replaced as A → BC , A → B , A → C
B→b replaced as B → b
C→D replaced as C → D
D→d replaced as D → d
Elimination of Unit Productions
Def: Any production of a CFG G = (N, T, S, P) of the form
A→B
where A, B N is called a unit production.

Theorem: Every CFL without ε can be generated by a CFG without unit productions.
Theorem: Every CFL without ε can be generated by a CFG without
ε-productions, unit productions and useless symbols.
G = (N, T, S, P)  G’ = (N, T, S, P’)
Step 1: Find all pairs (A, B) such that A B
*

Step 2: P = P1UP2 where


P1 is the set of all unit productions and
P2 is the set of all non-unit productions

*
P’ = P2 U For every pair (A, B) such that A B
add rules A→ β if B→ β P2
Example:
S → aSb
S→A L(G)= { ancmdmbn / n ≥ 0, m ≥ 1 }
A → cAd
A → cd
*
Step 1: Find all pairs (A, B) such that A B
(S, A)
Step 2: P1 = {S → A }
P2 = {S → aSb , A → cAd , A → cd}

P’ = P2 U {S → cAd , S → cd }
= {S → aSb , A → cAd , A → cd , S → cAd , S → cd }
G = (N, T, S, P)

⇓ Eli𝑚𝑖𝑛𝑎𝑡𝑒 ε−productions
G1 = (N, T, S, P1)
⇓ Eli𝑚𝑖𝑛𝑎𝑡𝑒 unit productions
G2 = (N, T, S, P2)

⇓ Apply Lemma 1
G3 = (N3, T, S, P3) Elimination of
⇓ Apply Lemma 2 useless symbols
G4 = (N4, T4, S, P4)
(Reduced Grammar)
Chomsky Normal Form
Steps to convert CFG to Chomsky Normal
Form
Problems
Chomsky Normal Form (CNF)
A context-free grammar is in Chomsky normal form if all productions
are of the form
A → BC
or
A→a
Where A, B, C ∈ N and a ∈ T.

Note: Every CFL without ε can be generated by a CFG with rules in CNF.
Examples
The grammar with productions
S → AS / a
A → SA/ b
is in Chomsky normal form. But the grammar with productions
S → AS / AAS
A → S A/ aa
is not in Chomsky normal form.
Convert CFG into CNF
Consider the grammar G = ({S}, {a, b}, P, S), where
P: S → aSb
S → ab
Step :1 For every terminal symbol introduce a new non-terminal.
S → ASB A→a
S → AB B→b
Convert CFG into CNF
Step :2 If A→ B1B2. . . Bm is a rule, then it is replaced by
A→ B1D1
D1→ B2D2
D2→ B3D3
.....
Dm-3→ Bm-2Dm-2
Dm-2→ Bm-1Bm
S → AD A→a
D → SB
S → AB B→b
Problems
2) Convert the grammar in CNF
E → E+E
E → E*E
E → (E)
E→a
Step :1 For every terminal symbol introduce a new non-terminal.
E → EPE P→+
E → EME M→*
E → LER L→( R→)
E→a
Problems (Continued..)

Step :2
E → ED1 P→+
D1 → PE
E → E D2 M→*
D2 → ME
E → L D3 L→( R→)
D3 → ER
E→a
Problems
3) Convert the grammar in CNF
S → aAD
A → aB
A → bAB
B→a
D→d
4) Convert the grammar in CNF
S → aSa
S → bSb
S→c
Greibach Normal Form (GNF)
A context-free grammar is in Greibach normal form if all productions
are of the form
A → ax
Where A ∈ N , a ∈ T and x ∈ N*.

Note: Every CFL without ε can be generated by a CFG with rules in GNF.
Lemma 1:
Define an A-production to be a production with non-terminal A on the
left. Let G = (N, T, P, S) be a context-free grammar. Let A → α1Bα2 be a
production in P and B → β1 / β2 /. . . / βr be the set of all B-productions.
Let G1 = (N, T, P1, S) be obtained from G by deleting the production
A → α1Bα2
from P and adding the productions
A → α1 β1 α2 / α1 β2 α2 / . . . / α1 βr α2
Then L(G) = L(G1).
Lemma 2:
Let G = (N, T, P, S) be a context-free grammar. Let A → Aα1 / Aα2 /. . . / Aαr
be the set of all A-productions for which A is the leftmost symbol of the
RHS. Let A → β1 / β2 /. . . / βs be the remaining A-productions.
Let G1 = (NU{Z}, T, P1, S) be the CFG formed by adding the non-terminal Z
to N and replacing all the A-productions
1) A → βi 2) Z → αi
A → βi Z , 1≤ i ≤ s Z → αi Z , 1≤ i ≤ r

Then L(G) = L(G1).


Convert CFG into GNF
Consider the grammar G = ({S}, {a, b}, P, S), where
P: S → aSb
S → ab
Step :1 For every terminal symbol introduce a new non-terminal.
S → ASB A→a
S → AB B→b
Step :2 Introduce order among non-terminals by renaming them.
S = A1 A1 → A2 A1 A3 A2 → a
A = A2 A1 → A2 A3 A3 → b
B = A3
Step :3 Use Lemma 1 and Lemma 2 and convert the rules in such way
that at the end of this step, the rules are in GNF or of the form
Ai → Aj γ , j > i
You may also have some Z rules.
Step :4 Convert the Ai rules into GNF by back substitution
An substitute in An-1
An-1 substitute in An-2 . . . .

A1 → a A1 A3 A2 → a
A1 → a A3 A3 → b
Step :5 Convert the Z rules into GNF
Example:2
S → SS
S → aSb
S → ab
Step :1 For every terminal symbol introduce a new non-terminal.
S → SS
S → ASB A→a
S → AB B→b
Step :2 Introduce order among non-terminals by renaming them.
S = A1 A1 → A1 A1
A = A2 A1 → A2 A1 A3 A2 → a
B = A3 A1 → A2 A3 A3 → b
Step :3
A1 → A1 A1
A1 → A2 A1 A3 / A 2 A3
A2 → a
A3 → b
Apply Lemma 2 for A1 productions, we get
A1 → A2 A1 A3 / A 2 A3
A 1 → A 2 A 1 A 3 Z / A 2 A 3Z
Z → A1 / A1 Z
A2 → a
A3 → b
Step :4 Convert the Ai rules into GNF by back substitution
A1 → aA1 A3 / aA3
A1 → aA1 A3 Z / aA3Z
Z → A1 / A1 Z
A2 → a
A3 → b
Step :5 Convert the Z rules into GNF
A1 → aA1 A3 / aA3 / aA1 A3 Z / aA3Z
Z → aA1 A3 / aA3 / aA1 A3 Z / aA3Z /
aA1 A3 Z/ aA3 Z / aA1 A3 Z Z/ aA3ZZ
A2 → a
A3 → b
Example:3
S → AS / a
A → SA / b

Step :1 For every terminal symbol introduce a new non-terminal.


S → AS / a
A → SA / b
Step :2 Introduce order among non-terminals by renaming them.
S = A1 A1 → A2A1 A1 → a
A = A2 A2 → A1A2 A2 → b
Step :3
A1 → A2A1 / a
A2 → A1A2
A2 → b
Apply Lemma 1 for A2 → A1A2 productions, we get
A2 → A2A1A2 / aA2
A 1 → A 2A 1 / a
A2 → b
Apply Lemma 2 for A2 → A2A1A2 productions, we get
A2 → aA2 / b / aA2 Z / bZ
Z → A1A2 / A1A2Z
A1 → A2A1 / a
Step :4 Convert the Ai rules into GNF by back substitution
A1 → aA2 A1 / b A1 / aA2 Z A1 / bZ A1 / a
A2 → aA2 / b / aA2 Z / bZ
Z → A1A2 / A1A2Z

Step :5 Convert the Z rules into GNF


A1 → aA2 A1 / b A1 / aA2 Z A1 / bZ A1 / a
A2 → aA2 / b / aA2 Z / bZ
Z → aA2 A1 A2 / b A1 A2/ aA2 Z A1A2 / bZ A1 A2/ aA2
Z → aA2 A1 A2 Z / b A1 A2 Z / aA2 Z A1A2 Z / bZ A1 A2 Z / aA2 Z
Convert the following grammars into GNF:
1) S → aSa
S → bSb
S→a
S→b
2) S → ABa /a
A → aaA / B
B → bAb
3) S → ab
S → aS
S → aaS
Pumping lemma for context-free languages
Let L be an infinite context-free language. Then there exists some positive
integer n such that for any wL with |w|≥ n can be decomposed as
w = uvxyz
with |vxy| ≤ n and |vy| ≥ 1
such that
wi = uvixyiz is also in L for i = 0, 1, 2, . . .
Example:1 Prove that L = { anbn cn / n ≥ 1 } is not context-free.
Assume that L is context-free language.
Let n be a positive integer.
Let w = anbn cn. Then |w| = 3n ≥ n
Therefore, by pumping lemma w can be decomposed as
w = uvxyz with |vxy| ≤ n and |vy| ≥ 1 both v and y not null
As 1 ≤ |vy| ≤ n, v or y cannot contain all the three symbols a, b, c
So, v or y is of the form
case 1: v or y contains only a’s (or b’s or c’s )
case 2: v or y is of the form aibj (or bicj )
Case 1: v or y contains only a’s (or b’s or c’s )
v or y contains only a’s  v = ai , 1 ≤ i ≤ n
w = uvxyz = anbn cn = an-iaibn cn
w0 = uxz = an-ibn cn , 1 ≤ i ≤ n
w0 L , contradiction.
Case 2: v or y is of the form aibj (or bicj )
v or y is of the form aibj v = aibj , 1 ≤ i+j ≤ n
w = uvxyz = anbn cn
w2 = uv2xy2z = uaibj aibj xy2z
w2 L , contradiction.
Hence L is not CFL.
Example:2 Prove that L = { ap / p is prime } is not context-free.
Assume that L is context-free language.
Let n be a positive integer and let p be a prime num greater than n i.e. p > n.
Let w = ap. Then |w| = p > n
Therefore, by pumping lemma w can be decomposed as
w = uvxyz with |vxy| ≤ n and |vy| ≥ 1 both v and y not null.
|vy| ≥ 1 , |vxy| ≤ n  1 ≤ |vy| ≤ n
|vy| = m , 1 ≤ m ≤ n
Let i = p +1  p = i -1
Consider
| uvixyiz| = |uvxyz| + |(vy)i - 1| = |uvxyz| + |vy|(i -1)
= p + m(i -1) = p + mp = (m+1)p , is not prime.
Therefore, uvixyiz L , a contradiction.
Thus L is not context-free language.
Prove the following languages are not context-free:
1. L ={ ww / w  { a, b}* }
2. L = {a / n  1}
n2

3. L ={ wwRw / w  { a, b}* }
4. L ={ anbm cn dm / n, m ≥ 1 }
5. L = { anbn cm / n ≠ m }
Closure Properties of CFL
1. A context-free language is closed under the following operations:
i) Union
ii) Concatenation
iii) Kleene closure
2. A context-free language is not closed under
i) Intersection
ii) Complementation
3. A context-free language is closed under reversal.
4. If L is a context-free language and R is a regular language, then
L∩R is context-free language.
1. If L1 and L2 are context-free languages, then L1UL2 is a context-free
language.
Proof: L1 is a CFL  there exist a CFG G1 = (N1, T1, S1, P1) such that G1
generates L1
L2 is a CFL  there exist a CFG G2 = (N2, T2, S2, P2) such that G2
generates L2
Now, Construct a CFG G by combining G1 and G2 that will generate L1UL2
G = (N1U N2U{S} , T1UT2 , S , P1UP2U{S→S1 , S→S2} )

Then L(G) = L1UL2


2. If L1 and L2 are context-free languages, then L1L2 is a context-free
language.
Proof: L1 is a CFL  there exist a CFG G1 = (N1, T1, S1, P1) such that G1
generates L1
L2 is a CFL  there exist a CFG G2 = (N2, T2, S2, P2) such that G2
generates L2
Now, Construct a CFG G by combining G1 and G2 that will generate L1L2
G = (N1U N2U{S} , T1UT2 , S , P1UP2U{S→S1S2} )

Then L(G) = L1L2


3. If L1 is a context-free language, then L1* is a context-free
language.
Proof: L1 is a CFL  there exist a CFG G1 = (N1, T1, S1, P1) such that G1
generates L1
Now, Construct a CFG G by from G1 that will generate L1*
G = (N1U{S} , T1 , S , P1U{S→S1S /ε} )

Then L(G) = L1*


4. Context-free languages are not closed under intersection.
Proof: Let us consider two context-free languages L1 and L2 , where
L1 = { anbn cm / n, m ≥ 1 }
L2 = { anbm cm / n, m ≥ 1 }
The productions of L1 are
P: S1 → AB , A → aAb / ab , B → cB / c
The productions of L2 are
P: S2 → CD , C → aC / a , D → bDc / bc
So, L1 and L2 are context-free languages. But
L1∩L2 = { anbn cn / n ≥ 1 } which is context-sensitive language.

Therefore, context-free languages are not closed under intersection.


5. Context-free languages are not closed under complementation.
Proof: Assume that CFL is closed under complementation.
Then, L1 is a CFL  L1c is also context-free language.
L2 is a CFL  L2c is also context-free language.

L1c and L2c are CFLs  L1c U L2c is context-free language


L1c U L2c is context-free language  (L1c U L2c)c is also context-free

But, (L1c U L2c)c = L1∩L2 is not context-free, a contradiction.


Hence, CFL is not closed under complementation
6. If L is a context-free language, then so is LR
Proof: L is a CFL  there exist a CFG G = (N, T, S, P) such that
G generates L
A grammar generating LR is given by GR = (N, T, S, PR) ,
PR can be obtained from P by reversing the right hand side of the
production

PR = { A → αR / A → α  P}
7. If L is a context-free language and R is a regular language, then
L∩R is context-free language.
Proof: L is a CFL there exists a PDA M1 =(Q1, Σ1, Γ1, δ1, q1, Z1, F1)
such that M1 accepts L
R is regular there exists a DFA M2 =(Q2, Σ2, δ2, q2, F2)
such that M2 accepts R
We can combine M1 and M2 into a single PDA
M=(Q, Σ, Γ, δ, [q1, q2 ], Z, F) , where
Q = Q1 x Q2 , Σ = Σ1 U Σ2 , Γ = Γ1 , Z = Z1 , F = F1 x F2

([pi, pj ] , β )  δ([qm, qn ] , a , b) if and only if


(pi , β )  δ1(qm , a , b) and δ2 (qn , a ) = pj
Problem 1: Show that the language L = { anbn / n ≥ 0, n ≠ 100} is
context-free.
Solution: L = {, ab, a2b2, . . . , a99b99 , a101b101 , . . . }
S → aS1b / 
S1 → aS2b / 
S2 → aS3b / 
.........
.........
S98 → aS99b / 
S99 → aS100b / 
S100 → aS101b
S101 → aS101b /  Hence L is CFL.
L = { anbn / n ≥ 0, n ≠ 100}
Let L1 = { a100b100 } , it is finite and hence it is regular.
L1 is regular its complement is also regular. That is
L1c = Σ*- L1 is regular.
L1c = {a, b}*- { a100b100 } is regular.
L2 = { anbn / n ≥ 0 } is context-free language.
Therefore,
L2 ∩ L1c = { anbn / n ≥ 0, n ≠ 100} is context-free language.
PUSHDOWN AUTOMATA (PDA)
Regular Language Type-3 FSA DFA
NFA
-NFA
Regular Exp.
Not Regular Language Pumping Lemma
Myhill - Nerode Theorem

CFL Type-2 PDA (NPDA)


NPDA ≠ DPDA
DPDA  NPDA
(LR Grammar) DCFL  CFL
Context-Sensitive Grammars and Languages
1) G= ({S}, {a, b, c }, P, S), where
P: S → aSBc (rule 1) S  abc
S → abc (rule 2) S  aSBc  aabcBc  aabBcc  aabbcc
cB → Bc (rule 3)
bB →bb (rule 4) S  aSBc aaSBcBc  aaabcBcBc
 aaabBccBc  aaabBcBcc  aaabBBccc
 aaabbBccc  aaabbbccc

L(G) = { anbnCn/ n ≥ 1 }
Grammars and Languages
6) G= ({S, A, B}, {a, b}, P, S), where
P: S → AB (rule 1) S  AB  aAbB  aaB aa = aa
A → aAb (rule 2) S  AB aAbB aAbbbB  aAbbb
bB → bbbB (rule 3)  aabb
aAb →aa (rule 4) S  AB aAbB  aAbbbB  aAbbb
B →  (rule 5) aaAbbbb
 aaabbb
L(G) = { an+1bn+k/ n ≥ 1, k = -1, 1, 3, 5, . . .}
Greibach Normal Form
Convert the following grammars into GNF:
1) S → aSa
S → bSb
S→a
S→b
2) S → ABa /a
A → aaA / B
B → bAb
3) S → ab
S → aS
S → aaS

You might also like