6 Pushdown Automata CIS 5513 Automata and Formal

  • Slides: 24
Download presentation
6. Pushdown Automata CIS 5513 - Automata and Formal Languages – Pei Wang

6. Pushdown Automata CIS 5513 - Automata and Formal Languages – Pei Wang

Pushdown automata: idea Context-Free Languages can be accepted by pushdown automata A pushdown automaton

Pushdown automata: idea Context-Free Languages can be accepted by pushdown automata A pushdown automaton is an ɛ-NFA with a stack with unlimited capacity Its transition function also takes the top of the stack into account, and may change the top item in the stack It accepts at a final state or an 2

Pushdown automata: definition A pushdown automaton (PDA) P = (Q, Σ, Γ, δ, q

Pushdown automata: definition A pushdown automaton (PDA) P = (Q, Σ, Γ, δ, q 0, Z 0, F) where �Γ (Gamma): A finite stack alphabet �δ: Q×(Σ {ε})×Γ → 2 Q×Γ*, a transition function that specifies the set of possible next state and the new content at the top of the stack �Z 0 Γ, the start symbol in stack The other components are like in an 3

Example: transition table A PDA for the binary language {wwr}: P = ({q 0,

Example: transition table A PDA for the binary language {wwr}: P = ({q 0, q 1, q 2}, {0, 1}, {Z 0, 0, 1}, δ, q 0, Z 0, {q 2}) 1. δ(q 0, 0, Z 0) = {(q 0, 0 Z 0)}; δ(q 0, 1, Z 0) = {(q 0, 1 Z 0)} 2. δ(q 0, 0, 0) = {(q 0, 00)}; δ(q 0, 0, 1) = {(q 0, 01)}; δ(q 0, 1, 0) = {(q 0, 10)}; δ(q 0, 1, 1) = {(q 0, 11)} 3. 4 δ(q 0, ɛ, Z 0) = {(q 1, Z 0)}; δ(q 0, ɛ, 0) =

Example: transition graph 5

Example: transition graph 5

Instantaneous descriptions of PDA The configuration, or instantaneous description (ID), of a PDA: (q,

Instantaneous descriptions of PDA The configuration, or instantaneous description (ID), of a PDA: (q, w, r), where q is the state, w is the remaining input, and r is the stack contents Here “state” only specifies the “control unit”, not the stored “data” in stack If the value of δ(q, a, X) contains (p, α), then the ID transition is (q, aw, 6

ID transitions: an example 7

ID transitions: an example 7

Properties of ID transitions ID transition with multiple moves is shown as ˫* Symbols

Properties of ID transitions ID transition with multiple moves is shown as ˫* Symbols may be added or removed to input or stack in ID transitions 8

Exercise 6. 1. 1 Solution 9

Exercise 6. 1. 1 Solution 9

Two types of acceptance A PDA can define a language in two ways 10

Two types of acceptance A PDA can define a language in two ways 10

From Empty Stack to Final State PF can be obtained by adding an ɛ-transition

From Empty Stack to Final State PF can be obtained by adding an ɛ-transition to a final state whenever the stack in PN is empty, with the help of a new stack-symbol below Z 0 11

From Final State to Empty Stack PN can be obtained by popping out all

From Final State to Empty Stack PN can be obtained by popping out all stack symbols in final states in PF, with the help of a new stack-symbol below Z 0 12

Exercises for Section 6. 2. 1(a): Design a PDA to accept {0 n 1

Exercises for Section 6. 2. 1(a): Design a PDA to accept {0 n 1 n | n ≥ 1} 6. 2. 2(a): Design a PDA to accept {aibjck | i = j or j = k} Solutions: http: //infolab. stanford. edu/~ullman/ialcsols/so l 6. html#sol 62 13

CFG to PDA From a given CFG G = (V, T, Q, S), a

CFG to PDA From a given CFG G = (V, T, Q, S), a PN = ({q}, T, V T, , q, S) can accept L(G) by simulating the leftmost derivation to expand the symbols in stack to match the input When PN is in ID (q, y, Aα) where A is a variable and there is a rule A→β in Q, the next ID can be (q, y, βα). Remove the common prefix of βα and y. Repeat until the stack is 14

CFG to PDA: example 15

CFG to PDA: example 15

PDA to CFG:simple case If a PDA is P = ({q}, Σ, Γ, δ,

PDA to CFG:simple case If a PDA is P = ({q}, Σ, Γ, δ, q, Z), the equivalent CFG G = (Γ {S}, Σ, R, S), where R contains S → Z, and for each (q, Y 1 Y 2…Yk) in δ(q, a, X), R contains X → a. Y 1 Y 2…Yk, where k ≥ 0, and Yi can be a variable, a terminal, or ε 16

PDA to CFG: general case Use a grammar variable [p. Xq] to represent the

PDA to CFG: general case Use a grammar variable [p. Xq] to represent the state change from p to q for the popping out of a stack symbol X 17

PDA to CFG (cont. ) 18

PDA to CFG (cont. ) 18

PDA to CFG: example 6. 10: δ(q, i, Z) = {(q, ZZ)}; δ(q, e,

PDA to CFG: example 6. 10: δ(q, i, Z) = {(q, ZZ)}; δ(q, e, Z) = {(q, ε)} 19

PDA to CFG: example (2) 20

PDA to CFG: example (2) 20

PDA to CFG: example (3) 21

PDA to CFG: example (3) 21

Exercises for Section 6. 3 Solutions: 22 http: //infolab. stanford. edu/~ullman/ialcsols/sol 6. ht

Exercises for Section 6. 3 Solutions: 22 http: //infolab. stanford. edu/~ullman/ialcsols/sol 6. ht

Deterministic pushdown automata A PDA is deterministic (DPDA), if �Each δ(q, a, X) has

Deterministic pushdown automata A PDA is deterministic (DPDA), if �Each δ(q, a, X) has at most one element �If δ(q, a, X) is nonempty, then δ(q, ɛ, X) must be empty �Lwcwr = {wcw. R} with a marker c 23

PDA, DPDA, and DFA The set of language accepted by DPDA is a subset

PDA, DPDA, and DFA The set of language accepted by DPDA is a subset of CFL, and a super set of regular language, so the capability of DPDA is between those of PDA and DFA DPDA accept all regular languages, but only the context-free languages that have unambiguous CFG 24