Formal Languages and Automata http mail im tku























- Slides: 23
形式語言 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
第五章 與上下文無關的語言 (Context-Free Languages)
Context-Free Grammars § Regular grammar 比較嚴謹,他要求 production 的左邊是單一變數,右邊是 left linear 或 right linear,如果我們放寬對右邊的限制,就形成 了 context-free grammar § A grammar G = (V, T, S, P) is said to be contextfree if all productions in P have the form A x, where A V and x (V T)* § A language L is said to be context-free if and only if there is a context-free grammar G such that L = L(G) 淡江大學資訊管理系侯永昌 3
Context-Free Grammars § 例:G = ({S}, {a, b}, S, P) with productions S a. Sa, S b. Sb, S is context-free S a. Sa aa. Saa aab. Sbaa aabbaa L(G) = {ww. R : w {a, b}*} § 例:G with productions S ab. B, A aa. Bb, B bb. Aa, A is context-free S ab. B abbb. Aa abbbaa. Bba abbbaabb. Aaba abbbaa. Bbaba abbbaabb. Aababa ab(bbaa)2 bba(ba)2 L(G) = {ab(bbaa)nbba(ba)n : n 0} 淡江大學資訊管理系侯永昌 4
Context-Free Grammars § 例:L = {anbm : n m} is context-free For n > m, S AS 1, S 1 a. S 1 b | , A a. A | a For n < m, S S 1 B, S 1 a. S 1 b | , B b. B | b S AS 1|S 1 B, S 1 a. S 1 b| , A a. A|a, B b. B|b § 例:S a. Sb | SS | is context-free L(G) = {abaabb, aababb, ababab, …} L(G) = {w {a, b}* : na(w) = nb(w) and na(v) nb(v), where v is any prefix of w} § 上述兩個語言都不是線性 (linear),因為 production 的右邊有兩個以上的變數 (variable) 淡江大學資訊管理系侯永昌 5
Leftmost and Rightmost Derivations § 當 production 的右邊有兩個以上的變數時,那 一個變數會被優先代換?就有一些不同的考量 § 例:G = ({S, A, B}, {a, b}, S, P) with productions 1. 2. 3. 4. 5. S AB A aa. A A B Bb B 1 2 3 4 5 § S AB aa. Bb aab 1 4 2 5 3 § S ABb aa. Ab aab 淡江大學資訊管理系侯永昌 6
Leftmost and Rightmost Derivations § A derivation is said to be leftmost if in each step the leftmost variable in the sentential form is replaced. If in each step the rightmost variable is replaced, we call the derivation rightmost. § 例:Grammar with productions S a. AB, A b. Bb, B A|. Then leftmost derivation S a. AB ab. Ab. B abbbb rightmost derivation S a. AB a. A ab. Bb ab. Ab abb. Bbb abbbb 淡江大學資訊管理系侯永昌 7
Derivation (Parse) Trees § Let G = (V, T, S, P) be a context-free grammar. An ordered tree is a derivation tree for G if and only if it has the following properties 1. The root is labeled S 2. Every leaf has a label from T { } 3. Every interior vertex (a vertex that is not a leaf) has a label from V 4. If a vertex has label A V, and its children are labeled (from left to right) a 1, a 2, …, an, then P must contain a production of the form A a 1 a 2…an 5. A leaf labeled has no siblings, that is, a vertex with a child labeled can have no other children 淡江大學資訊管理系侯永昌 9
Derivation (Parse) Trees § § 如果將第 2 點修正為 every leaf has a label from V T { },這樣的樹稱為partial derivation tree 例:Grammar with productions S a. AB, A b. Bb, B A|. Then 淡江大學資訊管理系侯永昌 11
Derivation (Parse) Trees § 定理:Let G = (V, T, S, P) be a context-free grammar. Then for every w L(G), there exists a derivation tree of G whose yield is w. Conversely, the yield of any derivation tree is in L(G). Also, if t. G is any partial derivation tree for G whose root is label S, then the yield of t. G is a sentential form of G 淡江大學資訊管理系侯永昌 12
Parsing § § The term parsing describes finding a sequence of productions by which a w L(G) is derived 例:S SS | a. Sb | b. Sa | , w = aabb § S SS, S a. Sb, S b. Sa, S ,後兩項很快 就知道可以刪去 § S SSS, S SS a. Sb. S, S SS b. Sa. S, S S, § S a. Sb a. SSb, S a. Sb aa. Sbb, S a. Sb ab. Sab, S a. Sb ab, § S a. Sb aa. Sbb aabb 淡江大學資訊管理系侯永昌 13
Parsing § 定理:Suppose that G = (V, T, S, P) is a contextfree grammar that does not have any rule of the form A , or A B, where A, B V. Then the exhaustive search parsing method can be made into an algorithm which, for any w *, either produces a parsing of w or tells us that no parsing is possible 淡江大學資訊管理系侯永昌 15
Parsing § § § A context-free grammar G = (V, T, S, P) is said to be a simple grammar or s-grammar if all its productions are of the form A ax, where A V, a T, x V*, and any pair (A, a) occurs at most once in P 例:S a. S | b. SS | c is an s-grammar 例:S a. S | b. SS | a. SS | c is not an s-grammar 淡江大學資訊管理系侯永昌 16
Ambiguity in Grammars § § A context-free grammar G is said to be ambiguity if there exists some w L(G) that has at least two distinct derivation trees 例:S a. Sb | SS | 淡江大學資訊管理系侯永昌 17
Ambiguity in Grammars § 例:G = ({E, I}, {a, b, c, +, *, (, )}, E, P) with productions E I, E E+E, E E*E, E (E), I a | b | c a+b*c ambiguous 淡江大學資訊管理系侯永昌 18
Ambiguity in Grammars § 例:G = ({E, T, F, I}, {a, b, c, +, *, (, )}, E, P) with productions E T, T F, F I, E E+T, T T*F, F (E), I a | b | c a+b*c unambiguous 淡江大學資訊管理系侯永昌 19
Ambiguity in Grammars § If L is a context-free language for which there exists an unambiguous grammar, then L is said to be unambiguous. If every grammar that generates L is ambiguous, then the language is called inherently ambiguous 淡江大學資訊管理系侯永昌 20
Ambiguity in Grammars § 例:L = {anbncm} {anbmcm} with n and m nonnegative, is an inherently ambiguous context-free language § § § L = L 1 L 2 S 1 c | A, A a. Ab | S 2 a. S 2 | B, B b. Bc | , S S 1 | S 2 當 string 為 anbncn 時,他可以由 S 1 開始推導, 先產生字串 c,也可以由 S 2 開始推導,先產 生字串 a inherently ambiguous 淡江大學資訊管理系侯永昌 21
Context-Free Grammars and Programming Languages § Programming language that can be modeled by a context-free grammar are usually referred to as its syntax § However, context-free grammars cannot interpret the meaning (semantics) of a particular construct § Not all programs that are syntactically correct are acceptable programs 淡江大學資訊管理系侯永昌 22























