Theory of Computation Pushdown Automata pda Lecture 10

  • Slides: 50
Download presentation
Theory of Computation Pushdown Automata pda Lecture #10

Theory of Computation Pushdown Automata pda Lecture #10

Push-Down Automata • Recall that DFAs accept regular languages. • We want to design

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.

Pushdown Automaton -- PDA Generally, the PDA is a machine with following components: Input

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

The figure shows the following: • An input tape is divided into cells. Each

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.

 • The stack containing a symbol from a finite set Г, called the

• 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.

 • The input for the PDA is a string in Σ*. This string

• 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

 • In one computation step the PDA does one of the following: 1.

• In one computation step the PDA does one of the following: 1. 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).

 • The tape head either moves one cell to the right or stays

• 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 * Г

Definition: a deterministic PDA is a 7 -tuple (Q, Σ, Γ, δ, Z, q,

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 $.

q Q : is the start state. Z Г : is the initial stack

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.

Transitions • Let p Q, a Σ {□}, and A Г. Further more let

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

then – The PDA switches to state q, – The head moves according to

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 It can be drawn as p p a, A; w a, A → w q q

Configurations • A configuration fully describes the current “state” of the PDA. • Start

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 a 1, a 2, a 3, …, and the stack contains only the special symbol $.

 • Computation & Termination: start in the starting configuration, the PDA performs a

• 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.

Accepting string the PDA accepting the input staring a 1, a 2, a 3,

Accepting string the PDA accepting the input staring a 1, a 2, a 3, …, an Σ*, if 1. The PDA terminates at this input string, and 2. 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 □)

Accepting Strings • One may define acceptance “by final state” only. – The input

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.

PDA behavior The behavior of the described PDA can be explained as follows: Initial

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

The States Input symbol Pop symbol Push symbol

The States Input symbol Pop symbol Push symbol

One step computation input stack top Replace

One step computation input stack top Replace

input stack top Push

input stack top Push

input stack top Pop

input stack top Pop

input stack top No Change

input stack top No Change

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

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

A Possible Transition input stack top Pop

A Possible Transition input stack top Pop

PDA & Languages (CFL)

PDA & Languages (CFL)

Example Design a PDA that accept the n n language L = {0 1

Example Design a PDA that accept the n n language L = {0 1 | 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 q 0 to state q 1, pushing the bottom-of-the-stack marker onto the stack.

 • In state q 1, if the machine reads an 0 from the

• In state q 1, 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 q 1 and it finds an 0 in the input, there will be an A to top of the stack.

 • The machine then pushes two A's back on the stack, one to

• 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 q 2 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 q 3, an accept state.

 • Since the language includes the empty string, we also have a transition

• Since the language includes the empty string, we also have a transition from state q 1 to state q 3 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 = {q 0, q 1, q 2, q 3}, Σ = {0, 1}, Γ = {$, A}, q 0 is the start state, F = {q 3}, and the transition function δ will be given by:

q 0 □ □ → q 1 R $, pushes $ onto the stack

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

Another way to design the PDA is: The PDA, uses only two states: state

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

Based on this discussion, a deterministic PDA, is M = (Q, Σ, Γ, δ,

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

q 0 □ S → q 0 N S, input consists only of 0’s;

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

DPDA Let M = (Q, Σ, Γ, δ, q, Z, F) be a pda.

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.

Example Design a dpda that accept the language L = {x ϵ {a, b}*

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

Transition tale for dpda with ε-transition that accepts L Move no. state input Stack

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

Transition tale for dpda with no ε-transition that accepts L Move no. state input

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

This can be shown as in the following transition diagram a, b/ε a, Z/a.

This can be shown as in the following transition diagram a, b/ε a, Z/a. Z b, Z/b. Z q 0 b, b/bb a, Z/Z b, Z/Z a, a/aa q 1 b, a/ε

Machine operation can be illustrated based on input string abbabaa as follows: (q 0

Machine operation can be illustrated based on input string abbabaa as follows: (q 0 , abbabaa, Z ) (q 1 , bbabaa, Z ) (q 0 , abaa, b. Z ) (q 0 , baa, Z ) (q 0 , aa, b. Z ) (q 0 , a, Z ) (q 1 , ε, Z ) accepted

NPDA The NPDA model is similar to the PDA, where it 7 -tuples, M

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

states Q is a finite set of Σ is the set of input alphabet

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

Same as the PDA, the language L is said to be accepted by the

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).

DPDA vs. NPDA • Deterministic PDA’s – Every state/stack combination has exactly one transition

DPDA vs. NPDA • Deterministic PDA’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 3 x as many a’s as b’s

Example Design an NPDA, that can be used to recognized/accept the following language L,

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 (q 0 as start state and q 1 as accepting state) with following meaning:

 • If the NPDA at state q 0, then it hasn’t reached the

• If the NPDA at state q 0, then it hasn’t reached the middle symbol of the input string. • If the NPDA at state q 1, 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.

 • At state q 0, when reading the current input symbol, it performs:

• At state q 0, 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 q 1 • At state q 1, 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.

Based on this discussion a NPDA, M = (Q, Σ, Γ, δ, q, F)

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

q 0 a $ → q 0 R $S, pushes S onto the stack

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

q 1 □ $ → q 0 N ε, input accepted q 1 □

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