sliek môn máy tự động chương 5 context-free grammar

36 149 0
sliek môn máy tự động chương 5 context-free grammar

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Chapter 5: Context-Free Grammar November 9, 2009 Chapter 5: Context-Free Grammar Non-regular Languages Not all languages are regular L1 = {an b n : n ≥ 0} is not regular L2 = {(), (()), ((())), } is not regular Some properties of programming languages are not regular Chapter 5: Context-Free Grammar Context-Free Grammars G = (V, T, S, P) Productions are of the form: A→x A ∈ V and x ∈ (V ∪ T )∗ Chapter 5: Context-Free Grammar CFG and Regular Language A regular language is also a context-free language Why the name? Chapter 5: Context-Free Grammar Example 5.1 G = ({S}, {a, b}, S, P) P={ S → aSa S → bSb S→λ } S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa L(G) = {ww R | w ∈ {a, b}∗ } Chapter 5: Context-Free Grammar Example 5.2 G = ({S, A, B}, {a, b}, S, P) P={ S → abB A → aaBb B → bbAa A→λ } S ⇒ abB ⇒ abbbAa ⇒ abbbaaBba ⇒ abbbaabbAaba ⇒ abbbaabbaba Chapter 5: Context-Free Grammar Example 5.3 L = {an b m | n = m} is context-free S → AS1 | S1 B S1 → aS1 b | λ A → aA | a B → bB | b Chapter 5: Context-Free Grammar Example 5.4 G = ({S}, {a, b}, S, P) P = { S → aSb | SS | λ} S ⇒ aSb ⇒ aaSbb ⇒ aaSSbb ⇒ aaabSbb ⇒ aaababbb L(G) = ? Chapter 5: Context-Free Grammar Derivations G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B → Bb, B → λ}) S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab Chapter 5: Context-Free Grammar Leftmost and Rightmost Derivations Leftmost: in each step the leftmost variable in the sentential form is replaced Rightmost: in each step the rightmost variable in the sentential form is replaced Chapter 5: Context-Free Grammar Exhaustive Search Parsing Disadvantages It is not efficient If w ∈ L(G) then it may never terminate, due to: / A→B A→λ Chapter 5: Context-Free Grammar Restricted Grammar for Exhaustive Search Parsing Suppose G = (V, T, S, P) is a context-free grammar which does not have any rule of the form A → B or A → λ Then the exhaustive search parsing method can decide if w ∈ L(G) or not Chapter 5: Context-Free Grammar Theorem 5.1 For every context-free grammar G, there exists an algorithm that parses any w ∈ L(G) in a number of steps proportional to |w |3 Not satisfactory as linear time parsing algorithm (proportional to the length of a string) Chapter 5: Context-Free Grammar Simple Grammars (S-Grammars) G = (V, T, S, P) Productions are of the form: A → ax A ∈ V, a ∈ T, and x ∈ V ∗ , and any pair (A,a) can occur in at most one rule Chapter 5: Context-Free Grammar Parsing in Simple Grammars (S-Grammars) Any w ∈ L(G) can be parsed in at most |w | steps w = a1 a2 an S ⇒ a1 A1 A2 Am ⇒ a1 a2 B1 B2 Bk A1 A2 Am ⇒ Chapter 5: Context-Free Grammar S-Grammars and Contemporary Programming Languages Many features of Pascal-like programming languages can be expressed with s-grammars Chapter 5: Context-Free Grammar Ambiguity Definition A context-free grammar G is said to be ambiguous if there exits some w ∈ L(G) that has at least two distinct derivation trees Chapter 5: Context-Free Grammar Example 5.10 G = ({E, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c E→I E→E+E E→E*E E → (E) I→a|b|c Chapter 5: Context-Free Grammar Example 5.11 G = ({E, T, F, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c E→T|E+T T→F|T*F F → I | (E) I→a|b|c Chapter 5: Context-Free Grammar Unambiguous Grammar If L is a context-free language for which there exists an unambiguous grammar, then L is said to be unambiguous Chapter 5: Context-Free Grammar Inherently Ambiguous Grammar 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 L is called inherently ambiguous Chapter 5: Context-Free Grammar Example 5.12 L = {an b n c m } ∪ {an b m c m } is inherently ambiguous L = L1 ∪ L2 S → S1 | S2 S1 → S1 c | A S2 → aS2 | B A → aAb | λ B → bBc | λ Chapter 5: Context-Free Grammar CFGs and Programming Languages Important uses of formal languages: to define precisely a programming language to construct an efficient translator for it RLs are used for simple patterns, while CFGs for more complicated aspects Chapter 5: Context-Free Grammar Example 5.14 S-grammars: ::= if ::= then ::= else Chapter 5: Context-Free Grammar Exerises Exercises: 2, 3, 4, 6, 7, 9, 15, 17, 22 of Section 5.1 - Linzs book Exercises: 1, 2, 5, 6, 8, 10, 11, 12, 13, 15 of Section 5.2 Linzs book Exercises: 1, 2, of Section 5.3 - Linzs book Chapter 5: Context-Free Grammar ... are not regular Chapter 5: Context-Free Grammar Context-Free Grammars G = (V, T, S, P) Productions are of the form: A→x A ∈ V and x ∈ (V ∪ T )∗ Chapter 5: Context-Free Grammar CFG and Regular... the yield of the tree Chapter 5: Context-Free Grammar Example 5. 6 S → aAB S A → bBb a A B→A|λ b B b A λ b B yield: abbbb B λ Chapter 5: Context-Free Grammar b Example 5. 7 S → aAB S A → bBb a A B→A|λ... Chapter 5: Context-Free Grammar Example 5. 11 G = ({E, T, F, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c E→T|E+T T→F|T*F F → I | (E) I→a|b|c Chapter 5: Context-Free Grammar Unambiguous Grammar

Ngày đăng: 23/10/2014, 17:47

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan