0% found this document useful (0 votes)
46 views

Chapter04 PDA (1)

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Chapter04 PDA (1)

Uploaded by

Shafi Esa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Chapter 4: Pushdown

Automata

By Michael T.(MSc.)
4.1 Introduction
▪ Finite automata cannot recognize all context-free languages. This is
because finite automata have strictly finite memories, whereas the
recognition of a context-free language may require storing an
unbounded amount of information.
▪ Pushdown Automata is a finite automata with extra memory called
stack which helps Pushdown automata to recognize Context Free
Languages.
▪ Pushdown automata can store an unbounded amount of information on
the stack.
▪ A pushdown automaton (PDA) can write symbols on the stack and
read them back later.
…cont’d
▪ Writing a symbol “pushes down” all the other symbols on the stack.
▪ At any time the symbol on the top of the stack can be read & removed.
▪ Writing a symbol on the stack is referred to as pushing the symbol,
and removing a symbol is referred to as popping it.
▪ Note that all access to the stack, for both reading and writing, may be
done only at the top. Meaning a stack is a last in first out(FIFO)
storage device.
▪ Any language which can be acceptable by FA can also be acceptable
by PDA.
▪ PDA also accepts a class of language which even cannot be accepted
by FA.
Finite Automata

Pushdown Automata
…cont’d
▪ Pushdown automata can be categorized into: nondeterministic
pushdown automata and deterministic pushdown automata.
▪ Nondeterministic pushdown automata accepts context-free languages
▪ Deterministic pushdown automata defines the deterministic context-
free languages, which is a proper subset of the context-free languages.
4.2 Non-deterministic Pushdown Automata
(NPDA)
Definition – A nondeterministic pushdown automata is defined by the septuple
M = (Q, Σ, Γ, δ, q0, z, F),
where
Q is a finite set of internal states of the control unit,
Σ is the input alphabet,
Γ is a finite set of symbols called the stack alphabet,
δ : Q × (Σ ∪ {λ}) × Γ → set of finite subsets of Q × Γ∗ is
the transition function,
q0 ∈ Q is the initial state of the control unit,
z ∈ Γ is the stack start symbol,
F ⊆ Q is the set of final states.
…cont’d
▪ The arguments of δ are the current state of the control unit, the
current input symbol, and the current symbol on top of the stack.
▪ The result is a set of pairs (q, x), where q is the next state of the
control unit and x is a string that is put on top of the stack in place
of the single symbol there before.
▪ Note that the second argument of δ may be λ, indicating that a move
that does not consume an input symbol is possible.
▪ Note also that δ is defined so that it needs a stack symbol; no move is
possible if the stack is empty.
…cont’d
Example 1
Suppose the set of transition rules of an npda contains
δ (q1, a, b) = {(q2, cd),(q3, λ)}.
If at any time the control unit is in state q1, the input symbol read is a,
and the symbol on top of the stack is b, then one of two things can
happen: (1) the control unit goes into state q2 and the string cd replaces
b on top of the stack, or (2) the control unit goes into state q3 with the
symbol b removed from the top of the stack. In our notation we assume
that the insertion of a string into a stack is done symbol by symbol,
starting at the right end of the string.
…cont’d
Example 2
Consider an npda with
Q = {q0, q1, q2, q3} ,
Σ = {a, b} ,
Γ = {0, 1} ,
z = 0,
F = {q3} ,
with initial state q0 and
δ (q0, a, 0) = {(q1, 10),(q3, λ)} ,
δ (q0, λ, 0) = {(q3, λ)} ,
…cont’d
δ (q1, a, 1) = {(q1, 11)} ,
δ (q1, b, 1) = {(q2, λ)} ,
δ (q2, b, 1) = {(q2, λ)} ,
δ (q2, λ, 0) = {(q3, λ)} .
▪ The crucial transitions are
δ (q1, a, 1) = {(q1, 11)},
which adds a 1 to the stack when an a is read, and
δ (q2, b, 1) = {(q2, λ)},
which removes a 1 when a b is encountered.
…cont’d
▪ These two steps count the number of a’s and match that count against
the number of b’s.
▪ The control unit is in state q1 until the first b is encountered at which
time it goes into state q2. This assures that no b precedes the last a.
▪ After analyzing the remaining transitions, we see that the npda will
end in the final state q3 if and only if the input string is in the language
L = {anbn: n ≥ 0} ∪ {a}.
▪ The above npda can be represented by transition graphs.
▪ In this representation we label the edges of the graph with three things:
the current input symbol, the symbol at the top of the stack, and the
string that replaces the top of the stack.
…cont’d
▪ The transition graph for the above npda is
…cont’d
Instantaneous Description (ID)
▪ ID is an informal notation of how a PDA computes an input string and
make a decision that string is accepted or rejected.
▪ An instantaneous description is a triple (q, w, u) where:
▪ q describes the current state.
▪ w is the unread part of the input string, and
▪ u is the stack contents (with the leftmost symbol indicating the top
of the stack)
▪ A move from one instantaneous description to another will be denoted
by the symbol ˫ ;
…cont’d
▪ Thus
(q1, aw, bx) ˫ (q2, w, yx)
is possible if and only if
(q2, y) ∈ δ (q1, a, b)
▪ Moves involving an arbitrary number of steps will be denoted by ˫∗.
▪ The expression (q1, w1, x1) ˫∗ (q2, w2, x2) indicates a possible
configuration change over a number of steps
▪ On occasions where several automata are under consideration we will
use ⊢M to emphasize that the move is made by the particular
automaton M
…cont’d
The Language Accepted by a Pushdown Automaton
▪ Let M = (Q, Σ, Γ, δ, q0, z, F) be a nondeterministic pushdown
automaton. The language accepted by M is the set
L(M) = {w ∈ Σ*: (q0, w, z) ⊢* M (p, λ, u), p ∈ F, u ∈ Γ*}.
▪ In words, the language accepted by M is the set of all strings that can
put M into a final state at the end of the string.
▪ Example, Construct an npda for the language L = {anbn: n>=1}
…cont’d
▪ Approach used in the construction of PDA – As we want to design a
NPDA, thus every time ‘a’ comes before ‘b’. When ‘a’ comes then
push it in stack and if again ‘a’ comes then also push it. After that,
when ‘b’ comes then pop one ‘a’ from the stack each time. So, at the
end if the stack becomes empty then we can say that the string is
accepted by the PDA.
δ (q0, a, z) = {(q0, az)}
δ (q0, a, a) = {(q0, aa)}
δ (q0, b, a) = {(q1, λ)}
δ (q1, b, a) = {(q1, λ)}
δ (q1, λ, z) = {(qf, z)}
…cont’d
4.2. Pushdown Automata and Context Free
Languages
▪ Theorem - For any context-free language L, there exists an npda M
such that L = L(M).
▪ To show that for every context-free language there is an npda that
accepts it, construct an npda that can carry out a leftmost derivation of
any string in the language.
▪ This represent the derivation by keeping the variables in the right part
of the sentential form on its stack, while the left part, consisting
entirely of terminals, is identical with the input read.
▪ We begin by putting the start symbol on the stack.
…cont’d
▪ Example - Construct a pda that accepts the language generated by a
grammar with productions
S → aA,
A → aABC |bB| a,
B → b,
C → c.
▪ The corresponding automaton will have three states {q0, q1, q2}, with
initial state q0 and final state q2. First, the start symbol S is put on the
stack by
δ (q0, λ, z) = {(q1,Sz)}
…cont’d
▪ The sequence of moves made by M in processing aaabc is
δ (q1, a, S) = {(q1, A)} ,
δ (q1, a, A) = {(q1, ABC),(q1, λ)} ,
δ (q1, b, A) = {(q1, B)} ,
δ (q1, b, B) = {(q1, λ)} ,
δ (q1, c, C) = {(q1, λ)} ,
δ (q1, λ, z) = {(qf , z)}
▪ This corresponds to the derivation
S ⇒ aA ⇒ aaABC ⇒ aaaBC ⇒ aaabC ⇒ aaabc.
…cont’d
▪ The sequence of moves made by M in processing aaabc is
(q0, aaabc, z) (q1, aaabc, Sz)
(q1, aabc, Az)
(q1, abc, ABCz)
(q1, bc, BCz)
(q1, c, Cz)
(q1, λ, z)
(qf , λ, z).
▪ This corresponds to the derivation
S ⇒ aA ⇒ aaABC ⇒ aaaBC ⇒ aaabC ⇒ aaabc.
…cont’d
▪ Theorem - If L = L(M) for some npda M, then L is a context-free
language.
▪ For every context-free language there is an npda that accepts it, and
conversely, that the language accepted by any npda is context free.
4.3 Deterministic Pushdown Automata
▪ A deterministic pushdown automaton (dpda) is a pushdown automaton
that never has a choice in its move.
▪ Definition: A pushdown automaton M = (Q, Σ, Γ, δ, q0, z, F) is said to
be deterministic ( a deterministic PDA or DPA), if and only the
following conditions are met:
▪ For every q ∈ Q, a ∈ Σ ∪ {λ} and b ∈ Γ,
1) δ (q, a, b) contains at most one element,
2) if δ (q, λ, b) is not empty, then δ (q, c, b) must be empty for
every c ∈ Σ.
The first of these conditions simply requires that for any given input
symbol and any stack top, at most one move can be made. The
second condition is that when a λ-move is possible for some
configuration, no input-consuming alternative is available.
4.4. Deterministic Context Free Languages
▪ Deterministic finite automata and nondeterministic finite automata are
equivalent in language recognition power.
▪ In contrast, nondeterministic pushdown automata are more powerful
than their deterministic counterparts.
▪ Certain context-free languages cannot be recognized by deterministic
PDAs—these languages require nondeterministic PDAs.
▪ The languages that are recognizable by deterministic pushdown
automata are called deterministic context-free languages (DCFLs).
▪ The deterministic context-free languages is relevant to practical
applications, such as the design of parsers in compilers for
programming languages, because the parsing problem is generally
easier for DCFLs than for CFLs.
…cont’d
▪ Definition – A language L is said to be a deterministic context-free
language if and only if there exists a dpda M such that L = L(M).
▪ Example - The language L = {anbn : n ≥ 0} is a deterministic context-
free language.
The pda M = ({q0, q1, q2} , {a, b} , {0, 1} , δ, q0, 0, {q0}) with
δ (q0, a, 0) = {(q1, 10)} ,
δ (q1, a, 1) = {(q1, 11)} ,
δ (q1, b, 1) = {(q2, λ)} ,
δ (q2, b, 1) = {(q2, λ)} ,
δ (q2, λ, 0) = {(q0, λ)}
accepts the given language and satisfies the aforementioned conditions
and hence deterministic

You might also like