sliek môn máy tự động chương 6 simplication of context-free

39 127 0
sliek môn máy tự động chương 6 simplication of context-free

Đ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 6: Simplification of Context-Free Grammars November 13, 2009 Chapter 6: Simplification of Context-Free Grammars Simplification of Context-Free Grammars Some useful substitution rules Removing useless productions Removing λ-productions Removing unit-productions Chapter 6: Simplification of Context-Free Grammars Some Useful Substitution Rules G = (V, T, S, P) A → x1 Bx2 ∈ P B → y1 | y2 | | yn ∈ P G ∗ = (V, T, S, P ∗ ) A → x1 y1 x2 | x1 y2 x2 | | x1 yn x2 ∈ P ∗ L(G ) = L(G ∗ ) Chapter 6: Simplification of Context-Free Grammars Example 6.1 G = ({A, B}, {a, b}, A, P) A → a | aaA | abBc B → bA | b G ∗ = ({A, B}, {a, b}, A, P ∗ ) A → a | aaA | abbAc | abbc Chapter 6: Simplification of Context-Free Grammars Eliminating left-recursive G = (V, T, S, P) A → Ax1 | Ax2 | | Axn ∈ P A → y1 | y2 | | ym ∈ P G ∗ = (V ∪ {Z}, T, S, P ∗ ) A → yi | yi Z (i=1,m) ∈ P ∗ Z → xi | xi Z (i=1,n) ∈ P ∗ L(G ) = L(G ∗ ) Chapter 6: Simplification of Context-Free Grammars Example 6.2 G = ({A, B}, {a, b}, A, P) A → Aa | aBc | λ B → Bb | ba A → aBcZ | Z | aBc | λ Z → a | aZ B → Bb | ba A → aBcZ | Z | aBc | λ Z → a | aZ B → ba | baY Y → b | bY Chapter 6: Simplification of Context-Free Grammars Removing Useless Productions S → aSb | λ | A A → aA S → A is redundant as A cannot be transformed into a terminal string Chapter 6: Simplification of Context-Free Grammars Removing Useless Productions G = (V, T, S, P) A ∈ V is useful iff there is w ∈ L(G) such that: S ⇒∗ xAy ⇒∗ w A production is useless if it involves any useless variable Chapter 6: Simplification of Context-Free Grammars Example 6.3 G = ({S, A, B}, {a, b}, S, P) S→A|B A → aA | λ C → bA B → Bc Chapter 6: Simplification of Context-Free Grammars Example 6.4 G = ({S, A, B, C}, {a, b}, S, P) S → aS | A | C S → aS | A S → aS | A A→a A→a A→a B → aa B → aa C → aCb Chapter 6: Simplification of Context-Free Grammars Example 6.7 S → Aa | B A → a | bc | B B → A | bb Chapter 6: Simplification of Context-Free Grammars Example 6.7 S → Aa | B A → a | bc | B B → A | bb S ⇒∗ A S ⇒∗ B A ⇒∗ B B ⇒∗ A S → Aa A → a | bc B → bb S → a | bc | bb A → bb B → a | bc Chapter 6: Simplification of Context-Free Grammars Theorem 6.4 - Linz ’s book Theorem Let L be a context-free language that does not contain λ Then there exists a CFG that generates L and does not have any useless productions, λ-productions, or unit-productions Chapter 6: Simplification of Context-Free Grammars Proof of theorem 6.4 - Linz ’s book Remove λ-productions Remove unit-productions Remove useless-productions Chapter 6: Simplification of Context-Free Grammars Two Important Normal Forms Chomsky normal form Greibach normal form Chapter 6: Simplification of Context-Free Grammars Chomsky Normal Form Definition A context-free grammar G = (V, T, S, P) is in Chomsky normal form iff all productions are of the form: A → BC or A→a where A, B, C ∈ V and a ∈ T Chapter 6: Simplification of Context-Free Grammars Theorem 6.5 - Linz ’s book Theorem Any context-free grammar G = (V, T, S, P) such that λ ∈ L(G) / has an equivalent grammar G ∗ = (V ∗ , T, S, P ∗ ) in Chomsky normal form Chapter 6: Simplification of Context-Free Grammars Proof of theorem 6.5 - Linz ’s book First, construct an equivalent grammar G1 = (V1 , T, S, P1 ) V1 = V ∪ {Ba | a ∈ T} P1 = P ∪ {Ba → a | a ∈ T} Remove all terminals from productions of length > 1: Put all productions A → a into P1 Repeat until no more productions are added to P1 : For each production A → x1 x2 xn (n ≥ 2, xi ∈ T ∪ V ) add A → C1 C2 Cn to P1 where Ci = xi if xi ∈ V or Ci = Ba if xi = a Chapter 6: Simplification of Context-Free Grammars Proof of theorem 6.5 - Linz ’s book Construct G ∗ = (V ∗ , T, S, P ∗ ) from G1 = (V1 , T, S, P1 ) V ∗ = V1 Reduce the length of the right sides of the productions: Put all productions A → a and A → BC into P ∗ Repeat until no more productions are added to P ∗ : For each production A → C1 C2 Cn (n ≥ 2) add A → C1 D1 , D1 → C2 D2 , , Dn−2 → Cn−1 Dn to P ∗ Chapter 6: Simplification of Context-Free Grammars Example 6.8 S → ABa A → aaB B → aC Chapter 6: Simplification of Context-Free Grammars Greibach Normal Form Definition A context-free grammar G = (V, T, S, P) is in Greibach normal form iff all productions are of the form: A → ax where a ∈ T and x ∈ V ∗ Chapter 6: Simplification of Context-Free Grammars Theorem 6.6 - Linz ’s book Theorem Any context-free grammar G = (V, T, S, P) such that λ ∈ L(G) / has an equivalent grammar G ∗ = (V ∗ , T, S, P ∗ ) in Greibach normal form Chapter 6: Simplification of Context-Free Grammars Proof of theorem 6.6 - Linz ’s book Rewrite the grammar in Chomsky normal form Relabel variables A1 , A2 , , An Rewrite the grammar so that all productions have one of the following forms: Ai → Aj xj (j > i) Zi → Aj xj (j ≤ n, Zi introduced to eliminate left recursion) Ai → axi (a ∈ T and xi ∈ V ∗ ) Start from An → axn to derive Greibach productions Chapter 6: Simplification of Context-Free Grammars Example 6.9 A2 → A1 A2 |b A1 → A2 A2 |a Chapter 6: Simplification of Context-Free Grammars Exerises Exercises: 3, 4, 5, 6, 7, 8, 17, 22 of Section 6.1 - Linzs book Exercises: 2, 3, 4, 6, 9, 10, 11 of Section 6.2 - Linzs book Chapter 6: Simplification of Context-Free Grammars ... Chapter 6: Simplification of Context-Free Grammars Example 6. 9 A2 → A1 A2 |b A1 → A2 A2 |a Chapter 6: Simplification of Context-Free Grammars Exerises Exercises: 3, 4, 5, 6, 7, 8, 17, 22 of Section 6. 1... | yn to P ∗ Chapter 6: Simplification of Context-Free Grammars Example 6. 6 S → Aa | B B → A | bb A → a | bc | B Chapter 6: Simplification of Context-Free Grammars Example 6. 7 S → Aa | B A → a... Chapter 6: Simplification of Context-Free Grammars Proof of theorem 6. 4 - Linz ’s book Remove λ-productions Remove unit-productions Remove useless-productions Chapter 6: Simplification of Context-Free

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

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

Tài liệu liên quan