You are on page 1of 32

Arba Minch University

Arba Minch Institute of Technology


Department of Computer Science & IT

Formal Language and


Automata Theory

Prince M. Thomas

Target Group - G3CS – A/B/C Academic Year – 2019/20

1
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

2
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

3
Normal Forms

In 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.
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
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
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
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 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
Aa

11
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

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.
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 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

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
16
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).

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

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


δ - mapping function
18
Cont.,

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

5. The context free languages are not closed under complement

31
End of Chapter - 05

32

You might also like