Simplification of Context-Free Grammars

38 358 0
Simplification of Context-Free Grammars

Đ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

Simplification of Context-Free Grammars Quan Thanh Tho qttho@cse.hcmut.edu.vn Simplification of Context-Free Grammars • Some useful substitution rules • Removing useless productions • Removing λ-productions • Removing unit-productions Some Useful Substitution Rules G = (V, T, S, P) A → x Bx ∈ P B → y | y | | y ∈ P n L(G) = L(G^) G^ = (V, T, S, P^) A → x y x | x y x | | x y x ∈ P^ 1 2 n Example G = ({A, B}, {a, b}, A, P) A → a | aaA | abBc B → abbA | b G^ = (V, T, S, P^) A → a | aaA | ababbAc | abbc Example 6.1 G = (V, T, S, P) A → Ax | Ax | | Ax ∈ P n A → y | y | | y ∈ P m L(G) = L(G^) G^ = (V∪{Z}, T, S, P^) A → y | y Z (i =1, m) ∈ P^ i i Z → x | x Z (i =1, n) ∈ P^ i i Example 6.2 G = ({A, B}, {a, b}, A, P) A → Aa | aBc | λ B → Bb | ba A → aBc | aBcZ | Z | λ A → aBc | aBcZ | Z | λ Z → a | aZ Z → a | aZ B → Bb | ba B → ba | baY Y → b | bY Removing Useless Productions S → aSb | λ | A A → aA S → A is redundant as A cannot be transformed into a terminal string 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 it it involves any useless variable Example 6.3 G = ({S, A, B}, {a, b}, S, P) S→A A → aA | λ B → bA Example 6.4 G = ({S, A, B, C}, {a, b}, S, P) S → aS | A | C S → aS | A A→a A→a B → aa S → aS | A B → aa A→a C → aCb 10 Example 6.6 S → Aa | B B → A | bb A → a | bc | B 24 Example 6.7 S → Aa | B S → Aa A → a | bc | B A → a | bc B → A | bb B → bb S ⇒* A S → a | bc | bb S ⇒* B A → bb A ⇒* B B → a | bc B ⇒* A 25 Theorem 6.4 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 26 Theorem 6.4 (cont’d) 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 Proof: Remove λ-productions Remove unit-productions Remove useless-productions 27 Two Important Normal Forms • Chomsky normal form • Greibach normal form 28 Chomsky Normal Form 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 29 Theorem 6.5 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 30 Theorem 6.5 (cont’d) Proof: First, construct an equivalent grammar G = (V , T, S, P ) 1 • V1 = V ∪ {Ba | a ∈ T} P = P ∪ {B → a | a ∈ T} a • Remove all terminals from productions of length > 1: Put all productions A → a into P Repeat until no more productions are added to P : For each production A → x x x n (n ≥ 2, x ∈ T∪V) i add A → C C C to P n where C = x if x ∈ V or C = B if x = a i i i i a i 31 Theorem 6.5 (cont’d) Proof: Construct G^ = (V^, T, S, P^) from G = (V , T, S, P ) 1 • 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 → C C C n (n > 2) add A → C D , D → C D , , D → C D to P^ 1 2 n-2 n-1 n 32 Example 6.8 S → ABa A → aaB B → aC 33 Greibach Normal Form 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* 34 Theorem 6.6 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 35 Theorem 6.6 (cont’d) Proof: • Rewrite the grammar in Chomsky normal form • Relabel variables A1, A2, , An • Rewrite the grammar so that all productions have one of the A →Ax i j j (j > i) Z →Ax i j j (j ≤ n, Z introduced to eliminate left recursion) i A → ax i i following forms: (a ∈ T and x ∈ V*) i • Start from An → axn to derive Greibach productions 36 Example 6.9 A →A A |b 2 A →A A |a 2 37 Homework • Exercises: 3, 4, 5, 6, 7, 8, 17, 22 of Section 6.1 - Linz’s book • Exercises: 2, 3, 4, 6, 9, 10, 11 of Section 6.2 - Linz’s book 38 .. .Simplification of Context-Free Grammars • Some useful substitution rules • Removing useless productions • Removing... useful production ⇒ G^ = (V^, T^, S, P^) 14 Removing λ-Productions • Any production of a context-free grammar of the form: A→λ is called a λ-production • Any variable A for which the derivation:... equivalent grammar G^ having no λ-productions 17 Theorem 6.2 (cont’d) Proof: • Find the set VN of all nullable variables of G: For all productions A → λ, put A into V N Repeat until no more variables

Ngày đăng: 13/05/2014, 10:21

Mục lục

  • Simplification of Context-Free Grammars

  • Some Useful Substitution Rules

  • Two Important Normal Forms

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

Tài liệu liên quan