At Module-4
At Module-4
Pushdown Automata:
4.1 The languages of a PDA – acceptance by final state,
4.2 acceptance by empty stack Informal
4.3 introduction formal definition of PDA,
4.4 a graphical notation for PDA’s,
4.5 The languages of a PDA – from empty stack to final state, from final state to empty stack,
4.6 Equivalence of PDA’s and CFG’s – from grammars to PDA, from PDA’s to grammars.
Pushdown Automata(PDA)
o Pushdown automata is a way to implement a CFG in the same way we design
DFA for a regular grammar. A DFA can remember a finite amount of information,
but a PDA can remember an infinite amount of information.
o Pushdown automata is simply an NFA augmented with an "external stack
memory". The addition of stack is used to provide a last-in-first-out memory
management capability to Pushdown automata. Pushdown automata can store
an unbounded amount of information on the stack. It can access a limited
amount of information on the stack. A PDA can push an element onto the top of
the stack and pop off an element from the top of the stack. To read an element
into the stack, the top elements must be popped off and are lost.
o A PDA is more powerful than FA. Any language which can be acceptable by FA
can also be acceptable by PDA. PDA also accepts a class of language which
even cannot be accepted by FA. Thus PDA is much more superior to FA.
PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input head is read-
only and may only move from left to right, one symbol at a time.
Finite control: The finite control has some pointer which points the current symbol
which is to be read.
Stack: The stack is a structure in which we can push and remove the items from one
end only. It has an infinite size. In PDA, the stack is used to store the items temporarily.
Γ: a stack symbol which can be pushed and popped from the stack
q0: the initial state
δ: mapping function which is used for moving from current state to next state.
Turnstile Notation:
⊢ sign describes the turnstile notation and represents one move.
For example,
(p, b, T) ⊢ (q, w, α)
In the above example, while taking a transition from state p to q, the input symbol 'b' is
consumed, and the top of the stack 'T' is represented by a new string α.
Example 1:
Design a PDA for accepting a language {anb2n | n>=1}.
Now when we read b, we will change the state from q0 to q1 and start popping
corresponding 'a'. Hence,
1. δ(q0, b, a) = (q1, ε)
Thus this process of popping 'b' will be repeated unless all the symbols are read. Note
that popping action occurs in state q1 only.
1. δ(q1, b, a) = (q1, ε)
After reading all b's, all the corresponding a's should get popped. Hence when we read
ε as input symbol then there should be nothing in the stack. Hence the move will be:
1. δ(q1, ε, Z) = (q2, ε)
Where
PDA = ({q0, q1, q2}, {a, b}, {a, Z}, δ, q0, Z, {q2})
Now we will simulate this PDA for the input string "aaabbbbbb".
Example 2:
Design a PDA for accepting a language {0n1m0n | m, n>=1}.
Solution: In this PDA, n number of 0's are followed by any number of 1's followed n
number of 0's. Hence the logic for design of such PDA will be as follows:
Push all 0's onto the stack on encountering first 0's. Then if we read 1, just do nothing.
Then read 0, and on each read of 0, pop one 0 from the stack.
For instance:
Now we will simulate this PDA for the input string "0011100".
PDA Acceptance
A language can be accepted by Pushdown automata using two approaches:
1. Acceptance by Final State: The PDA is said to accept its input by the final state if it
enters any final state in zero or more moves after reading the entire input.
Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language acceptable by the final state can
be defined as:
2. Acceptance by Empty Stack: On reading the input string from the initial
configuration for some PDA, the stack of PDA gets empty.
Skip Ad
Let P =(Q, ∑, Γ, δ, q0, Z, F) be a PDA. The language acceptable by empty stack can be
defined as:
Example:
Construct a PDA that accepts the language L over {0, 1} by empty stack which accepts
all the string of 0's and 1's in which a number of 0's are twice of number of 1's.
Solution:
We are going to design the first part i.e. 1 comes before 0's. The logic is that read single
1 and push two 1's onto the stack. Thereafter on reading two 0's, POP two 1's from the
stack. The δ can be
Now, consider the second part i.e. if 0 comes before 1's. The logic is that read first 0,
push it onto the stack and change state from q0 to q1. [Note that state q1 indicates that
first 0 is read and still second 0 has yet to read].
Being in q1, if 1 is encountered then POP 0. Being in q1, if 0 is read then simply read
that second 0 and move ahead. The δ will be:
In this tutorial we define those symbols that do not participate in derivation of any string, i.e.
the useless symbols, and remove the useless productions from the grammar.
A symbol X is useful if:
1. If X is generating, i.e., X => w, where w ϵ L(G) and w in V *, this means that the
*
t
A number that is useful is both generating and reachable. For reduction of a given grammar
G:
1. Identify non-generating symbols in the given CFG and eliminate those productions
which contains non-generating symbols.
2. Identify non-reachable symbols and eliminate those productions which contain the
non-reachable symbols
3.
Example: Remove the useless symbol from the given context free grammar:
S -> aB / bX
A -> Bad / bSX / a
B -> aSB / bBX
X -> SBD / aBx / ad
Solution:
A and X directly derive string of terminals a and ad, hence they are useful. Since X is a
useful symbol so S is also a useful symbol as S -> bX. But B does not derive any string w in
V* so clearly B is a non-generating symbol.So eliminating those productions with B in them
t
we get
S -> bX
A -> bSX / a
X -> ad
S -> bX
X -> ad
Question: Find the equivalent useless grammar from the given grammar
X -> Xz / xYz
Y -> yYy / Xz
Z -> Zy / z
1. Select a unit production A -> B, such that there exist a production B -> α, where α is
a terminal
S -> AB
A -> a
B -> C / b
C -> D
D -> E
E -> a
Solution:
B -> C
C -> D
D -> E
For production D -> E there is E -> a so we add D -> a to the grammar and add D -> E from
the grammar. Now we have C -> D so we add a production C -> a tp the grammar and
delete C -> D from the grammar.
Similarly we have B -> C by adding B -> a and removing B -> C we get the final grammar
free of unit production as:
S -> AB
A -> a
B -> a / b
C -> a
D -> a
E -> a
We can see that C, D and E are unreachable symbols so to get a completely reduced
grammar we remove them from the CFG. The final CFG is :
S -> AB
A -> a
B -> a / b
Question: Identify and remove the unit productions from the following CFG
S -> S + T/ T
T -> T * F/ F
F -> (S)/a
For example:
1. G1 = {S → AB, S → c, A → a, B → b}
2. G2 = {S → aA, A → a, B → c}
The production rules of Grammar G1 satisfy the rules specified for CNF, so the
grammar G1 is in CNF. However, the production rule of Grammar G2 does not satisfy
the rules specified for CNF as S → aZ contains terminal followed by non-terminal. So
the grammar G2 is not in CNF.
1. S1 → S
Step 2: In the grammar, remove the null, unit and useless productions. You can refer to
the Simplification of CFG.
Step 3: Eliminate terminals from the RHS of the production if they exist with other non-
terminals or terminals. For example, production S → aA can be decomposed as:
1. S → RA
2. R → a
Step 4: Eliminate RHS with more than two non-terminals. For example, S → ASB can
be decomposed as:
1. S → RS
2. R → AS
Example:
Convert the given CFG to CNF. Consider the given grammar G1:
1. S → a | aA | B
2. A → aBB | ε
3. B → Aa | b
Solution:
Step 1: We will create a new production S1 → S, as the start symbol S appears on the
RHS. The grammar will be:
1. S1 → S
2. S → a | aA | B
3. A → aBB | ε
4. B → Aa | b
Step 2: As grammar G1 contains A → ε null production, its removal from the grammar
yields:
1. S1 → S
2. S → a | aA | B
3. A → aBB
4. B → Aa | b | a
1. S1 → S
2. S → a | aA | Aa | b
3. A → aBB
4. B → Aa | b | a
Also remove the unit production S1 → S, its removal from the grammar yields:
1. S0 → a | aA | Aa | b
2. S → a | aA | Aa | b
3. A → aBB
4. B → Aa | b | a
1. S0 → a | XA | AX | b
2. S → a | XA | AX | b
3. A → XBB
4. B → AX | b | a
5. X → a
Step 4: In the production rule A → XBB, RHS has more than two symbols, removing it
from grammar yield:
1. S0 → a | XA | AX | b
2. S → a | XA | AX | b
3. A → RB
4. B → AX | b | a
5. X → a
6. R → XB
Lemma
If L is a context-free language, there is a pumping length p such that any string w ∈
L of length ≥ p can be written as w = uvxyz, where vy ≠ ε, |vxy| ≤ p, and for all i ≥ 0,
uv xy z ∈ L.
i i
Problem
Find out whether the language L = {x y z | n ≥ 1} is context free or not.
n n n
Solution
Let L is context free. Then, L must satisfy pumping lemma.
At first, choose a number n of the pumping lemma. Then, take z as 0 1 2 .
n n n
Union
Concatenation
Kleene Star operation
Union
Let L and L be two context free languages. Then L ∪ L is also context free.
1 2 1 2
Example
Let L = { a b , n > 0}. Corresponding grammar G will have P: S1 → aAb|ab
1
n n
1
Union of L and L , L = L ∪ L = { a b } ∪ { c d }
1 2 1 2
n n m m
Concatenation
If L and L are context free languages, then L L is also context free.
1 2 1 2
Example
Union of the languages L and L , L = L L = { a b c d }
1 2 1 2
n n m m
Kleene Star
If L is a context free language, then L* is also context free.
Example
Let L = { a b , n ≥ 0}. Corresponding grammar G will have P: S → aAb| ε
n n
Kleene Star L = { a b }* 1
n n