Professional Documents
Culture Documents
Prince M. Thomas
1
Chapter – 05
Simplification of CFG and Pushdown Automata
2
Methods for Transforming Grammar
3
Normal Forms
Here you are going to study the above two forms with help
of example.
4
Chomsky’s Normal Form
Definition:
A CFG is in CNF, if each of its productions has one of
the following two forms.
1. Non-terminal Non-terminal . Non-terminal
• Only two non-terminal in the right of production
• Ex: S AB
2. Non-terminal terminal
• only one terminal in the right of the production
• Ex: S a
5
Cont.,
Procedure for converting to CNF:
Simplify the CFG
Eliminate non-terminal, if more than two non-terminal presented in the
production and make new rule
Ex: S ABA
In CNF form: P1 BA rule 1
S AP1 rule 2
See the rule 1 and 2, only two non-terminals only available in right of the
production.
Eliminate the terminal, if more than one terminal is presented in the
production and make new rule
Ex: S aa
In CNF form: P1 a rule1
P2 a rule2
S P1P2 rule3
Now see the rule1 and 2 are containing only one terminal. Also rule3 is
containing two non-terminals only
6
Cont.,
Example 5.1: Convert the given CFG to CNF
S aSa | bSb | a | b
Write the rules separately S aSa rule1
S bSb rule2
S a rule3
S b rule4
Start with adding new symbol for the terminals
Aa new rule1
Bb new rule2
Apply new rule1 in rule1
S ASA in this rule three non-terminal available so eliminate
the non-terminals
S AP1 new rule3
P1 SA new rule4
7
Cont.,
Apply new rule2 in rule2
S BSB in this rule three non-terminal available so eliminate the
non-terminals
S BP2 new rule5
P2 SB new rule6
Now consider the rule3 and rule4, these two rules are already in
CNF format so no need to change it.
Finally the CFG in CNF
Aa new rule1
Bb new rule2
S AP1 new rule3
P1 SA new rule4
S BP2 new rule5
P2 SB new rule6
Sa new rule7
Sb new rule8 8
Cont.,
Example 5.2: Reduce the following grammar to chomsky
normal form.
S a | AAB
A ab | aB |
B aba |
First simplify the given grammar by removal of unit
production, useless symbol and production. After simplify
the above CFG you will get the simplified CFG like the
below:
S a | AB | AA | B
A ab | aB | a
B aba
9
Cont.,
In the simplified CFG, rule1, rule2, rule3 and rule7 are already in
CNF.
Rule4 S B can be
B aba
Let P1 a is in CNF newrule1
P2 b is in CNF newrule2
so now B P1P2P1
P3 P2P1 is in CNF newrule3
so B P1P3 is in CNF newrule4
So S P1P3 in CNF newrule5
Rule5 A ab apply the newrule1 and 2
So A P1P2 is in CNF newrule6
Rule6 A aB apply the newrule1
So A P1B is in CNF newrule7
10
Cont.,
Final the CFG in CNF
P1 a
P2 b
P3 P2P1
B P1P3
S P1P3
A P1P2
A P1B
S a | AB | AA
Aa
11
Greibach Normal Form (GNF)
Definition:
A CFG is in CNF, if each of its productions has one of
the following form.
12
Cont.,
Example 5.3: Convert the grammar CFG to GNF
S abSbrule1
S aa rule2
Consider rule1: S abSb
if we create another NT to define terminal b.
Bb newrule1
Hence S aBSB newrule2
Now consider rule2: S aa
if we create another NT to define terminal a.
Aa newrule3
Hence S aA newrule4
So the GNF will be:
S aBSB | aA
Aa
Bb 13
Cont.,
Example 5.4: Convert the given CFG to GNF
S ABA
A aA |
B bB |
Before converting the CFG to GNF, first we have to simplify the CFG by
using the removal of useless symbol, production and unit production.
After simplifying the CFG, we will get the below simplified CFG.
S ABA | AB | BA | AA | A | B
A aA | a
B bB | b
In the above CFG, rule7, 8, 9 and 10 are in GNF so need to convert. Then
the remaining rules from the CFG has to change in the GNF format.
Let us replace the A by aA and a
So S aABA | aBA | aAB | aB | aAA | aA | a
Let us replace the B by bB and b
So S bBA | bB | bA | b
14
Cont.,
Hence the GNF
S aABA | aBA | aAB | aB | aAA | aA | a
S bBA | bB | bA | b
A aA | a
B bB | b
15
Chomsky’s hierarchy of Grammar
The Chomsky hierarchy
1. Type-0 Unrestricted Grammar
2. Type-1 Context Free Grammar
3. Type-2 Context Sensitive Grammar
4. Type-3 Regular Grammar
17
Pushdown Automata
Introduction:
Non-regular languages are not accepted by the Finite State
machine. So we need another new model to accept the non-regular
languages. That automata is called Pushdown Automata (PDA). It has
a stack which is used to remember the input.
Inserting symbol into the stack is called Push operation
Removing symbol from the stack is called Pop operation
Definition: PDA is a collection of the seven components
M = (Q, Σ, δ,┌, q , Z0, F )
0
19
Pushdown Automata and Context Free Language
20
Cont.,
21
Cont.,
22
Cont.,
23
24
Cont.,
25
26
Cont.,
27
Cont.,
28
Cont.,
29
Deterministic PDA and Deterministic CFL
30
Properties of CFL
The context free languages closed under some operation means after
performing that particular operation on those CFLs the resultant
language is context free language. Properties are:
Here L, L1 and L2 are considered as CFL
1. The context free languages are closed under union
L = L1 U L2
2. The context free languages are closed under concatenation
L = L1 L2
3. The context free languages are closed under kleen closure
L1 = L1*
4. The context free languages are not closed under intersection
31
End of Chapter - 05
32