Chapter 05 - Pushdown Automata
Chapter 05 - Pushdown Automata
1
Methods for Transforming Grammar
2
Normal Forms
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
4
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
5
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
Sa rule3
Sb 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
6
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 7
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
8
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
9
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
10
Greibach Normal Form (GNF)
Definition:
A CFG is in CNF, if each of its productions has
one of the following form.
11
Cont.,
Example 5.3: Convert the grammar CFG to GNF
S abSb rule1
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 12
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
13
So S bBA | bB | bA | b
Cont.,
Hence the GNF
S aABA | aBA | aAB | aB | aAA | aA | a
S bBA | bB | bA | b
A aA | a
B bB | b
14
Chomsky’s hierarchy of Grammar
The Chomsky hierarchy
1. Type-0 Unrestricted Grammar
2. Type-1 Context Sensitive Grammar
3. Type-2 Context Free Grammar
4. Type-3 Regular Grammar
16
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 language 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
18
Pushdown Automata and Context Free Language
19
Cont.,
20
Cont.,
21
Cont.,
22
23
Cont.,
24
25
Cont.,
26
Cont.,
27
Cont.,
28
Deterministic PDA and Deterministic CFL
29
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