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

TOC Lecture 14

Context-free languages are closed under union, concatenation, and Kleene star operations. However, they are not necessarily closed under intersection, difference, complement, and other operations. The membership and emptiness problems are decidable for context-free languages using algorithms like CYK, while the finiteness problem is also decidable.

Uploaded by

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

TOC Lecture 14

Context-free languages are closed under union, concatenation, and Kleene star operations. However, they are not necessarily closed under intersection, difference, complement, and other operations. The membership and emptiness problems are decidable for context-free languages using algorithms like CYK, while the finiteness problem is also decidable.

Uploaded by

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

TOC Lecture-14

Properties of CFL
Closure Properties
Closure Properties
• Context-free languages are closed under:

– Union

– Concatenation

– Kleene Star operation


Union
• Let L1 and L2 be two context free languages. Then L1 ∪ L2 is
also context free.

• Example:
− Let L1 = { anbn , n > 0}. Corresponding grammar G1 will have
P: S1  aAb | ab
− Let L2 = { cmdm , m ≥ 0}. Corresponding grammar G2 will have
P: S2  cBd | ε
− Union of L1 and L2 , L = L1 ∪ L2 = { anbn } ∪ { cmdm }
− The corresponding grammar G will have the additional
production : S  S1 | S2
Concatenation
• Let L1 and L2 be two context free languages. Then L1L2 is also
context free.

• Example:
− Let L1 = { anbn , n > 0}. Corresponding grammar G1 will have
P: S1  aAb | ab
− Let L2 = { cmdm , m ≥ 0}. Corresponding grammar G2 will have
P: S2  cBd | ε
− Concatenation of L1 and L2 , L = L1L2 = { anbncmdm }
− The corresponding grammar G will have the additional
production: S  S1 S2
Kleene Star
• If L is a context free language, then L* is also context free.

• Example:

− Let L = { anbn , n ≥ 0}. Corresponding grammar G will have


P: S  aAb| ε
− Kleene Star L1 = { anbn }*

− The corresponding grammar G1 will have additional


production: S1  SS1 | ε
Other Properties
• Intersection − If L1 and L2 are context free languages, then L1 ∩ L2
is not necessarily context free.

• Difference − If L1 and L2 are context free languages, then L1 - L2 is


not necessarily context free.

• Complement − If L1 is a context free language, then L1’ may not be


context free.

• Union with Regular Language − If L1 is a regular language and L2 is


a context free language, then L1 ∪ L2 is a context free language.

• Intersection with Regular Language − If L1 is a regular language


and L2 is a context free language, then L1 ∩ L2 is a context free
language.
Decision Properties
Decision Properties
1. Test for Membership: Decidable
– CYK Algorithm

2. Test for Emptiness: Decidable

3. Test for Finiteness: Decidable


CYK Algorithm
• CYK Algorithm is a membership algorithm of context free
grammar.

• It is used to decide whether a given string belongs to the


language of grammar or not.

• CYK Algorithm operates only on Context Free Grammars given


in Chomsky Normal Form.

• The worst case running time of CYK Algorithm is O(n3.|G|)


where:
 n = length of parsed string, and
 |G| = number of rules in the given grammar G.
CYK Algorithm
Begin
for ( i = 1 to n do )
Vi1 { A | A → a is a production where ith symbol of x is a }
for ( j = 2 to n do )
for ( i = 1 to n - j + 1 do )
Begin
Vij = ϕ
For k = 1 to j - 1 do
Vij = Vij ∪ { A | A → BC is a production where B is in
Vik and C is in V(i + k)(j - k) }
End
End
Example
• Check whether the string ‘abbb’ belongs to the given grammar:
S  AB
A  BB | a
B  AB | b
• Solution: b b b a
4 3 2 1
14 13 12 11
a 1 S, B A S,B A
24 23 22
b 2 S. B A B
34 33
b 3 A B
44
b 4 B
Yes, ‘abbb’ belongs to the given grammar
as the cell14 contains the start symbol S.

You might also like