09_pushdown
09_pushdown
1
Topics
2
Nondeterministic Pushdown
Automata
3
Automata for Context-free Languages?
• We know that for regular languages, we have DFAs and NFAs which are equivalent to
them.
5
Pushdown Automata
Definition 7.1
7
Pushdown Automata
Example 7.2
• Unspecified transitions are to the null sets, and represent dead configuration.
• δ(q0, a,0) = {(q1,10), (q3, λ)}, δ(q0, λ,0) = {(q3, λ)},
• δ(q1, a,1) = {(q1,11)}, δ(q1, b,1) = {(q2, λ)},
• δ(q2, b,1) = {(q2, λ)}, δ(q2, λ,0) = {(q3, λ)}.
• This NPDA accepts the language
n n
• L = {a b : n ≥ 0} ∪ {a}.
9
Pushdown Automata
Example 7.3
• The NPDA in Example 7.2 is represented by the transition graph in Figure 7.2.
• δ(q0, a,0) = {(q1,10), (q3, λ)},
• δ(q0, λ,0) = {(q3, λ)},
• δ(q1, a,1) = {(q1,11)},
• δ(q1, b,1) = {(q2, λ)},
• δ(q2, b,1) = {(q2, λ)},
• δ(q2, λ,0) = {(q3, λ)}. Figure 7.2
10
Instantaneous Description
• The triplet
• (q, w, u), where
• q is the state of the CU,
• w is the unread part of the input string, and
• u is the stack content
• is called an instantaneous description of a pushdown automaton.
11
Instantaneous Description
• A move from one instantaneous description to another will be denoted by the symbol ⊢
(turnstile).
13
The Language Accepted by Pushdown Automata
Example 7.4
aabbab
• Construct an NPDA for the language
• L = {w ∈ {a, b}* : na(w) = nb(w)}. z
• For this language, we only need to count the number of a's and b's.
• In order to do that, we can insert a counter symbol into the stack.
• Whenever we encounter a, we put 0, and for b, we pop up 0 from the stack.
14
The Language Accepted by Pushdown Automata
Example 7.4
• How about there exists more b's than a's from the beginning?
• We don't have 0s to pop from the stack.
• We can leverage another symbol 1 to express such cases.
• These 1s will be matched against a's later.
bbaaab
15
The Language Accepted by Pushdown Automata
Example 7.4
Figure 7.3
16
The Language Accepted by Pushdown Automata
Example 7.5
20
Pushdown Automata for Context-Free Languages
21
Pushdown Automata for Context-Free Languages
Example 7.6
• S → aSbb | a.
• Converting the grammar into GNF, we obtain
• S → aSA | a, A → bB, B → b.
• The corresponding automaton will have three states {q0, q1, q2} correspond
to S, A, and B, having q0 as the initial state and q2 be a final state.
22
Pushdown Automata for Context-Free Languages
Example 7.6
• S → aSA | a, A → bB, B → b.
• Here is the first step, which put S to the stack.
• Every derivation should start from the start symbol.
• δ(q0, λ, z) = {(q0, Sz)}.
• S → aSA will be simulated by removing S from the stack and replacing it with SA, while
reading a to the input.
• A → bB and B → b give
• δ(q1, b, A) = {(q1, B)},
• δ(q1, b, B) = {(q1, λ)} respectively.
• When we find the stack start symbol on top of the stack indicates the
completion of derivation.
25
Pushdown Automata for Context-Free Languages
Theorem 7.1
• To show that M accepts any w ∈ L(G), consider the partial leftmost derivation
• S ⇒* a1a2⋯an A1A2⋯Am ⇒ a1a2⋯anbB1⋯Bk A2⋯Am.
• After reading a1a2⋯an, the stack must contain A1A2⋯Am.
• For the next step, G must have a production A1 → bB1⋯Bk.
• Then by construction, M has a transition rule (q1, B1⋯Bk) ∈ δ(q1, b, A1),
• so that the stack contains B1⋯Bk A2⋯Am, after reading a1a2⋯anb.
27
Pushdown Automata for Context-Free Languages
Theorem 7.1
28
Pushdown Automata for Context-Free Languages
Theorem 7.1
• Then the first step in (q1, a1a2a3⋯an, Sz) ⊢* (q1, λ, z) must be a rule of the form (7.2) to get
• (q1, a1a2a3⋯an, Sz) ⊢ (q1, a2a3⋯an, u1z).
• But then the grammar has a rule of the form S → a1u1, so that S ⇒ a1u1.
• Repeating this, writing u1 = Au2, we have
• (q1, a2a3⋯an, Au2z) ⊢ (q1, a3⋯an, u3u2z), implying that A → a2u3 is in the grammar,
• and that S ⇒* a1a2u3u2. With this, (7.4) implies S ⇒* a1a2⋯an.
• Hence L(M) ⊆ L(G), completing the proof.
• If λ ∈ L, we add δ(q0, λ, z) = {(qf , z)}.
30
Pushdown Automata for Context-Free Languages
Example 7.7
1. It has a single final state qf that is entered iff. the stack is empty;
2. With a ∈ Σ ∪ {λ}, all transitions must have the form δ(qi, a, A) = {c1, c2, ⋯, cn},
where
• Suppose a grammar whose variables are of the form (qi Aqj) and whose productions are s.t.
• (qi Aqj) ⇒* v, iff. the NPDA erases A from the stack while reading v and going from state qi to qj.
• Then we can have productions
• (qi Aqj) → a for (7.5) and
• (qi Aqk) → a(qj Bql)(qlCqk) for (7.6),
• where qk and ql take on all possible values in Q.
• Finally, as a start variable, we take (q0zqf ), where qf is the single final state of the NPDA.
• Then (q0zqf ) ⇒* w iff. the NPDA removes z while reading w and going from q0 to qf.
35
Context-Free Grammars for Pushdown Automata
Example 7.8
37
Context-Free Grammars for Pushdown Automata
Example 7.8
• From the first two transitions δ(q0, a, z) = {(q0, Az)}, δ(q3, λ, z) = {(q0, Az)},
• we get a set of productions using (qi Aqk) → a(qjBql)(qlCqk).
• With δ(q0, a, z) = {(q0, Az)}, k = 0,1,2,3, l = 0,1,2,3,
• (q0zq0) → a(q0 Aq0)(q0zq0) | a(q0 Aq1)(q1zq0) | a(q0 Aq2)(q2zq0) | a(q0 Aq3)(q3zq0),
• (q0zq1) → a(q0 Aq0)(q0zq1) | a(q0 Aq1)(q1zq1) | a(q0 Aq2)(q2zq1) | a(q0 Aq3)(q3zq1), ...
• With δ(q3, λ, z) = {(q0, Az)},
• (q3zq0) → (q0 Aq0)(q0zq0) | (q0 Aq1)(q1zq0) ....
38
Context-Free Grammars for Pushdown Automata
Example 7.8
• After write down all the productions, we can consider to simplify this.
• A variable that does not occur on the left side of productions must be useless, so
we can eliminate (q0 Aq0) and (q0 Aq2).
• Also, with modified transitions from the following, there is no path from q1 to q0,
from q1 to q1, from q1 to q3, and from q2 to q2.
• Consider the string w = aab, which is accepted by the PDA in Example 7.8.
• δ(q0, a, z) = {(q0, Az)}, δ(q3, λ, z) = {(q0, Az)},
• δ(q0, a, A) = {(q3, λ)}, δ(q0, b, A) = {(q1, λ)},
• δ(q1, λ, z) = {(q2, λ)}.
• Check successive configurations and corresponding derivation with G by yourself.
• (q0, aab, z) ⊢ (q0, ab, Az) ⊢ (q3, b, z) ⊢ ...
• (q0zq2) ⇒ a(q0 Aq3)(q3zq2) ⇒ aa(q3zq2) ⇒ ...
41
Context-Free Grammars for Pushdown Automata
Theorem 7.2
42
Deterministic Pushdown Automata and
Deterministic Context-free Languages
43
Deterministic Pushdown Accepter
Definition 7.3
44
Deterministic Context-Free Languages
Definition 7.4
• Condition 1 is for any given input symbol and stack top, at most one move
can be made.
45
Deterministic Pushdown Automata and Context-Free Languages
Example 7.10
n n
• The language L = {a b : 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 it satisfies the conditions.
46
Deterministic vs. Nondeterministic PDAs
Example 7.11
48
Deterministic vs. Nondeterministic PDAs
Example 7.11
49
Deterministic vs. Nondeterministic PDAs
Example 7.11
50
Grammars for Deterministic
Context-Free Languages
51
Grammars for Deterministic Context-Free Languages
52
Grammars for Deterministic Context-Free Languages
• For general context-free grammars, this is not the case, but if we restrict the form
of a grammar, we can achieve the goal.
55
LL(k) Grammar
Definition 7.5
57
LL Grammar
Example 7.12
• The grammar
• S → aSb | ab
• is not an s-grammar, but it is an LL(2) grammar.
• In order to determine which production to be applied,
• we look at two consecutive symbols of the input string.
• For ab, we must apply the second, otherwise apply the first.
58
LL Grammar
Example 7.13
• The grammar
• S → SS | aSb | ab
• generates the language of properly nested parenthesis structures (Example 5.4).
• The grammar is not an LL(k) grammar for any k.
• If we look-ahead k symbols, still there exists a possibility that these symbols are
a prefix of more than one strings.
59
LL Grammar
Example 7.13
• However, this does not mean that the language itself is not deterministic or no LL
grammar for it exists.
60
Summary
61