Formal Languages and Automata http mail im tku

  • Slides: 33
Download presentation
形式語言 Formal Languages and Automata 淡江大學 資訊管理系 侯 永 昌 http: //mail. im. tku.

形式語言 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

教科書:An Introduction to Formal Languages and Automata, 5 th Edition, by Peter Linz, Jones & Bartlett Learning 出版,開發圖書總經銷 淡江大學資訊管理系侯永昌 1

第十一章 形式語言之層級關係 (A Hierarchy of Formal Languages and Automata)

第十一章 形式語言之層級關係 (A Hierarchy of Formal Languages and Automata)

Recursive Languages § A language L on is said to be recursive (遞迴) if

Recursive Languages § A language L on is said to be recursive (遞迴) if there exists a Turing machine M that accepts L and halts on every w in +. In other words, a language is recursive if and only if there exists a membership algorithm for it § 也就是說,如果 L 是一個 recursive 語言,那 一定存在一個圖林機,可以判斷讀到的字串 w 是否屬於recursive 語言 l l 是:它會正確的停在終止狀態 不是:它也會停在非終止狀態,解決前一頁第 2 點的問題 淡江大學資訊管理系侯永昌 4

Countable and Uncountable § A set is said to be countable if its elements

Countable and Uncountable § A set is said to be countable if its elements can be put into one-to-one correspondence with positive integers, i. e. , the elements of the set can be written in some order § 有限集合中的元素當然是可數的,無限集合 中的元素很可能是不可數 (uncountable)的, 旦也有可能是可數的 § 例:偶數,雖然它有無限多個 (infinite),但 是卻是可數的 (countable) § 解: 0, 2, 4, …, 2 i, … 是第 i + 1 個元素 淡江大學資訊管理系侯永昌 7

Languages Not Recursively Enumerable § 定理:For any nonempty , there exist languages that are

Languages Not Recursively Enumerable § 定理:For any nonempty , there exist languages that are not recursively enumerable § 定理:There exists a recursively enumerable language whose complement is not recursively enumerable § 某些遞迴可列舉語言的補集合就不是遞迴可 列舉語言 淡江大學資訊管理系侯永昌 12

Languages That is Recursively Enumerable but Not Recursive § 定理:If a language L and

Languages That is Recursively Enumerable but Not Recursive § 定理:If a language L and its complement L are both recursively enumerable, then both languages are recursive. If L is recursive, then L is also recursive, and consequently both are recursively enumerable § 定理:There exists a recursively enumerable language that is not recursive; that is, the family of recursive languages is a proper subset of the family of recursively enumerable languages 淡江大學資訊管理系侯永昌 13

Unrestricted Grammars § A grammar G = (V, T, S, P) is called unrestricted

Unrestricted Grammars § A grammar G = (V, T, S, P) is called unrestricted if all the productions are of the form u v, where u is in (V T)+ and v is in (V T)* § 無限制文法對於生產規則的左邊和右邊 (包括 變數和終端符號的數量、順序) 都沒有任何限 制,除了 不能出現在左邊以外 § 定理:Any language generated by an unrestricted grammar is recursively enumerable 淡江大學資訊管理系侯永昌 14

Unrestricted Grammars § 例:Let M = (Q, , q 0, �, F) be a

Unrestricted Grammars § 例:Let M = (Q, , q 0, �, F) be a Turing machine with Q = {q 0, q 1}, = {a, b, �}, = {a, b}, F = {q 1} and (q 0, a) = (q 0, a, R), (q 0, �) = (q 1, �, L). This machine accepts L(aa*) § 解︰ q 0 aa ├ aq 0 a ├ aaq 0 ├ aq 1 a § 定理︰For every recursively enumerable language L, there exists an unrestricted grammar G, such that L = L(G) 淡江大學資訊管理系侯永昌 15

Context-Sensitive Grammars and Languages § A grammar G = (V, T, S, P) is

Context-Sensitive Grammars and Languages § A grammar G = (V, T, S, P) is said to be contextsensitive (對上下文敏感、與上下文有關) if all the productions are of the form x y, where x and y (V T)+ and |x| |y| § The length of successive sentential forms can never decrease § x is not allowed in the context-sensitive grammar 淡江大學資訊管理系侯永昌 16

Nondeterministic Turing Machine § A nondeterministic Turing machine is an automaton as given by

