You are on page 1of 15

Simplification of CFG

• Removal of useless symbols (reduction of CFG)


• Removal of unit productions
• Removal of null productions
• X->ε
•Useless symbols

Those symbols that do not participate in derivation of any string are called
useless symbols.

A symbol X is useful if:


•If X is generating, i.e., X =>* w, where w ϵ L(G) and w in T, this means that
the string leads to a string of terminal symbols.
•If X is reachable If there is a derivation S =>* αXβ =>* w, w ϵ L(G), for
same α and β, then X is said to be reachable.

For reduction of a given grammar G:


•Identify non-generating symbols in the given CFG and eliminate those
productions which contains non-generating symbols.

•Identify non-reachable symbols and eliminate those productions which


contain the non-reachable symbols
Remove the useless symbol from the given context free grammar:
S -> aB / bX, A -> Bad / bSX / a, B -> aSB / bBX, X -> SBD / aBx / ad
Ex. 1
S -> AB|a S->a
A -> b
Result production is S->a
Ex.4
Ex. 3 S->aC/SB
A->bSCa
S -> bX B->aSB/bBC
X ->ad C->aBC/ad

Ex.5
Ex. 2 A -> xyz / Xyzz
G=({S,A,B,C},{a,b,d},P,{S}) X -> Xz / xYz
S -> aaB |aaS Y -> yYy / Xz
B -> ab|b Z -> Zy / z
•Unit Production

S → XY, X → a, Y → Z | b, Z → M, M → N, N → a

S->AB,
B->a|b
A->a
•Null Production

Ex.1 S → ASA | aB | b, A → B, B → b | ∈
Ex.2
S → ABAC, A → aA/∈, B → bB | ∈, C->c
S->ABAC
Removing A → ∈ from the given grammar
S->ABAC|BAC|ABC|BC
A->aA|a
B->bB| ∈
C->c
Removing B-> ∈ from the grammar
S->ABAC|BAC|ABC|BC|AAC|AC|C
A->aA|a
B->bB|b
C->c
Removing Unit production S->C
S->ABAC|BAC|ABC|BC|AAC|AC|c
A->aA|a
B->bB|b
C->c
CNF
CHOMSKY NORMAL FORM(CNF): A CFG is in Chomsky Normal Form if the
Productions are in the following forms −
• A → a (Non-terminal -> one terminal)
• A → BC ( Non-terminas -> string of exactly two non-terminals)
where A, B, and C are non-terminals(N) and a is terminal(T).

Algorithm to Convert into Chomsky Normal Form −

Step 1 − If the start symbol S occurs on some right side, create a new start
symbol S’ and a new production S’→ S.
Step 2 − Remove Null productions.
Step 3 − Remove unit productions.
Step 4 − Replace each production A → B1…Bn where n > 2 with A → B1C where C
→ B2…Bn. Repeat this step for all productions having two or more symbols in the
right side.
Step 5 − If the right side of any production is in the form A → aB where a is a
terminal and A, B are non-terminal, then the production is replaced by A →
XB and X → a. Repeat this step for every production which is in the form A → aB.
Ex.: Change the following grammar in CNF: S->1A/0B, A->1AA/0S/0, B->0BB/1

Ex.: Convert the following CFG into CNF: S’->ASA


S → ASA | aB, A → B | S, B → b | ε S’->SA
S’->S, S → ASA | aB, A → B | S, B → b | ε S’->AS
Removing B-> ε from the grammar S’->aB
S’->S S’->a
S->ASA|aB|a S->ASA
A-> B| ε|S S->SA
B->b S->AS
S->a
Removing A-> ε
A->b
S’->S A->XA
S->ASA|SA|AS|S|aB|a A->SA
A->B|S A->AS
B->b A->YB
A->a
S’-> ASA|SA|AS|aB|a B->b
S->ASA|SA|AS|aB|a S->YB
A->b| ASA|SA|AS|aB|a Y->a
S->XA
B->b
X->AS
GNF
GREIBACH NORMAL FORM(GNF): A CFG is in Greibach Normal Form if the
Productions are in the following forms −
A→b
A → bD1…Dn
where A, D1,....,Dn are non-terminals and b is a terminal.
Algorithm to Convert a CFG into Greibach Normal Form:
• Step 1 − Remove Null productions
• Step 2 − Remove unit productions.
• Step 3 – Check whether the production is in CNF. If not convert into CNF.
• Step 4 − Remove all direct and indirect left-recursion.
• Step 5 − Do proper substitutions of productions to convert it into the proper
form of GNF.
Pumping Lemma for context free Grammar
Theorem: Let L be a Context free language. Then L has a pumping length P such that
any string S where |S| ≥ P may be divided into five parts, S = uvxyz, such that the
following conditions must be true
• |vy| > 0
• |vxy| ≤ P
• For all i ≥ 0, the string uvixyiz is also in L.

Applications of Pumping Lemma


Pumping Lemma is to be applied to show that certain languages are not context free. It should never
be used to show a language is context free.

•If L is context free, it satisfies Pumping Lemma.


•If L does not satisfy Pumping Lemma, it is not context free.

Method to prove that a language L is not context free


•At first, we have to assume that L is context free. So, the pumping lemma should hold
for L.
•Use the pumping lemma to obtain a contradiction −
Select S such that |S| ≥ P
 Divide S in uxvyz
 show that uvixyiz does not belong to L for some i.
 | vy|>0
 |vxy|<=P
Show that S can not satisfy all the 3 conditions at the same time
Hence L is not context free.
Show that L={a^nb^nc^n | n>=0 } is not CFL
•Assume that L is a Context free language
•L must have a pumping lemma (say p)
•Now we take a string S such that S= a^pb^pc^p
• we divide S into parts uvxyz
Now assume p=4
S= a^4b^4c^4
= aaaabbbbcccc
Case 1: v and y contain only one type symbol
Let u=a, v=aa , x=abbbbc, y=c and z=cc
Take uv^ixy^iz
for some i=2, it is uv^2xy^2z
= aaaaaabbbbccccc
=a^6b^4c^5
= a^6b^4c^5 does not belong to L
This CFL in non-regular.
Chomsky Hierarchy
Type-0 - UG

Type-1 - CSG

Type-2 - CFG

Type-3 - RG

TM LBA PDA FA CSL REL


RL CFL
• Unrestricted Grammar(UG) - α->β where α,βϵ(NUT)*

• Context Sensitive Grammar(CSG) - α->β where |α|<=|β|and


α,βϵ(NUT)+

• Context Free Grammar(CFG) - α->β where |α|= 1 and


βϵ(NUT)*

• Regular Grammar (RG) - α->β where |α|= 1 and β is either of the


form NT* and T*N i.e. A->wB or A->Bw where wϵT*

• Note: Here N means Non-terminal and T means Terminal


S->AB|BC
A->aB|bA|a
B->bB|cC|b
C->c
S->AB A1 A2 A3
A->BS|a
B->SA|b
S->CA|BB
B->b|SB
C->b
A->a

You might also like