Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 38 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
38
Dung lượng
80 KB
Nội dung
Simplification of Context-Free Grammars Quan Thanh Tho qttho@cse.hcmut.edu.vn 2 Simplification of Context-Free Grammars • Some useful substitution rules. • Removing useless productions. • Removing λ-productions. • Removing unit-productions. 3 Some Useful Substitution Rules G = (V, T, S, P) A → x 1 Bx 2 ∈ P B → y 1 | y 2 | | y n ∈ P L(G) = L(G^) G^ = (V, T, S, P^) A → x 1 y 1 x 2 | x 1 y 2 x 2 | | x 1 y n x 2 ∈ P^ 4 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 5 Example 6.1 G = (V, T, S, P) A → Ax 1 | Ax 2 | | Ax n ∈ P A → y 1 | y 2 | | y m ∈ P L(G) = L(G^) G^ = (V∪{Z}, T, S, P^) A → y i | y i Z (i =1, m) ∈ P^ Z → x i | x i Z (i =1, n) ∈ P^ 6 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 7 Removing Useless Productions S → aSb | λ | A A → aA S → A is redundant as A cannot be transformed into a terminal string. 8 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. 9 Example 6.3 G = ({S, A, B}, {a, b}, S, P) S → A A → aA | λ B → bA 10 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 [...]... every A and B ∈ V such that A ⇒* B and B → y1 | y2 | | yn ∈ P^ add A → y1 | y2 | | yn to P^ 23 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... (xi ∈ T*∪V1) add A to V1 3 Take P1 as all the productions in P with symbols in (V 1∪ T)* 13 Theorem 6.1 (cont’d) Proof: • Draw the variable dependency graph for G1 and find all variables that cannot be reached from S • Remove those variables and the productions involving them • Eliminate any terminal that does not occur in a useful production ⇒ G^ = (V^, T^, S, P^) 14 Removing λ-Productions • Any production... 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: 1 Remove λ-productions 2 Remove unit-productions 3 Remove useless-productions... 1 Put all productions A → a and A → BC into P^ 2 Repeat until no more productions are added to P^: For each production A → C1C2 Cn (n > 2) add A → C1D1, D1 → C2D2, , Dn-2 → Cn-1Dn to P^ 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... 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... Relabel variables A1, A2, , An • Rewrite the grammar so that all productions have one of the following forms: Ai → Ajxj (j > i) Zi → Ajxj (j ≤ n, Zi introduced to eliminate left recursion) Ai → axi (a ∈ T and xi ∈ V*) • Start from An → axn to derive Greibach productions 36 . (cont’d) Proof: • Draw the variable dependency graph for G 1 and find all variables that cannot be reached from S. • Remove those variables and the productions involving them. • Eliminate any terminal