Presentation is loading. Please wait.

Presentation is loading. Please wait.

Theory of Computation Pushdown Automata pda Lecture #10.

Similar presentations


Presentation on theme: "Theory of Computation Pushdown Automata pda Lecture #10."— Presentation transcript:

1 Theory of Computation Pushdown Automata pda Lecture #10

2 Push-Down Automata Recall that DFAs accept regular languages.
We want to design machines similar to DFAs that will accept context-free languages (CFL). These machines will need to be more powerful. To handle a language like {anbn | n  0 }, the machine needs to “remember” the number of as. To do this, a stack will be used. A push-down automaton (PDA) is essentially an NFA with a stack.

3 Pushdown Automaton -- PDA
Generally, the PDA is a machine with following components: Input String (Tape) Stack States

4 The figure shows the following:
An input tape is divided into cells. Each cell containing a symbol belonging to a finite set Σ, called the tape alphabet. There is a special symbol □ that is not contained in Σ, this symbol is called the blank symbol. If the cell contains □ this means the cell is empty. The tape contains a head that can move a long the tape, one cell to the right per move, also it can read the cell currently under scan.

5 The stack containing a symbol from a finite set Г, called the stack alphabet. This set containing a special symbol $. The stack contains a head that can read a top symbol of the stack.. It can pop the top symbol, it is also can push a symbol of Г onto the stack. There is a state control, which can be any one of the finite number of the states. The set of states is denoted by Q. the set Q contains one special state q, called the start state.

6 The input for the PDA is a string in Σ
The input for the PDA is a string in Σ*. This string is sorted in the input tape, and the tape head is initially at the leftmost symbol of the input string. Input Initially, the stack contains only the special symbol $, and the PDA is at the start state q. Stack Start state

7 In one computation step the PDA does one of the following:
Assume that the PDA currently at state r, a is input symbol of Σ that is read by the tape head, and A is the symbol of Г that is at the top of the stack. 2. Depending on the current state r, the tape symbol a and the stack symbol A: The PDA switches to a state r’ of Q (which may be equal to r).

8 The tape head either moves one cell to the right or stays at the current cell, and
The top symbol A is replaced by string w of Г*

9 Definition: a deterministic PDA is a 7-tuple (Q, Σ, Γ, δ, Z, q, F) where,
Q : is a finite set of states. Σ : is a finite alphabet of tape symbols, which is not containing the blank symbol □. Γ : is a finite alphabet of stack symbols, that contains special symbol $.

10 Z  Г : is the initial stack symbol.
q  Q : is the start state. Z  Г : is the initial stack symbol. F  Q : is the set of final states. δ : is the transition function, that is defined as: δ : Q  (Σ  {□})  Γ → Q  {R,N}  Γ* The transition function δ is considered as the program that tells what the PDA can do in one computation step.

11 Transitions Let p Q, a  Σ  {□}, and A  Г. Further more let q  Q, σ  {N, R} and w  Г* be such that δ(p, a, A) = (q, σ , w) ** This transition means that: The PDA is in state p. The tape head reads the symbol a from the tape, and A is the top symbol on the stack

12 The PDA switches to state q,
then The PDA switches to state q, The head moves according to σ: if σ = R, then it moves one cell to the right; if σ = N then it doesn’t move, and The symbol A on the stack replaced by the string w Note that ** above can be written in the following form: p a A → q σ w p q a, A; w It can be drawn as p q a, A → w

13 Configurations A configuration fully describes the current “state” of the PDA. Start configuration: initially, the PDA is at the start state q, the tape head is at the leftmost symbol of the input staring a1,a2,a3,…,an, and the stack contains only the special symbol $.

