0% found this document useful (0 votes)
35 views31 pages

Chapter 05 - Pushdown Automata

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

Chapter 05 - Pushdown Automata

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

Chapter – 05

Simplification of CFG and Pushdown


Automata

Methods for Transforming Grammar – Normal Forms


– Chomsky’s hierarchy of Grammar – Pushdown
Automata – Non Deterministic Pushdown Automata
– Pushdown Automata and Context Free languages –
Deterministic Pushdown Automata and
Deterministic Context Free language – Properties of
Context free Languages

1
Methods for Transforming Grammar

Transforming grammar is nothing but


simplification of grammar. We have discussed
simplification of CFG in Chapter-04. Methods for
Transforming grammar are;
• Removal of useless symbol
• Removal of ε productions
• Removal of Unit productions

2
Normal Forms

In last previous unit, you have learned how to


simplify the CFG. Now here how to write the
grammar in a specific form. Using that forms you
can decide, how many non-terminal and terminals
should present in the right side of the grammar. Lot
of normal forms available but here you are going to
see two important forms.

• Chomsky’s Normal Form (CNF)


• Greibach Normal Form (GNF)

Here you are going to study the above two forms


with help of example. 3
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

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
Sa rule3
Sb rule4
Start with adding new symbol for the terminals
Aa new rule1
Bb 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
Aa new rule1
Bb new rule2
S  AP1 new rule3
P1  SA new rule4
S  BP2 new rule5
P2  SB new rule6
Sa new rule7
Sb 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
Aa

10
Greibach Normal Form (GNF)
Definition:
A CFG is in CNF, if each of its productions has
one of the following form.

Non-terminal  one terminal . Any number of Non-


terminal

• Only one terminal and any number of non-


terminals in the right of production
–Ex: S  a, S  aB, S  aBA,

But S  Aa, S  AB, S  AA are not in GNF

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.
Bb newrule1
Hence S  aBSB newrule2
Now consider rule2: S  aa
if we create another NT to define terminal a.
Aa newrule3
Hence S  aA newrule4
So the GNF will be:
S  aBSB | aA
Aa
Bb 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

Type-0 Unrestricted Grammar:


It includes all formal grammars. It generate exactly all
languages that can recognized by Turing machine. These
languages are known as Recursively Enumerable Language

Type-1 Context Sensitive Grammar:


Generate the context-sensitive languages. The
languages described by these grammars are exactly
all languages that can be recognized by a linear
bounded automata 15
Cont.,
Type-2 Context Free Grammar:
Generate the context-free languages. These are defined by
rules of the form with a non-terminal and a string of
terminals and/or non-terminals. These languages are
exactly all languages that can be recognized by a non-
deterministic pushdown automaton.

Type-3 Regular Grammar:


Generate the regular languages. Such a grammar restricts
its rules to a single non-terminal on the left-hand side and
a right-hand side consisting of a single terminal, possibly
followed by a single non-terminal (right regular).

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

Q – finite set of states


Σ – input set
┌ - stack alphabet
q – starting state
0

Z - starting symbol in stack which is in ┌


0

F – set of final state


17
δ - mapping function
Cont.,

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

5. The context free languages are not closed under


complement
30
End of Chapter - 05

31

You might also like