Formal Languages and Automata http mail im tku



































- Slides: 35
形式語言 Formal Languages and Automata 淡江大學 資訊管理系 侯 永 昌 http: //mail. im. tku. edu. tw/~ychou ftp: //mail. im. tku. edu. tw/Prof_Hou
教科書:An Introduction to Formal Languages and Automata, 5 th Edition, by Peter Linz, Jones & Bartlett Learning 出版,開發圖書總經銷 淡江大學資訊管理系侯永昌 1
第七章 下推自動機 (Pushdown Automata)
Pushdown Automata § Finite automata 可以用來辨識 regular grammar § 因為 finite automata 中只有有限的狀態,因此 不能用來辨識 {anbn : n 0} 或 {ww. R}這樣的 context-free grammar § 我們需要一個以堆疊 (stack) 來儲存系統狀態 的機器 (pushdown automata, pda),就可以處理 上述 context-free grammar的問題 淡江大學資訊管理系侯永昌 3
Nondeterministic Pushdown Automata § A nondeterministic pushdown accepter (npda) is defined by septuple M = (Q, , q 0, z, F), where l l l l Q is a finite set of internal states of the control init 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 q 0 Q is the initial state of the control unit z is the stack start symbol F Q is the set of final states 淡江大學資訊管理系侯永昌 4
Nondeterministic Pushdown Automata { } : Q ( { }) Q * Q 淡江大學資訊管理系侯永昌 5
Nondeterministic Pushdown Automata Input, Pop stack, Push stack 淡江大學資訊管理系侯永昌 10
Instantaneous Description § The triplet (q, w, u), where q is the state of the control unit, 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), is called an instantaneous description of a pushdown automata § A move from one instantaneous description to another will be denoted by the symbol├; thus (q 1, aw, bx)├ (q 2, w, yx) is possible if and only if (q 2, y) (q 1, a, b) 淡江大學資訊管理系侯永昌 11
Language Accepted by a pda § (q 1, aw, bx)├* (q 2, w, yx) indicates a possible configuration change over a number of steps § Let M = (Q, , q 0, z, F) be a nondeterministic pushdown automata. The language accepted by M is the set L(M) = {w * * : (q 0, w, z)├M (p, , u), p F, u *} § The language accepted by M is the set of all strings that can put M into a final state at end of the string. The final stack content u is irrelevant to this definition of acceptance 淡江大學資訊管理系侯永昌 12
Language Accepted by a pda § (q 0, baab, z)├ (q 0, aab, 1 z)├ (q 0, ab, z) ├ (q 0, b, 0 z)├ (q 0, , z)├ (qf, , z) 3 1 2 4 5 6 7 淡江大學資訊管理系侯永昌 15
Pushdown Automata for Context-Free Languages § 定理:For any context-free language L, there exists an npda M such that L = L(M) § 為了簡化推導的複雜度,我們將 context-free 語言都轉換成 Greibach normal form § pda 的建構方式為:針對每一個生產規則 A ax,當堆疊的頂端元素為 A,輸入字元為 a 時,堆疊將 A 移除 (pop),然後放入 (push) x § 例:Construct a pda that accept the language generated by a grammar with productions S a. Sbb | a 淡江大學資訊管理系侯永昌 18
Pushdown Automata for Context-Free Languages § (q 0, aaabc, z)├ (q 1, aaabc, Sz)├ (q 1, aabc, Az) ├ (q 1, abc, ABCz)├ (q 1, bc, BCz)├ (q 0, c, Cz) ├ (q 1, , z)├ (qf, , z) § S a. A aa. ABC aaab. C aaabc 淡江大學資訊管理系侯永昌 21
Context-Free Grammars for Pushdown Automata § 一個 npda 一定滿足下列的狀況: § It has a single final state qf that is entered if and only if the stack is empty; § With a { }, all transitions must have the term (qi, a, A) = {c 1, c 2, …, cn}, where 1. ci = (qj, ) (qi. Aqj) a, or 2. ci = (qj, BC) (qi. Aqk) a(qj. Bql)(ql. Cqk) 也就是說,每走一步,堆疊中都會少一個元 素或多一個元素 淡江大學資訊管理系侯永昌 23
Context-Free Grammars for Pushdown Automata § 前 2 式可以得到如下的grammar (q 0 zq 0) a(q 0 Aq 0)(q 0 zq 0) | a(q 0 Aq 1)(q 1 zq 0) | a(q 0 Aq 2)(q 2 zq 0) | a(q 0 Aq 3)(q 3 zq 0), (q 0 zq 1) a(q 0 Aq 0)(q 0 zq 1) | a(q 0 Aq 1)(q 1 zq 1) | a(q 0 Aq 2)(q 2 zq 1) | a(q 0 Aq 3)(q 3 zq 1), (q 0 zq 2) a(q 0 Aq 0)(q 0 zq 2) | a(q 0 Aq 1)(q 1 zq 2) | a(q 0 Aq 2)(q 2 zq 2) | a(q 0 Aq 3)(q 3 zq 2), (q 0 zq 3) a(q 0 Aq 0)(q 0 zq 3) | a(q 0 Aq 1)(q 1 zq 3) | a(q 0 Aq 2)(q 2 zq 3) | a(q 0 Aq 3)(q 3 zq 3), 淡江大學資訊管理系侯永昌 26
Context-Free Grammars for Pushdown Automata (q 3 zq 0) (q 0 Aq 0)(q 0 zq 0) | (q 0 Aq 1)(q 1 zq 0) | (q 0 Aq 2)(q 2 zq 0) | (q 0 Aq 3)(q 3 zq 0), (q 3 zq 1) (q 0 Aq 0)(q 0 zq 1) | (q 0 Aq 1)(q 1 zq 1) | (q 0 Aq 2)(q 2 zq 1) | (q 0 Aq 3)(q 3 zq 1), (q 3 zq 2) (q 0 Aq 0)(q 0 zq 2) | (q 0 Aq 1)(q 1 zq 2) | (q 0 Aq 2)(q 2 zq 2) | (q 0 Aq 3)(q 3 zq 2), (q 3 zq 3) (q 0 Aq 0)(q 0 zq 3) | (q 0 Aq 1)(q 1 zq 3) | (q 0 Aq 2)(q 2 zq 3) | (q 0 Aq 3)(q 3 zq 3), 淡江大學資訊管理系侯永昌 27
Context-Free Grammars for Pushdown Automata § 因此最後剩下:其中 q 0 zq 2 為起始變數 1(q Aq ) a, 2(q Aq ) b, 3(q zq ) , 0 3 0 1 1 2 4(q zq ) a(q Aq )(q zq ), 0 0 0 3 3 0 5(q zq ) a(q Aq )(q zq ), 0 1 0 3 3 1 6(q zq ) a(q Aq )(q zq ) | a(q Aq )(q zq ), 0 2 0 1 1 2 0 3 3 2 7(q zq ) a(q Aq )(q zq ), 0 3 3 3 8(q zq ) (q Aq )(q zq ), 3 0 0 3 3 0 9(q zq ) (q Aq )(q zq ), 3 1 0 3 3 1 10(q zq ) (q Aq )(q zq ) | (q Aq )(q zq ), 3 2 0 1 1 2 0 3 3 2 11(q zq ) (q Aq )(q zq ), 3 3 0 3 3 3 淡江大學資訊管理系侯永昌 29
Context-Free Grammars for Pushdown Automata § 例:驗證字串 w = aab 可以被上一頁的 npda 所接受 § 解:(q 0, aab, z)├ (q 0, ab, Az)├ (q 3, b, z) ├ (q 0, b, Az)├ (q 1, , z)├ (q 2, , ) 6 1 10 § (q 0 zq 2) a(q 0 Aq 3)(q 3 zq 2) aa(q 3 zq 2) 2 aab(q zq ) 3 aab aa(q 0 Aq 1) (q 1 zq 2) 1 2 淡江大學資訊管理系侯永昌 30
Context-Free Grammars for Pushdown Automata § 定理:If L = L(M) for some npda M, then L is a context-free language 淡江大學資訊管理系侯永昌 31
Deterministic Pushdown Automata and Deterministic Context-Free Languages § A deterministic pushdown accepter (dpda) is a pushdown automata that never has a choice in its move § A pushdown automaton M = (Q, , q 0, z, F) is said to be deterministic if it is an automaton as defined in page 4, subject to the restrictions that, for every q Q, a { }, 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 淡江大學資訊管理系侯永昌 32
Deterministic Pushdown Automata and Deterministic Context-Free Languages § A language L is said to be deterministic contextfree language if and only if there exists a dpda M such that L = L(M) § 例:L = {anbn : n 0} is deterministic contextfree language. The pda M = ({q 0, q 1, q 2}, {a, b}, {0, 1}, , q 0, 0, {q 0}) with (q 0, a, 0) = {(q 1, 10)}, (q 1, a, 1) = {(q 1, 11)}, (q 1, b, 1) = {(q 2, )}, (q 2, , 0) = {(q 0, )} accepts the given language 淡江大學資訊管理系侯永昌 33
Deterministic Pushdown Automata and Deterministic Context-Free Languages § Page 16 的例子就不是 deterministic,因為 (q 0, a, a) = {(q 0, aa)}, (q 0, , a) = {(q 1, a)}, 違反 page 32 之第 2 點 § 每一個 nondeterministic finite automata 都有一 個全等 (equivalent) 的 deterministic finite automata 與之對應 (參見第二章 page 18) § 和 finite automata 不同,nondeterministic 和 deterministic pushdown automata 是不全等的 § 表示有一些 context-free language 不是 deterministic 淡江大學資訊管理系侯永昌 34



































