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

ATC Module 3

The document describes how to convert a context-free grammar (CFG) to Chomsky normal form (CNF) in 4 steps: 1. Remove epsilon productions 2. Remove unit productions 3. Remove mixed productions 4. Remove long productions by replacing non-terminals with other non-terminals. An example grammar is converted to CNF through these 4 steps.

Uploaded by

Pranav Kanagal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

ATC Module 3

The document describes how to convert a context-free grammar (CFG) to Chomsky normal form (CNF) in 4 steps: 1. Remove epsilon productions 2. Remove unit productions 3. Remove mixed productions 4. Remove long productions by replacing non-terminals with other non-terminals. An example grammar is converted to CNF through these 4 steps.

Uploaded by

Pranav Kanagal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

CFG

Part 2
Simplifying Context-free Grammar

The given CFG can be simplified by using the


following two algorithms

a. Removing unproductive symbols


b. Removing unreachable symbols
Removing unproductive symbols
Example:

1. a and b are terminals. Hence they are productive


2. A is productive, because of AaAb
3. B is productive, because of BbA
4. S and D are productive, because of S AB and D AB
5. C is unproductive
So Remove C from both LHS and RHS. The grammar obtained is
S AB
A aAb | ϵ
B  bA
D AB
Removing unreachable symbols
Removing unreachable symbols
The grammar obtained after removing unproductive symbol is
S AB
A aAb | ϵ
B  bA
D AB
1. Start state S is reachable
2. A and B are reachable. Because of S AB
3. D is not Reachable.
4. Hence remove D production.
The simplified grammar obtained after removing both
unproductive and unreachable symbols is as follows:
S AB
A aAb| ϵ
B  bA
Simplify the given CFG
Simplify the given CFG
1. S aA|bB Algorithm 1: Remove unproductive
A-> aA |a Step 1: Let G`=G
Step 2: Group1: Pro= {a, b}
B bB Group2: Unpro = {S,A,B,D,E}
D ab|Ea Step 3: A->a A is Productive
EaC|d Pass 1 D->ab D is Productive
E->d E is Productive
Pass 2: S->aA S is Productive
A->aA|a
D->Ea Pass 3: No change
Grammar after removing unproductive symbols
S aA
A-> aA |a
D ab|Ea
Ed
Algorithm 2: Remove Unreachable
Step 1: Let G=G`
Step 2: Reac G= {S}
Unreach = {A, D, E}
Step 3: S->aA A is reachable
D and E are not reachable
Simplified grammar is given by
S-aA
A->aA|a
Simplify the given CFG
2. S aA|a|Bb|cC
A aB
B a|Aa
Grammar after algorithm1 is
C cCD
S aA|a|Bb
Dddd
A aB
Algo1:
Step 1: G1: {a,b,c,d} B a|Aa
Pass 1: G2: {S, A, B, C, D} Dddd
Step 2: S->a S is productive
B->a B is productive
D->ddd D is productive
Pass 2: A->aB A is productive
Pass 3: No change ;
C is unproductive symbol. Remove
Algo 2: Remove unreachable
S aA|a|Bb
A aB
Simplified
B a|Aa
Grammar
Dddd
S aA|a|Bb
Step 1: Let G`=G A aB
Step 2: G1: Reach={S} B a|Aa
Pass 1: G2: Unreach= {A, B, D}
Pass 2: A and B are reachable due to
S->aA|Bb
D is unreachable; Remove D
Proving that a Grammar is correct
1. G generates only string in L
2. G generates all strings in L
1. Prove the correctness of the
Language L={anbn|n≥0}
CFG: G= (V,T,P,S)
S
Where V= {S,a,b} S Є ϵ L={anbn}
aSb
T={a,b} aaSbb
aaaSbbb
aaaaSbbbb
P= S aSb|ϵ aaaaϵbbbb
aaaabbbbb ϵ L={anbn}
S is the start state
ST balanced Parentheses is ambiguous for
1. (()) ()
2. ()()()
1. S (S)
SSS
SЄ
2.
S (S)
SSS
SЄ
Causes of ambiguity
• Є production
• Symmetry

• Remove these to remove ambiguity


Eliminating Є production
Finding Nullable Variables
Eliminating E rules

Whenever language contains Є .


It is solved by the following grammar
Eliminating symmetric rules
Balanced Parentheses
S (S) Eliminate Є Eliminate

S  SS rules S*  Є symmetric rule

S*S SSS S*  Є
S Є
SSS SSS1 S*S
S(S) OR SSS1
S() SS1S SS1
S1(S)
S1()
Remove Ambiguity
Unambiguous
G= EE+E
EE+T|T
EE*E TT*F|F
E(E) F (E)|id
E(id)
E=E+T
=T+T
= F+T
= id + T
= id + T * F
= id + F * F
= id + id * F
= id + id * id
Normal Forms
1. Chomsky Normal Form (CNF)
Xa where a Є ∑
or
X BC where B and C are elements of V- ∑
{ BC- Only two non-terminals}
2. Greibach Normal Form (GNF)
Xaβ where a Є ∑ and β Є (V- ∑)*
Example: valid: Xa, XbY, XbAB
Invalid: X aa, X aYBa, XYZ
Steps to get CNF
1. Eliminate ‘Є’ (Epsilon) production A Є
2. Eliminate Unit production AB
3. Eliminate mixed production A->aB, ABaC
4. Eliminate long productions ABCDE
Eliminating Є production (Repeated
Finding Nullable Variables
slide)
Note: AB

Remove unit production Ba


Can be written as
R= SXY A a
S: does not contain unit production
XA X: Remove rule XA, add XB|a
A B|a Remove XB, add X->b. Final rule X-> b|a

Bb A: Remove AB, add A->b


YT
B: does not contain unit production
T Y|c
R= SXY Y: Remove YT, add YY |c; since YY is
Xb|a useless; remove it, so it is Yc
A b T: Remove TY, add Tc, this has no effect
Bb since already we had Yc.
Yc
Tc
Remove mixed production
1. Aa; contains no mixed production
Aa 2. AaB; Replace a by X; AXB (Valid); add Xa
3. ABaC; Replace a by X; ABXC
AaB 4. ABbC; Replace b by Y; ABYC; add Yb
ABaC
ABbC Aa
AXB
ABXC
ABYC
Xa
Yb
Remove long production from the
grammar
Example: A->BCDEF
Solution : A BW W= CDEF
W  CX X= DEF
X D Y Y = EF
Y EF
Convert the given grammar to CNF
G = S aACa Step 1: Remove epsilon production

A B | a Pass 1: {}
BC|c Pass 2: C ε ; Nullable variable: {C}
Pass 3: BC; B is nullable: {B, C}
C  cC| ε
Pass 4: A B; A is nullable: {A, B, C}

Rewrite rule:
SaACa |aAa |aCa | aa
A B |a
B C|c
CcC |c
Step 2: Remove Unit Production
SaACa |aAa |aCa | aa
A B |a
B C|c
CcC |c
1. Remove AB; add A  C |c, Remove AC; add A  cC
2. Remove BC; Add BcC

SaACa |aAa |aCa | aa


A cC | c |a
B cC | c
C cC |c
Step 3: Remove mixed productions
SaACa |aAa |aCa | aa
A cC | c |a
B cC | c
C cC |c
Soln: SXACX | XAX | XCX |XX
A YC | c | a
BYC | c
C YC | c
Xa
Yc
SXS1
Step 4: Remove Long S1AS2
productions S2CX
SXACX | XAX | XCX |XX SXS3
A YC | c | a S3AX
BYC | c SXS2
SXX
C YC | c
A YC | c | a
Xa BYC | c
Yc C YC | c
Xa
Yc
GNF
• S->Aba|B
• A->aA|B|a
• B->aAb
1. No E production
2. Unit production
S->Aba|aAb
A->aA|aAb|a
B->aAb
Convert to CNF
• S0A|1B
• A0AA|1S|1
• B1BB|0SS|0
GNF
S->Aba|B
A->aA|B|a
B->aAb

You might also like