Transcript
Pushdown Automata
1Welcome
Working Principle:Pushdown Automata Vs Turing MachinesAbhishek ShivhareDeptt. Of CSEITSEC, Gr. Noida2
Finite AutomataRecall 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
Requirement of a memory:An illusion : anbn is accepted by:
It doesnt guarantee equal nos. of as and bs:It can accept any no. of as and any no. of bs(as = bs OR as bs )4Initial StateFinal state a bb
Requirement of a memoryTo handle a language like {anbn | n 0}, the machine needs to remember the number of as.To do this, we use a stack.A push-down automaton (PDA) is essentially an NFA with a stack.
5
Definition of a Push-Down AutomatonA push-down automaton (PDA) is a six-tuple (K, , , , s, F) whereK 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 *).
PDA ; NFA With a Stack:
a1a2
Control
Input
Stack
7
TransitionsLet ((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.
ConfigurationsA 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, *, *).
Case Study for wcwT:
(q0, a, $)= (q1, a$)(q0, b, $)= (q1, b$)PUSH starting symbol onto stack (a/b) and change state10
Input:a a b b c b b a a $
Case Study for wcwT:
(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$
Case Study for wcwT:
(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 $
Case Study for wcwT:
(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 $
Case Study for wcwT:
(q2, a, a)= (q2, )(q2, b, b)= (q2, )POP moves continue until a mismatch
14
Input:a a a a $
Case Study for wcwT:
(q2, , $)= (qf, $)
Final state comes into picture as input exhausts and stack becomes empty!Hence our string is accepted as valid15
Input: $
PDA for wcwT :(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, $)
16Initial (PUSH) move
PUSH Move
No Stack Operation
POP Move
Acceptance
Case Study: anbn All as are pushed into stackOn finding b on input, popping off as may be achieved At last if all input is exhausted and stack becomes empty result achieved.17
Push-Down AutomataStack 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 = k18
Push-Down AutomataBut 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
From CFGs to Stack MachinesA: Use a three phase process: Push mode: Before reading #, push everything on the stack. Reading # switches modes. 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
From CFGs to Stack Machines 21(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
From CFGs to Stack Machines 22(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
Input:aaab#baa
From CFGs to Stack Machines 23(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
a
Input:aaab#baa
From CFGs to Stack Machines 24(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
aa
Input:aaab#baa
From CFGs to Stack Machines 25(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
aaa
Input:aaab#baa
From CFGs to Stack Machines 26(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
baaa
Input:aaab#baa
From CFGs to Stack Machines 27(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
baaa
Input:aaab#baa
From CFGs to Stack Machines 28(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
aaa
Input:aaab#baa
From CFGs to Stack Machines 29(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
aa
Input:aaab#baa
From CFGs to Stack Machines 30(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
a
Input:aaab#baaREJECT (nonempty stack)
From CFGs to Stack Machines 31(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
aa
Input:aaab#baaa
From CFGs to Stack Machines 32(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
a
Input:aaab#baaa
From CFGs to Stack Machines 33(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
Input:aaab#baaaPause input
From CFGs to Stack Machines 34(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
Input:aaab#baaaACCEPT
From CFGs to Stack Machines 35(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
aa
Input:aaab#baaaa
From CFGs to Stack Machines 36(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
a
Input:aaab#baaaa
From CFGs to Stack Machines 37(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
Input:aaab#baaaaPause input
From CFGs to Stack Machines 38(1)PUSH(3)POPread a or b ?Push it
ACCEPT(2)read # ?(ignore stack)read == peek ?PopElse: CRASH!empty stack?
Input:aaab#baaaaCRASH
Sipsers Version
39
e , e$
a , eab , eb
#, eee , $ea , aeb , be
Input:aaab#baaaACCEPT!
Turing Machines
40
BackgroundAutomata accepts regular languages onlyFor example, {anbn : n = 0, 1, } is not regularPushdown Automata accepts context-free languages onlyFor example, {anbncn : w *} is not context-free
41
Basic IdeaTuring decomposed operations in the device as follows:Computational steps erase a symbol observed by the pencil and write a new one in its placeThe decision about which symbol should be written and which will be observed next depend on:A pencil will be observing symbols from a paperThe current symbol being observedThe state of mind of the deviceThis device is called a Turing MachineThe pencil can move left and right
42
Expressivity of Turing Machines (TMs)TMs can simulate any data structure
TMs can simulate major components of imperative languages: sequence, branching and loop
TMs can control branching and loops
43
Informal Description
a1a2
Control
Tape
cell
headThe head:Reads the symbol from the cell it is pointing to, andEither:Writes a new symbol in the cell, orMoves one cell to the left or right
44
Informal Description (II)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
45
Preliminaries to Formal Description denotes the alphabet of characters in language as usual.
denotes the set of symbols that can be written on tapeIt 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
46
Preliminaries to Formal Description (II) 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
47
Formal DefinitionDefinition. A Turing machine is a 7-tuple (Q, , , , q0, qaccept, qreject), 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
48
Formal Definition (II) is a collection of transitions defined by the function:
: (Q {qaccept, qreject }) Q {L , R}
49
Case Study:Lets take a case of a3b3c3Moves are :aaabbbccc#aabbbccc#aa#bbccc#aa#bb#cc##a#bb#cc##a##b#cc##a##b##c#####b##c########c#########50
a a b b b c c cabc#q1#Rq2q2q3q4q5q6q7
51q7
# a a b b b c c cabc#q1#Rq2q2aRq2q3q4q5q6q7
52q7
# a a b b b c c cabc#q1#Rq2q2aRq2q3q4q5q6q7
53q7
# a a b b b c c cabc#q1#Rq2q2aRq2#Rq3q3q4q5q6q7
54q7
# a a # b b c c cabc#q1#Rq2q2aRq2#Rq3q3bRq3q4q5q6q7
55q7
# a a # b b c c cabc#q1#Rq2q2aRq2#Rq3q3bRq3q4q5q6q7
56q7
# a a # b b c c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4q5q6q7
57q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5q6q7
58q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5#Lq5q6q7
59q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5bLq5#Lq5q6q7
60q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5bLq5#Lq5q6q7
61q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5bLq5#Lq5q6q7
62q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5aLq6bLq5#Lq5q6q7
63q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5aLq6bLq5#Lq5q6aLq6q7
64q7
# a a # b b # c cabc#q1#Rq2q2aRq2#Rq3q3bRq3#Rq4q4cLq5q5aLq6bLq5#Lq5q6aLq6#Rq1q7
65q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accepting anbncnabc#q1#Rq2#Rq1q2aRq2#Rq3#Rq2q3bRq3#Rq4#Rq3q4cLq5#Lq7q5aLq6bLq5#Lq5q6aLq6#Rq1q7
89q7
90Comparison with Previous Models
DeviceSeparate Input?Read/Write Data StructureDeterministic by default?FAPDATM
901 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
91Comparison with Previous Models
DeviceSeparate Input?Read/Write Data StructureDeterministic by default?FAYesNoneYesPDATM
911 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
92Comparison with Previous Models
DeviceSeparate Input?Read/Write Data StructureDeterministic by default?FAYesNoneYesPDAYesLIFO StackNoTM
921 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
93Comparison with Previous Models
DeviceSeparate Input?Read/Write Data StructureDeterministic by default?FAYesNoneYesPDAYesLIFO StackNoTMNo1-way infinite tape. 1 cell access per step.Yes(but will also allow crashes)
931 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
94Comparison with Previous Models
DeviceType of GrammarString/LanguageFinite AutomataRegular(Type 3)an
941 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
95Comparison with Previous Models
DeviceType of GrammarString/LanguageFinite AutomataRegular(Type 3)anPush Down AutomataContext Free(Type 2)anbn
951 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
96Comparison with Previous Models
DeviceType of GrammarString/LanguageFinite AutomataRegular(Type 3)anPush Down AutomataContext Free(Type 2)anbn
Linear Bounded AutomataContext Sensitive(Type 1)N.A.
961 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
97Comparison with Previous Models
DeviceType of GrammarString/LanguageFinite AutomataRegular(Type 3)anPush Down AutomataContext Free(Type 2)anbn
Linear Bounded AutomataContext Sensitive(Type 1)N.A.Turing MachineUnrestricted (Type 0)anbncn
971 Students (and others) often complain about allowing unbounded/infinite tape in the description of a supposedly physically realizable machine. If this bothers you, think instead that the machine has a finite tape, but that another finite tape is added, every time the machine runs out of memory.
98Questions???
99
THANKS
top related