3. Recall that DFAs accept regular
languages(for e.g. an)
We want to design more powerful
machines similar to DFAs that will accept
context-free languages(for e.g. anbn ).
Final
state
a
4. An illusion : anbn is accepted by:
It doesn’t guarantee equal nos. of a’s and b’s:
It can accept any no. of a’s and any no. of
b’s(a’s = b’s OR a’s ≠ b’s )
4
Initial
State
Final
state
a b
b
5. To handle a language like {anbn | n 0},
the machine needs to “remember” the
number of a’s.
To do this, we use a stack.
A push-down automaton (PDA) is
essentially an NFA with a stack.
5
6. A push-down automaton (PDA) is a six-
tuple (K, Σ, Γ, Δ, s, F) where
• K is a finite set of states.
• Σ is a finite alphabet of tape symbols.
• Γ is a finite alphabet of stack symbols.
• s K is the start state.
• F K is the set of final states.
• Δ is the transition relation, i.e., Δ is a finite subset
of
(K (Σ {e}) Γ*) (K Γ*).
8. Let ((p, a, β), (q, γ)) Δ be a transition.
It means that we
• Move from state p.
• Read a from the tape,
• Pop the string β off the stack,
• Move to state q,
• Push string γ onto the stack.
The first three (p, a, β), are “input.”
The last two (q, γ) are “output.”
9. A configuration fully describes the current
“state” of the PDA.
• The current state p.
• The remaining input w.
• The current stack contents .
Thus, a configuration is a triple
(p, w, ) (K, *, *).
10. ∆(q0, a, $)= (q1, a$)
∆(q0, b, $)= (q1, b$)
PUSH starting symbol onto stack (a/b) and
change state
10
Input:
a a b b c b b a a
$
11. ∆(q1, a, a)= (q1, aa)
∆(q1, b, b)= (q1, bb)
∆(q1, a, b)= (q1, ab)
∆(q1, b, a)= (q1, ba)
PUSH moves continue till occurrence of ‘c’
11
Input:
a a b b c b b a
a$
12. ∆(q1, c, b)= (q2, b)
∆(q1, c, a)= (q2, a)
No movement in stack but state changed
12
Input:
a a b b c
b b a a $
13. ∆(q2, b, b)= (q2, €)
∆(q2, a, a )= (q2, €)
POP moves start as c is passed
13
Input:
a a b b
b b a a $
14. ∆(q2, a, a)= (q2, €)
∆(q2, b, b)= (q2, €)
POP moves continue until a mismatch
14
Input:
a a
a a $
15. ∆(q2, €, $)= (qf, $)
Final state comes into picture as input
exhausts and stack becomes empty!
Hence our string is accepted as valid
15
Input:
€
$
16. ∆(q0, a, $)= (q1, a$)
∆(q0, b, $)= (q1, b$)
∆(q1, a, a)= (q1, aa)
∆(q1, b, b)= (q1, bb)
∆(q1, a, b)= (q1, ab)
∆(q1, b, a)= (q1, ba)
∆(q1, a, c)= (q2, a)
∆(q1, b, c)= (q2, b)
∆(q2, a, a)= (q2, €)
∆(q2, b, b)=(q2, €)
∆(q2, €, $)= (qf, $)
16
Initial (PUSH)
move
PUSH Move
No Stack
Operation
POP Move
Acceptance
17. All a’s are pushed into stack
On finding b on input, popping off a’s may
be achieved
At last if all input is exhausted and stack
becomes empty result achieved.
17
18. Stack uses LIFO, works best for strings
like:
ambncndm | m 0, n 0,
ambmcndn | m 0, n 0,
ambncpdq | m + n = p + q and
ambnck | m = n or n = k
18
19. But has a disadvantage, cannot work for
followings:
anb2ncn | m 0, n 0,
ambm+ncn | m 0, n 0 and
anbncn | n 0 (Turing machines are
efficient for this type of language)
19
20. A: Use a three phase process:
1. Push mode: Before reading “#”, push
everything on the stack.
2. Reading “#” switches modes.
3. Pop mode: Read remaining symbols making
sure that each new read symbol is identical to
symbol popped from stack.
Accept if able to empty stack completely.
Otherwise reject, and reject if could not pop
somewhere.
20
21. 21
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
22. 22
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
Input:
aaab#baa
23. 23
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a
Input:
aaab#baa
24. 24
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a a
Input:
aaab#baa
25. 25
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a a a
Input:
aaab#baa
26. 26
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
b a a a
Input:
aaab#baa
27. 27
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
b a a a
Input:
aaab#baa
28. 28
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a a a
Input:
aaab#baa
29. 29
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a a
Input:
aaab#baa
30. 30
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a
Input:
aaab#baa
REJECT (nonempty stack)
31. 31
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a a
Input:
aaab#baaa
32. 32
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a
Input:
aaab#baaa
33. 33
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
Input:
aaab#baaa
Pause input
34. 34
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
Input:
aaab#baaa
ACCEPT
35. 35
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a a
Input:
aaab#baaaa
36. 36
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
a
Input:
aaab#baaaa
37. 37
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
Input:
aaab#baaaa
Pause input
38. 38
(1)
PUSH
(3)
POP
read a or b ?
Push it
ACCEPT
(2)
read # ?
(ignore stack)
read == peek ?
Pop
Else: CRASH!
empty
stack?
Input:
aaab#baaaa
CRASH
39. 39
e , e$
a , ea
b , eb
#, ee e , $e
a , ae
b , be
Input:
aaab#baaa
ACCEPT!
40.
41. •Automata accepts regular languages only
For example, {anbn : n = 0, 1, …} is not regular
•Pushdown Automata accepts context-free languages only
For example, {anbncn : w *} is not context-free
42. Turing decomposed operations in the device as follows:
•Computational steps erase a symbol
observed by the pencil and write a new one
in its place
•The decision about which symbol should be
written and which will be observed next
depend on:
•A pencil will be “observing” symbols from a paper
1. The current symbol being observed
2. The “state of mind” of the device
This device is called a Turing Machine
•The pencil can move left and right
43. •TMs can simulate any data structure
•TMs can simulate major components of imperative
languages: sequence, branching and loop
•TMs can control branching and loops
45. •New cells can be added to the right of the tape as
needed (similar to RAM memory)
•These new cells contain the blank symbol, #
•The tape is bounded to the left
46. • denotes the alphabet of characters in language as
usual.
• denotes the set of symbols that can be written on tape
•It contains # and all symbols in
• Transitions can be described by (Case I):
((s,a),(bRq)) If the machine is in state s and the
current cell has an a then jump to
state q and write b in the current cell
and moves head to right
47. • Transitions can be described by (Case II):
((s,a),(bLq)) If the machine is in state s and the
current cell has an a then jump to
state q and write b in the current cell
and moves head to left
48. Definition. A Turing machine is a 7-tuple (Q, , , , q0, qaccept, q
where:
•Q is a set of states
• is a set of symbols (the alphabet)
• is a set of symbols that can be written in tape, # and
•q0 Q is the initial state
• qaccept is the accepting state
• qreject is the rejecting state, qreject qaccept
49. • is a collection of transitions defined by the
function:
: (Q {qaccept, qreject }) Q {L , R}
50. Lets take a case of a3b3c3
Moves are :
aaabbbccc
#aabbbccc
#aa#bbccc
#aa#bb#cc
##a#bb#cc
##a##b#cc
##a##b##c
#####b##c
########c
#########
50