14 Computation & Termination: start in the starting configuration, the PDA performs a sequences of computation steps as described above. It terminates at the moment when the stack gets empty. (hence if the stack never becomes empty, the PDA doesn’t terminate.

15 the PDA accepting the input staring a1,a2,a3,…,an  Σ*, if
Accepting string the PDA accepting the input staring a1,a2,a3,…,an  Σ*, if The PDA terminates at this input string, and At the time of termination (when the stack gets empty) the tape head is immediately to the right of the symbol an (this cell must contain the blank symbol □)

16 Accepting Strings One may define acceptance “by final state” only.
The input is accepted if and only if the last state is a final state, regardless of whether the stack is empty. One may define acceptance “by empty stack” only. The input is accepted if and only if the stack is empty once the input is processed, regardless of which state the PDA is in.

17 PDA behavior Initial Stack Symbol
The behavior of the described PDA can be explained as follows: Initial Stack Symbol Stack bottom special symbol stack head top Appears at time 0

18 The States Input symbol Pop Push

19 One step computation input top stack Replace

20 input top stack Push

21 input top stack Pop

22 input top stack No Change

23 input top stack empty Pop Empty Stack
The PDA HALTS, No possible transition after

24 A Possible Transition input top stack Pop

25 PDA & Languages (CFL)

26 Example Design a PDA that accept the language L = {0n1n | n ≥ 0}.
The PDA will be designed in the following way: Machine begins with its input on the input tape and an empty stack. The first thing the machine does is go from state q0 to state q1, pushing the bottom-of-the-stack marker onto the stack.

27 In state q1, if the machine reads an 0 from the input tape and pops the blank off the stack, then that is the first 0 found in the input and the machine pushes the blank back onto the stack followed by an A. The machine counts the 0's in the input by pushing an A onto the stack for each 0 that it reads off the tape . From this point on, if the PDA is in state q1 and it finds an 0 in the input, there will be an A to top of the stack.

28 The machine then pushes two A's back on the stack, one to make up for the A that was popped and one to count the 0 just found in the input . As soon as the machine sees a 1 in the input it changes to state q2 and pops an A off the stack . It continues popping an A for each 1 that it finds. If the input was a correctly formatted string, the machine will read a blank off the input tape at the same time that it pops a blank off the stack and go to state q3, an accept state.

29 Since the language includes the empty string, we also have a transition from state q1 to state q3 that is used if the machine reads a blank from the input at the same time as it pops the marker off the stack at the very beginning of processing. Based on this discussion a PDA, M = (Q, Σ, Γ, δ, Z, q, F) where, Q = {q0, q1, q2, q3}, Σ = {0, 1}, Γ = {$, A}, q0 is the start state, F = {q3}, and the transition function δ will be given by:

30 Then, the PDA state diagram can be shown as:
q0 □ □ → q1 R $, pushes $ onto the stack q1 0 $ → q1 R $A, pushes 0 onto the stack q1 0 0 → q1 R AA, pushes 0 onto the stack q1 □ $ → q3 N ε, input string is empty accepted q1 1 A → q2 R A, pop top symbol (0) form the stack q2 1 A → q2 R ε, pop top symbol (0) form the stack q2 □ $ → q3 N ε input string is empty accepted Then, the PDA state diagram can be shown as:

31 q0 q1 □, □ → $ q2 q3 □, $ → □ 0, A→ AA 0, $ → $A 1, A → □

32 Another way to design the PDA is:
The PDA, uses only two states: state q0 and state q1, where q0 is the start state. Initially, the PDA will be in state q0. For each 0 that is read, the PDA pushes one symbol S onto the stack and stay in state q0. When the first 1 is read, then the PDA switches to state q1, from that moment: - for each 1 that is read, the PDA pops the top symbol form the stack, and stay in q1 . - for each 0 that is read, the PDA doesn’t make any changes, and therefore doesn’t terminate.

33 Based on this discussion, a deterministic PDA, is M = (Q, Σ, Γ, δ, Z, q, F) where,
Q = {q0, q1}, Σ = {0, 1}, Γ {$, S}, q0 is the start state, F = {q0,q1}, and the transition function δ will be given by: q0 0 $ → q0 R $S, pushes S onto the stack q0 0 S → q0 R SS, pushes S onto the stack q0 1 $ → q0 N $, first symbol in the input is 1, loop forever q0 1 S → q1 R ε, first symbol 1, encountered q0 □ $ → q0 N ε, input string is empty accepted

34 q1 0 $ → q1 N $, 0 to the right of 1; loop forever
q0 □ S → q0 N S, input consists only of 0’s; loop forever q1 0 $ → q1 N $, 0 to the right of 1; loop forever q1 0 S → q1 N S, 0 to the right of 1; loop forever q1 1$ → q1 N $, too many 1’s; loop forever q1 1 S → q1 R ε, pop top symbol form the stack q1 □ $ → q1 N ε, input string is empty accepted

35 DPDA Let M = (Q, Σ, Γ, δ, q, Z, F) be a pda. M will be deterministic if there is no configuration for which M ha a choice of more that one move. In other words M is deterministic if it satisfies both of the following conditions: 1. For any q ϵ Q , a ϵ Σ  {ε}, and X ϵ Γ, the set δ(q, a, X) has at most one element 2. For any q ϵ Q and X ϵ Γ, if δ(q, ε, X) ≠ ᶲ, then δ(q, a, X) = ᶲ for every a ϵ Σ . Note: a language that accepted dpda is considered to be DCFL.

36 Example Design a dpda that accept the language
L = {x ϵ {a, b}* | na(x) > nb(x)} Sol: M can be designed based on two configuration, first allow the use of ε-transition and other with no ε-transition This can be shown as in the following configurations using the transition table in both cases

37 Transition tale for dpda with ε-transition that accepts L
move Stack symbol input state Move no. (q1, aZ) Z a q0 1 (q0, bZ) b 2 (q0, ε) 3 (q0, bb) 4 (q1, aa) q1 5 6 (q1, a) ε 7

38 Transition tale for dpda with no ε-transition that accepts L
move Stack symbol input state Move no. (q1, Z) Z a q0 1 (q0, bZ) b 2 (q0, ε) 3 (q0, bb) 4 (q1, aZ) q1 5 (q0, Z) 6 (q1, aa) 7 (q1, ε) 8

39 q0 q1 This can be shown as in the following transition diagram a, Z/aZ
a, Z/Z a, Z/aZ b, Z/Z b, Z/bZ a, b/ε b, b/bb a, a/aa b, a/ε

40 Machine operation can be illustrated based on input string abbabaa as follows:
(q0 , abbabaa, Z ) (q1 , bbabaa, Z ) (q0 , babaa, Z ) (q0 , abaa, bZ ) (q0 , baa, Z ) (q0 , aa, bZ ) (q0 , a, Z ) (q1 , ε, Z ) accepted

41 NPDA The NPDA model is similar to the PDA, where it 7-tuples, M = (Q, Σ, Γ, δ, q, Z, F). The difference is that, the next computation step may not be uniquely defined. The automata can make out a choice of a finite number of possible transitions. In this case the transition function δ is given by: δ : Q  (Σ  {□}  {ε})  Γ → Pj(Q  {R,N}  Γ*) Where Pj(K) is the set of all finite subsets of the set K

42 states Q is a finite set of
Σ is the set of input alphabet Гis the stack alphabet q Q is the initial state Z is the stack start symbol, and F Q is a set of final states

43 Same as the PDA, the language L is said to be accepted by the NPDA if all strings in Σ* performing L are accepted/recognized by M, and it is represented as L(M).

44 DPDA vs. NPDA Deterministic PDA’s NPDA’s are more powerful than DPDA’s
Every state/stack combination has exactly one transition No ε-transitions (ε for both input and stack symbol) NPDA’s are more powerful than DPDA’s Example: strings from a*b* that have either twice as many a’s as b’s or 3x as many a’s as b’s

45 Example Design an NPDA, that can be used to recognized/accept the following language L, where L(M) = {vbw :v,w{a,b}*,|v| = |w|}. Sol. The NPDA, can be designed based on two states (q0 as start state and q1 as accepting state) with following meaning:

46 If the NPDA at state q0, then it hasn’t reached the middle symbol of the input string.
If the NPDA at state q1, then it has already reached the middle symbol of the input string. Since the automata makes only one single pass over the input string, it has to guess (nondeterminism) when it reaches the middle of the string.

47 At state q0, when reading the current input symbol, it performs:
- pushes one symbol S onto stake and stay in the same state, or - in case current input symbol is b is guess it has reached to middle of the string and switches to state q1 At state q1, when reading the current input symbol, it pops symbol S from the top of the stack and stay at the state. Then string will be accepted when stack is empty for the first time after the entire input string has been read.

48 Based on this discussion a NPDA, M = (Q, Σ, Γ, δ, q, F) where,
Q = {q0, q1,}, Σ = {a, b}, Γ {$, A}, q0 is the start state, F = {q3}, and, the transition function δ will be given by:

49 q0 a $ → q0 R $S, pushes S onto the stack
q0 a S → q0 R SS, pushes S onto the stack q0 b $ → q1 R $, reached the middle (guess) q0 b S → q0 R $S, not reached the middle q0 b S → q1 R S, reached the middle (guess) q0 b S → q0 R SS, not reached the middle q0 □ $ → q0 N $, loop forever q0 □ S → q0 N S, loop forever q1 a $ → q1 N ε, empty stack, terminate, rejected q1 a S → q1 R ε q1 b $ → q1 N ε, empty stack, terminate, rejected q1 b S → q1 R ε

50 q1 □ $ → q0 N ε, input accepted
q1 □ $ → q1 N S, loop forever For more details and examples on PDAs (DPDA or NDPA) you can refer to the mentioned references.


Download ppt "Theory of Computation Pushdown Automata pda Lecture #10."

Similar presentations


Ads by Google