Simplification of Context-Free GrammarsSome useful substitution rules.. Chapter 6: Simplification of Context-Free Grammars... Removing Useless ProductionsG = V, T, S, P A production is u
Trang 1Chapter 6: Simplification of Context-Free
Grammars
November 13, 2009
Trang 2Simplification of Context-Free Grammars
Some useful substitution rules
Removing useless productions
Removing λ-productions
Removing unit-productions
Chapter 6: Simplification of Context-Free Grammars
Trang 3Some Useful Substitution Rules
Trang 4A → a | aaA | abbAc | abbc
Chapter 6: Simplification of Context-Free Grammars
Trang 7Removing Useless Productions
S → aSb | λ | A
A → aA
S → A is redundant as Acannot be transformed into aterminal string
Trang 8Removing Useless Productions
G = (V, T, S, P)
A production is useless if it involves anyuseless variable
Chapter 6: Simplification of Context-Free Grammars
Trang 12Theorem 6.1 - Linz ’s book
Theorem
Let G = (V, T, S, P) be a context-free grammar Then there
contain any useless variables or productions
Chapter 6: Simplification of Context-Free Grammars
Trang 13Proof of theorem 6.1 - Linz ’s book
Construct (V1, T, S, P1) such that V1 contains only variables
1 Set V1to ∅.
2 Repeat until no more variables are added to V 1 :
For every A ∈ T for which P has a production of the form
A → x 1 x 2 x n (x i ∈ T∗∪ V 1 ) add A to V 1
3 Take P1 as all the productions in P with symbols in (V1∪ T ) ∗
Trang 14Proof of theorem 6.1 - Linz ’s book
variables that cannot be reached from S
Remove those variables and the productions involving them.Eliminate any terminal that does not occur in a usefulproduction
⇒ G∗ = (V∗, T∗, S, P∗)
Chapter 6: Simplification of Context-Free Grammars
Trang 16Proof of theorem 6.1 - Linz ’s book
Chapter 6: Simplification of Context-Free Grammars
Trang 17Theorem 6.2 - Linz ’s book
Theorem
Let G = (V, T, S, P) be a CFG such that λ ∈ L(G) Then there
Trang 18Proof of theorem 6.2 - Linz ’s book
1 For all productions A → λ, put A into V N
2 Repeat until no more variables are added to V N :
For all productions
B → A 1 A 2 A n (A i ∈ V N ) add B to V N
Chapter 6: Simplification of Context-Free Grammars
Trang 19Proof of theorem 6.2 - Linz ’s book
For each production in P of the form:
A → x1x2 xm (m ≥ 1, xi ∈ V ∪ T )
replacing null variables with λin all possible combination
Exception: if all xi are nullable, then A → λ is not put into
P∗
Trang 21Removing Unit-Productions
Definition
Trang 22Theorem 6.3 - Linz ’s book
Theorem
Let G = (V, T, S, P) be a CFG without λ-productions Then there
have any unit-productions
Chapter 6: Simplification of Context-Free Grammars
Trang 23Proof of theorem 6.3 - Linz ’s book
For every A and B ∈ V such that
A ⇒∗B and B → y1| y2| | yn ∈ P ∗
add A → y 1 | y 2 | | y n to P∗.
Trang 25Example 6.7
S → Aa | B
A → a | bc | B
B → A | bb
Trang 27Theorem 6.4 - Linz ’s book
Theorem
Let L be a context-free language that does not contain λ Thenthere exists a CFG that generates L and does not have any uselessproductions, λ-productions, or unit-productions
Trang 28Proof of theorem 6.4 - Linz ’s book
Trang 29Two Important Normal Forms
Chomsky normal form
Greibachnormal form
Trang 30Chomsky Normal Form
Trang 31Theorem 6.5 - Linz ’s book
Theorem
normal form
Trang 32Proof of theorem 6.5 - Linz ’s book
Remove all terminals from productions of length > 1:
1 Put all productions A → a into P 1
2 Repeat until no more productions are added to P 1 :
For each production
A → x 1 x 2 x n (n ≥ 2, x i ∈ T ∪ V ) add A → C 1 C 2 C n to P 1
where C i = x i if x i ∈ V or C i = B a if x i = a
Chapter 6: Simplification of Context-Free Grammars
Trang 33Proof 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:
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 → C 1 C 2 C n (n ≥ 2) add A → C 1 D 1 , D 1 → C 2 D 2 , , D n−2 → C n−1 D n to P∗.
Trang 35Greibach Normal Form
Trang 36Theorem 6.6 - Linz ’s book
Theorem
normal form
Chapter 6: Simplification of Context-Free Grammars
Trang 37Proof of theorem 6.6 - Linz ’s book
Rewrite the grammar in Chomsky normal form
Trang 39Exercises: 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