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

Module 4 CNF

The document discusses the simplification of Context-Free Grammars (CFGs) by eliminating useless productions, null productions, and unit productions, and converting CFGs to Chomsky Normal Form (CNF) and Greibach Normal Form (GNF). It also covers the CYK algorithm for membership testing of strings against CFGs, elimination of left recursion, and the Pumping Lemma for proving certain languages are not context-free. Additionally, it outlines the closure properties of context-free languages, including their behavior under union, concatenation, and Kleene closure.

Uploaded by

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

Module 4 CNF

The document discusses the simplification of Context-Free Grammars (CFGs) by eliminating useless productions, null productions, and unit productions, and converting CFGs to Chomsky Normal Form (CNF) and Greibach Normal Form (GNF). It also covers the CYK algorithm for membership testing of strings against CFGs, elimination of left recursion, and the Pumping Lemma for proving certain languages are not context-free. Additionally, it outlines the closure properties of context-free languages, including their behavior under union, concatenation, and Kleene closure.

Uploaded by

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

Context Free Grammar

Simplification of CFGs
• Useless productions: The productions that can never
take part in derivation of any string.
S -> AB, A -> a, B -> b, C -> dc (useless symbol)

• Null (λ or ε) productions: The productions of type ‘A -> ε’


are called ε productions

• Unit productions: The productions of type ‘S -> A, A -> B’


are called unit productions.
Simplification of CFG
S → A | OC1
A → B | 01 | 10
C → ε | CD

Steps:
1. Elimination of useless symbols
2. Elimination of ε production
3. Elimination of Unit production
(Refer Class notes for solution)
Chomsky Normal Form
• A way to simplify a context-free grammar
– Start symbol can have ε production. S → ε

– A non-terminal generating a single terminal.


A→a
– A non-terminal generating two non-terminals.
S → AB
• Useful for parsing algorithms like CYK
Steps to Convert CGF to CNF
• Step 1: If the Start Symbol S occurs on the right side, create a
new start symbol S and a new production S’.
S → ASB => S1 → S, S → ASB (Start symbol S1)
• Step 2: Remove null production, unit production and useless
production (Simplification of CGF)
• Step 3: Replace terminal on RHS with Non-terminal and create
new production.
A → aB => A → XB, X→a
• Step 4: Reduce number of Non-terminal on RHS by creating
new production.
S → ABC => S → XC, X → AB
CGF to CNF
• Convert the given Context Free Grammar to
Chomsky Normal Form
S → ASB | aB
A→B|S
B→b|ε

Sol: Check the grammar is in CNF


If not CNF follow the step 1 to step 4
(Refer Class notes for complete solution)
CYK Algorithm Cocke – Younger - Kasami

• Membership algorithm (Used to check wheather it


belongs to some grammar or not)
• Specific for CFG (to check the input is belongs to that
grammar or not)
• Grammar to be expressed in Chomsky Normal Form
• CYK – Input will be given
– grammar will be given
– need to verify the input belongs to the grammar
or not.
Running time of the algorithm Is - 0(n3)
• Parse the input ‘aabb’ for the below CFG using
CYK algorithm
S → AB | BB
A → CC | AB | a
B → BB | CA | b
C → BA | AA | b

(Refer Class notes for solution)


Elimination of Left Recursion
A  Aα | β A  β A’
A’  αA’ | ε

E.g.1 S  (L) | a
LL,S|S
E.g.2 A  AC | Aad | bd | ε

E.g.3 A2  A2 A2 A1 | 0A1 | 1
(Refer Class notes for complete solution)
GREIBACH NORMAL FORM (GNF)
• Greibach Normal Form (GNF) is a form of a context-
free grammar where every production rule follows
the format:
A→aα
where
A is a non-terminal,
a is a terminal, and
α is a (possibly empty) sequence of non-
terminals.
Procedure for Converting CFG to GNF
Step 1. If the given grammar is not in CNF, convert it to CNF.
Step 2. Change the names of non terminal symbols to A1 till AN in
same sequence. (A1 => Start Symbol)
Step 3. Ai  Ajβ 3 cases i < j, i > j,
i=j
Case 1: if Ai  Ajβ and i < j, Leave the production as such
Case 2: if Ai  Ajβ and i > j, Replace Aj with its production
bodies
Case 3: if Ai  Ajβ and i = j, Eliminate Left Recursion, ε –
production and introduce new variable B.
Step 4. Repeat Step 3 until we reach the production of the form
A→aα
CFG to GNF
S → AA | 0
A → SS | 1

(Refer Class notes for complete solution)


Pumping Lemma for CFG
• Pumping lemma is used to prove a language is not
context free.
• Let ‘L’ be a CFL
• Let ‘n’ be a integer constant
• Select a string ‘Z’ from ‘L’ such that Z ≥ n
• Divide the string ‘Z’ into 5 parts. uvwxy such that
i) |vwx| ≤ n
ii) |vx| ≥ 1
iii) ∀ i ≥ 0 ∃ uviwxiy ∈ L
1. Show that L = {an bn cn | n ≥ 1} is not CFL.

2. Prove that L = {aP | P is a prime number} is not CFL.

3. Prove that L = {an^2 | n ≥ 1} is not CFL.

4. Show that L = {ai bj | j = i2} is not CFL.


Closure Properties of CFL
• CFL are close under
– Union, Concatenation, Kleen closure
• Not close under
– Intersection and complement.
Let L1 ={an | n ≥ 0} and L2 ={bn | n ≥ 0}

Union: L3 = L1 U L2

Intersection: L3 = L1.L2

Kleen Closure: Let L = {a} then L* = { }

You might also like