theory of automata and formal languages unit 4
TRANSCRIPT
05/01/2023 Abhimanyu Mishra(CSE) JETGI
THEORY OF AUTOMATA AND FORMAL LANGUAGES
UNIT-IV
ABHIMANYU MISHRAASSISTANT PROF.(CSE)
JETGI
05/01/2023 Abhimanyu Mishra(CSE) JETGI
PUSHDOWN AUTOMATA INTRODUCTION
“The pushdown automata is essentially a finite automata with control of both an input tape and a stack to store what it has read. A stack is “FIRST IN FIRST OUT” list, that is, symbols can be entered or removed only at the top of the list. When a new symbol is entered at the top, the symbol previously at the top becomes second and so on”.Basically a pushdown automaton is − "Finite state machine" + "a stack“A pushdown automaton has three components −(i) an input tape,(ii) a control unit(iii) a stack with infinite size.
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Diagram
a b c b a c ……………..Input Tape
a c b a
Finite Control
05/01/2023 Abhimanyu Mishra(CSE) JETGI
DEFINATION OF PUSHDOWN AUTOMATA
A pushdown automata is a system, which is mathematically defined as follows:
P = ( Q,∑,┌,δ,s,F)
Where Q : non-empty finite set of states ∑ : non-empty finite set of input symbols ┌ : is finite set of pushdown symbols s : is the initial state, s ∈ Q F : is the set of final states and F Q δ : It is a transition function or transition relation.
05/01/2023 Abhimanyu Mishra(CSE) JETGI
THERE ARE TWO DIFFERENT WAYS TO DEFINE PDA ACCEPTABILITY.
(i) Final State Acceptability
In final state acceptability, a PDA accepts a string when, after reading the entire string, the PDA is in a final state. From the starting state, we can make moves that end up in a final state with any stack values. The stack values are irrelevant as long as we end up in a final state.
For a PDA (Q,∑,┌,δ,s, F) the language accepted by the set of final states F is − L(PDA) = {w | (q0, w, I) ⊢* (q, ε, x), q F}∈for any input stack string x
05/01/2023 Abhimanyu Mishra(CSE) JETGI
(ii) Empty Stack Acceptability
Here a PDA accepts a string when, after reading the entire string, the PDA has emptied its stack.
For a PDA (Q, ∑,┌,δ,s, F), the language accepted by the empty stack is −L(PDA) = {w | (q0, w, I) ⊢* (q, ε, ε), q Q}∈
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Example: Design a PDA which accepts the language L ={ w {a,b}*/w has equal no of a’ and b’s}∈
Now let us assume that PDA be P P = (Q,∑,┌,δ,s,F) Q = { s,q,f} ∑ = {a,b} ┌ = {a,b,c} F = {f} , and δ is as followsNow let us assume stack have c value in initial in stack,
05/01/2023 Abhimanyu Mishra(CSE) JETGI
(i) ((s, , ),(q,c))∈ ∈(ii) ((q,a,c),(q,ac))(iii) (q,a,a),(q,aa))(iv) ((q,a,b),(q, ))∈(v) ((q,b,c),(q,bc))(vi) ((q,b,b),(q,bb))(vii) ((q,b,a),(q, ))∈(viii) ((q, ,c),(f, ))∈ ∈
Now if PDA reads symbol ’a’ from the input tape and if stack is empty or has ‘a’ on the stack top then push ‘a’ into the stack, Now if reads ‘a’ from input and ‘b’ is on top of the stack then. It simply pops the symbol ‘b’ and pushes nothing.So final state is defined as, PDA is in state f and stack is empty
05/01/2023 Abhimanyu Mishra(CSE) JETGI
PUSHDOWN AUTOMATA AND CONTEXT-FREE-GRAMMARS
It should be clear now that PDA can recognize any language for which there exists a CFG. “ That is class of language accepts by pushdown automata is exactly the class of context-free languages”.
(i) Algorithm to find PDA corresponding to a given CFG(ii)Algorithm to find CFG corresponding to a given PDA
05/01/2023 Abhimanyu Mishra(CSE) JETGI
(I) ALGORITHM TO FIND PDA CORRESPONDING TO A GIVEN CFG
Input : A CFG, G = (Vn,Vt,P,S)Output: Equivalent PDA, P= (Q, ∑, ┌, δ, q0, F) Steps: (i) Convert of the production of CFG to GNF (ii) The PDA has only one state q0 (iii) The start symbol of CFG will be the start symbol in the PDA (iv) All non-terminals of the CFG will be the stack symbols of the PDA and all the terminals of the CFG will be the input symbols of the PDA. (v) For each production in the form A → aX where a is terminal and A, X are combination of terminal and non-terminals make a transition δ (q, a, A).
05/01/2023 Abhimanyu Mishra(CSE) JETGI
ExampleConstruct a PDA from the following CFG.G = ({S, X}, {a, b}, P, S)where the productions are −S → XS | ε , A → aXb | Ab | ab
Solution
Let the equivalent PDA,P = ({q}, {a, b}, {a, b, X, S}, δ, q, S)where δ −δ (q, ε , S) = {(q, XS), (q, ε )}δ(q, ε , X) = {(q, aXb), (q, Xb), (q, ab)}δ(q, a, a) = {(q, ε )}δ(q, 1, 1) = {(q, ε )}
05/01/2023 Abhimanyu Mishra(CSE) JETGI
(II) ALGORITHM TO FIND CFG CORRESPONDING TO A GIVEN PDA
Input : A CFG, G = (Vn,Vt,P,S)Output : Equivalent PDA, P = (Q, ∑, ┌, δ, q0, F)such that the non- terminals of the grammar G will be {Xwx | w,x Q} and the start ∈ state will be Aq0,F.Steps: (i) For every w, x, y, z Q, m ┌ and a, b ∑, if δ (w, a, ε) contains (y, ∈ ∈ ∈ m) and (z, b, m) contains (x, ε), add the production rule Xwx → a Xyzb in grammar G. (ii) For every w, x, y, z Q, add the production rule X∈ wx → XwyXyx in grammar G. (iii) For w Q, add the production rule X∈ ww → ε in grammar G.
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Example: Design a PDA for the following CFG, G = (Vn,Vt,P,S) with Vn ={S}, Vt ={(,)} and P is defined δ as follows
S → (S) | ε|SSSolution: Lets corresponding PDA will be P = (Q, ∑, ┌, δ, q0, F) Q={p,q} ∑ = {(,)}
┌ = (S,(,)} that is terminal and non-terminal in grammar S =p F = {q}, and δ is as,
05/01/2023 Abhimanyu Mishra(CSE) JETGI
(1) ((p, ε , ε) , {(q, S))(2) ((q, ε , S) ,{(q, ε )) because S → is a rule of CFG∈(3) ((q, ε, S) ,(q, SS))(4) ((q, ε,S) , (q, (S))(5) ((q, (,(),(q, ε))(6) (((q,),)),(q,(q,ε)) for each a ∈ Vt
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Now apply these transition relation on the string w=( )( )
S.N State Unread input
Stack Transition used
1 q ( ) ( ) ∈ -2 q ( ) ( ) S (1)3 q ( ) ( ) SS (3)4 q ( ) ( ) (S)SS (4)5 q ) ( ) S)S (5)6 q )( ) )S (2)7 q ( ) S (6)8 q ( ) (S) (4)9 q ) S) (5)10 q ) ) (2)11 q ∈ ∈ (6)
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Example: Consider the following PDA and construct equivalent CFG
1. ((q0,a,$) →(q0,a$))2. ((q0,b,$) →(q0,b$))3. ((q0,a,a) →(q0,aa))4. ((q0,b,b) →(q0,bb))5. ((q0,a,b) →(q0,ab))6. ((q0,b,a) →(q0,ba))7. ((q0,c,$) →(q1,$))8. ((q0,c,a) →(q1,a))9. ((q1,c,b) →(q1,b))10. ((q1,a,a) →(q1, ))∈11. ((q1,b,b) →(q1, ))∈12. ((q1, ,$) →(q∈ 1, ))∈
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Sol: CFG using above construction production rule is as follows: S→[q0 $ q0 ]/ [q0 $ q1 ] rule (i) [q0 a q0 ] → a [q0 b q1 ] → b [q1 $ q1 ] → rule(ii)∈
Now remaining productions are:-
[q0 $ q0 ] → a [q0 a q0 ]/ [q0 $ q0 ] [q0 $ q1 ] → a [q0 a q0 ]/ [q0 $ q1 ] [q0 $ q1 ] → a [q0 a q1 ]/ [q1 $ q1 ] [q0 $ q1 ] → b [q0 b q0 ]/ [q0 $ q0 ] [q0 $ q1 ]→ b [q0 b q0 ]/ [q0 $ q1 ] [q0 $ q1 ] → b [q0 b q1 ]/ [q1 $ q1 ]
05/01/2023 Abhimanyu Mishra(CSE) JETGI
[q0 a q0 ] → a [q0 a q0 ]/ [q0 a q0 ] [q0 a q1 ] → a [q0 a q0 ]/ [q0 a q1 ] [q0 a q1 ] → a [q0 a q1 ]/ [q1 a q1 ] [q0 a q0 ] → b [q0 b q0 ]/ [q0 a q0 ] [q0 a q1 ] → b [q0 b q0 ]/ [q0 a q0 ] [q0 a q1 ] → b [q0 b q1 ]/ [q1 a q1 ] [q0 b q0 ] → a [q0 a q0 ]/ [q0 b q0 ] [q0 b q1 ] → a [q0 a q0 ]/ [q0 b q1 ] [q0 b q1 ] → a [q0 a q1 ]/ [q1 b q1 ]
05/01/2023 Abhimanyu Mishra(CSE) JETGI
[q0 b q0 ] → b [q0 b q0 ]/ [q0 b q0 ] [q0 b q0 ] → b [q0 b q0 ]/ [q0 b q1 ] [q0 b q0 ] → b [q0 b q1 ]/ [q1 $ q1 ] [q0 $ q1 ] → c [q0 b q1 ] [q0 a q1 ] → c [q1 a q1 ] [q0 b q0 ] → c [q1 b q1 ]
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Problems?
1. Construct PDA for language L = {(ab)n /n>=1}.2. Construct a PDA to accept the language L = { set of all words in a and b with an even numbers of a’s}.3. Construct a PDA for the regular expression r = 0*1+
05/01/2023 Abhimanyu Mishra(CSE) JETGI
TWO STACK PDA
A two-stack PDA is a sixtuple (Q, , , , q0, F), where Q, , , q0, and F are the same as in a one-stack PDA. The transition function as follow:
: Q ( { }) ( { }) ( { }) to the set of all subsets of Q ( { }) ( { })
The two stacks of the PDA are independent. one stack: two stack:
A/B A/B/C
Two-Stack PDA accepts any language that accepted by a Turing Machine.
05/01/2023 Abhimanyu Mishra(CSE) JETGI
TWO STACK PDA
(i) A Turing machine can accept languages not accepted by any PDA with one stack.
(ii) The strength of pushdown automata can be increased by adding other stacks.
(iii) Actually, a PDA with two stacks have the same computation control as a Turing Machine.
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Diagram of TWO STACK PDA
INPUT TAPE
TAPE HEAD HEAD MOVES
STACK 1 STACK 2
Finite Control
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Theorem 8.13 (Hopcroft and Ullman [1])If a language L is accepted by a Turing machine, L is accepted by a Two-Stack machine.
The idea is that the first stack can hold what is to the left of the head, while the second stack holds what is to the right of the head, neglecting all the infinite blank symbols beyond the leftmost and rightmost of the head.
The proof is taken from (Hopcroft and Ullman [1])
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Let’s L be L(M) for some one tape TM M, two-stack machine S, simulating a one-tape TM M as the following:
S begins with a bottom-of-stack marker on each stack, this marker considered the start symbol for the stacks, and must not appear elsewhere on the stacks. The marker indicates that the stack is empty.
The proof is taken from (Hopcroft and Ullman [1])
05/01/2023 Abhimanyu Mishra(CSE) JETGI
Suppose that w$ is on the input of S. S copies the input w onto its first stack, and stops to copy when reading the end marker on the input.S pops each symbol in turn from its first stack and pushes it onto its second stack. The first stack of S is empty. The second stack holds w, with the left end of w is at the top.