Formal Languages and Automata http mail im tku

  • Slides: 23
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

第六章 與上下文無關語言的正規化 (Simplification of Context-Free Grammars and Normal Forms)

第六章 與上下文無關語言的正規化 (Simplification of Context-Free Grammars and Normal Forms)

Methods for Transforming Grammars § 定理: Let G = (V, T, S, P) be

Methods for Transforming Grammars § 定理: Let G = (V, T, S, P) be a context-free grammar. Suppose that P contains a production of the form A x 1 Bx 2. Assume that A and B are different variables and that B y 1 | y 2 | … | yn is the set of all productions in P that have B as the left side. Let G’ = (V, T, S, P’) be the grammar in which P’ is constructed by deleting A x 1 Bx 2 from P, and adding to it A x 1 y 1 x 2 | x 1 y 2 x 2 | … | x 1 ynx 2. Then L(G’) = L(G) 淡江大學資訊管理系侯永昌 5

Removing Useless Productions § Let G = (V, T, S, P) be a context-free

Removing Useless Productions § Let G = (V, T, S, P) be a context-free grammar. A variable A V is said to be useful if and only if * x. Ay there is at least one w L(G) such that S * w, with x, y in (V T)*. § 換句話說:一個變數要有用,代表至少有一個 字串可以經由他推導而得,否則這就是一個 無用的 (useless) 變數 § 包含了無用變數的生產規則都可以被省略不要 淡江大學資訊管理系侯永昌 7

Removing Useless Productions § 定理:Let G = (V, T, S, P) be a context-free

Removing Useless Productions § 定理:Let G = (V, T, S, P) be a context-free grammar. Then there exists an equivalent grammar G’ = (V’, T’, S, P’) that does not contain any useless variables or productions 淡江大學資訊管理系侯永昌 10

Removing -Productions § Any production of a context-free grammar of the form A is

Removing -Productions § Any production of a context-free grammar of the form A is called a -production. Any * is variable A for which the derivation A possible is called nullable § 例:有一個文法包含 S a. S 1 b, S 1 a. S 1 b | , 起始字元為 S。這個文法可以產生的字串為 {anbn : n 1} § 他相對應的 -free 的生產規則為 S a. S 1 b | ab, S 1 a. S 1 b | ab 就可以產生一個跟原來文法一 樣的語言 淡江大學資訊管理系侯永昌 11

Removing Unit-Productions § Any production of a context-free grammar of the form A B,

Removing Unit-Productions § Any production of a context-free grammar of the form A B, where A, B V, is called a unit-production 淡江大學資訊管理系侯永昌 14

Removing Unit-Productions § 定理:Let G = (V, T, S, P) be any context-free grammar

Removing Unit-Productions § 定理:Let G = (V, T, S, P) be any context-free grammar without -production. Then there exists a context-free grammar G’ = (V’, T’, S, P’) that does not have any unit-production and that is equivalent to G § 步驟 1:首先留下原先的 non-unit production * B, § 步驟 2:針對所有的 unit production A 將步驟 1 的結果帶入 § 步驟 3:合併步驟 1 和步驟 2 的結果 淡江大學資訊管理系侯永昌 15

Removing Unit-Productions § 例:移除下列生產規則中的 unit-production S Aa | B, B A | bb, A

Removing Unit-Productions § 例:移除下列生產規則中的 unit-production S Aa | B, B A | bb, A a | bc | B § 首先留下原先的 non-unit production S Aa, B bb, A a | bc § 針對 B A B a | bc 針對 A B A bb 針對 S B S a | bc | bb § 合併後, S Aa | bc | bb, B bb | a | bc, A a | bc | bb 淡江大學資訊管理系侯永昌 16

Methods for Transforming Grammars § 定理:Let L be a context-free grammar that does not

Methods for Transforming Grammars § 定理:Let L be a context-free grammar that does not contain . Then there exists a contextfree grammar that generate L and that does not have any useless productions, -productions, or unit-productions 淡江大學資訊管理系侯永昌 17

Chomsky Normal Form § A context-free grammar is in Chomsky normal form if all

Chomsky Normal Form § A context-free grammar is in Chomsky normal form if all productions are of the form A BC or A a, where A, B, C V, and a in T § 例:S AS | a, A SA | b 符合 Chomsky normal form,但是 S AS | AAS, A SA | aa 不符合 Chomsky normal form § 定理:Any context-free grammar G = (V, T, S, P) with L(G) has an equivalent G’ = (V’, T’, S, P’) in Chomsky normal form 淡江大學資訊管理系侯永昌 18

Chomsky Normal Form § 例:將下列生產規則改為符合 Chomsky normal form 的形式 S ABa, A aab, B

Chomsky Normal Form § 例:將下列生產規則改為符合 Chomsky normal form 的形式 S ABa, A aab, B Ac § 步驟 2: l l S ABCa, A Ca. Cb, B ACc Ca a , Cb b, Cc c § 步驟 3: l S AD 1, D 1 BCa, A Ca. D 2, D 2 Ca. Cb § 步驟 4: l S AD 1, D 1 BCa, B ACc , Ca a , A Ca. D 2, Cb b, D 2 Ca. Cb, Cc c 淡江大學資訊管理系侯永昌 20

Greibach Normal Form § A context-free grammar is in Greibach normal form if all

Greibach Normal Form § A context-free grammar is in Greibach normal form if all productions are of the form A ax, where a T and x V* § Greibach normal form 和第五章第 16頁所說的 s-grammar有些類似,但是它允許 (A, a) 的配 對出現多次,而s-grammar不允許 § 例:S AB, A a. A | b. B | b, B b 不符合 Greibach normal form,但是利用第 5 頁的定 理可以得到:S a. AB | b. B。加上原來 的 A a. A | b. B | b, B b,就符合了 Greibach normal form 淡江大學資訊管理系侯永昌 21

Greibach Normal Form § 例:S ab. Sa | aa 不符合 Greibach normal form,但是改成 S

Greibach Normal Form § 例:S ab. Sa | aa 不符合 Greibach normal form,但是改成 S a. BSB | a. A, A a, B b 後,就符合了 Greibach normal form § 定理:For every context-free grammar G with L(G), there exists an equivalent grammar G’ in Greibach normal form 淡江大學資訊管理系侯永昌 22