Nondeterministic Turing Machine § A nondeterministic Turing machine is an automaton as given by page 9 -4 , except that is now a function : Q 2(Q (L, R). As always when nondeterminism is involved, the range of is a set of possible transitions, any of which can be chosen by the machine § 例︰If a Turing machine has transitions specified by (q 0, a) = {(q 1, b, R), (q 2, c, L}, it is nondeterministic. The moves q 0 aaa ├ bq 1 aa and q 0 aaa ├ q 2�caa are both possible 淡江大學資訊管理系侯永昌 18

Nondeterministic Turing Machine § 定理︰The class of deterministic Turing machines and the class of

Nondeterministic Turing Machine § 定理︰The class of deterministic Turing machines and the class of nondeterministic Turing machines are equivalent § A nondeterministic Turing machine M is said to accept a language L, if for all w L, at least one of the possible configurations accept w. There may be branches that lead to nonaccepting configurations, while some may put the machine into an infinite loop. But these are irrelevant for acceptance § A nondeterministic Turing machine M is said to decide a language L, if for all w *, there is a path that leads either to acceptance or rejection 淡江大學資訊管理系侯永昌 19

Linear Bounded Automata § While it is not possible to extend the power of

Linear Bounded Automata § While it is not possible to extend the power of the standard Turing machine by complicating the tape structure, it is possible to limit it by restricting the way in which the tape can be used l Pushdown automaton: tape is restricted to being used like a stack l Finite automaton: only a finite part of the tape to be used as work space l Linear bounded automaton: to use only that part of the tape occupied by the input 淡江大學資訊管理系侯永昌 20

Linear Bounded Automata § A linear bounded automaton (lba) is a nondeterministic Turing machine

Linear Bounded Automata § A linear bounded automaton (lba) is a nondeterministic Turing machine M = (Q, , q 0, �, F), subject to the restriction that must contain two special symbol [ and ], such that (qi, [) can contain only elements of the form (qj, [, R), and (qi, ]) can contain only elements of the form (qj, ], L) l l 對於長的輸入字串來說,lba 就可以使用比較大 的 作區間,短的字串就使用較小的 作區間 為了界定 作區間,lba 使用了兩個特殊的符號 [ 和 ],加在輸入字串的前後 淡江大學資訊管理系侯永昌 21

Linear Bounded Automata § A string w is accepted by a linear bounded automaton

Linear Bounded Automata § A string w is accepted by a linear bounded automaton if there is a possible sequence of moves q 0[w]├*M [x 1 qf x 2] for some qf F, x 1, x 2 *. The language accepted by the lba is the set of all such accepted strings § 例︰The language L = {anbncn : n 1} is accepted by some linear bounded automaton § 解︰page 9 -18~20 的例子中,我們可以發現, 它並沒有用超過輸入字串長度的空間 淡江大學資訊管理系侯永昌 22

Linear Bounded Automata § 例︰Find a linear bounded automaton that accepts the language L

Linear Bounded Automata § 例︰Find a linear bounded automaton that accepts the language L = {an! : n 0} is accepted by some linear bounded automaton § 解︰我們將輸入字串分別除上 2, 3, 4, …,如 果除的時候有餘數發生,就代表不接受;如 果最終的結果只剩下一個 a,就表示接受 淡江大學資訊管理系侯永昌 23

Context-Sensitive Grammars and Languages § Between the restricted, context-free grammars and the general, unrestricted

Context-Sensitive Grammars and Languages § Between the restricted, context-free grammars and the general, unrestricted grammars, a great variety of “somewhat restricted” grammars can be defined § A grammar G = (V, T, S, P) is said to be contextsensitive if all the productions are of the form x y, where x and y (V T)+ and |x| |y| l l The length of successive sentential forms can never decrease x is not allowed in the context-sensitive grammar 淡江大學資訊管理系侯永昌 25

Context-Sensitive Languages and Linear Bounded Automata § 定理︰A language L is said to be

Context-Sensitive Languages and Linear Bounded Automata § 定理︰A language L is said to be contextsensitive if there exists a context-sensitive grammar G, such that L = L(G) or L = L(G) { } § 例︰The language L = {anbncn : n 1} is a context-sensitive language. One such grammar is S abc|a. Abc, Ab b. A, Ac Bbcc, b. B Bb, a. B aa|aa. A § 解︰S a. Abc ab. Ac ab. Bbcc a. Bbbcc aa. Abbcc aab. Abcc aabb. Acc aabb. Bbccc aab. Bbbccc aa. Bbbbccc淡江大學資訊管理系侯永昌 aaabbbccc 26

Context-Sensitive Languages and Linear Bounded Automata § 構思方向︰將 A 從左邊往右邊送,直到碰上 c 為止,產生一對 bc,然後將 B

Context-Sensitive Languages and Linear Bounded Automata § 構思方向︰將 A 從左邊往右邊送,直到碰上 c 為止,產生一對 bc,然後將 B 從右邊往左 邊送,直到碰上 a 為止,產生一對 ab,然後 進入下一個循環,將 A 從左邊往右邊送,… § 定理︰For every context-sensitive language L is not including , there exists some linear bounded automaton M such that L = L(M) § 定理︰If a language L is accepted by some linear bounded automaton M, then there exists a context-sensitive grammar that generates L 淡江大學資訊管理系侯永昌 27

Relation Between Recursive and Context-Sensitive Languages § 定理︰Every context-sensitive language L is recursive §

Relation Between Recursive and Context-Sensitive Languages § 定理︰Every context-sensitive language L is recursive § 定理︰There exists a recursive language that is not context-sensitive § Linear bounded automata is l l less powerful than Turing machines, since they accept only a proper subset of the recursive languages more powerful than pushdown automata, since context-free grammars are a subset of the contextsensitive languages 淡江大學資訊管理系侯永昌 28

Language Families: The Chomsky Hierarchy LRE: recursively enumerable language LCS: context-sensitive language LCF: context-free

Language Families: The Chomsky Hierarchy LRE: recursively enumerable language LCS: context-sensitive language LCF: context-free language LREG: regular language 淡江大學資訊管理系侯永昌 29

Language Families LREC: recursive language LDCF: deterministic context-free language 淡江大學資訊管理系侯永昌 30

Language Families LREC: recursive language LDCF: deterministic context-free language 淡江大學資訊管理系侯永昌 30

Language Families § 例︰L = {w : na(w) = nb(w)} is context-free, deterministic, but

Language Families § 例︰L = {w : na(w) = nb(w)} is context-free, deterministic, but not linear,page 8 -12 § 例︰L = {anbn} {anb 2 n} is context-free, linear, but not deterministic,page 7 -33~34 LLIN: linear language (page 3 -21, 5 -5) 淡江大學資訊管理系侯永昌 31

Language Families Language Grammar Automaton Recursively Turing machine Type 0 Unrestricted enumerable automaton Context-

Language Families Language Grammar Automaton Recursively Turing machine Type 0 Unrestricted enumerable automaton Context- Linear bounded Type 1 sensitive automaton Context. Pushdown Type 2 free automaton Nondeterministic Type 3 Regular /deterministic finite automaton 淡江大學資訊管理系侯永昌 32