Then there exists an equivalent grammar G^ = V^, T^, S, P^ that does not contain any useless variables or productions... • Remove those variables and the productions involving them.. • E
Trang 1Simplification of Context-Free
Grammars
Quan Thanh Tho qttho@cse.hcmut.edu.vn
Trang 2Simplification of Context-Free
Grammars
• Some useful substitution rules.
• Removing useless productions.
• Removing λ -productions.
• Removing unit-productions.
Trang 3Some Useful Substitution Rules
G = (V, T, S, P)
A → x1Bx2 ∈ P
B → y1 | y2 | | yn ∈ P
L(G) = L(G^) G^ = (V, T, S, P^)
A → x1y1x2 | x1y2x2 | | x1ynx2 ∈ P^
Trang 5A → yi | yiZ (i =1, m) ∈ P^
Z → xi | xiZ (i =1, n) ∈ P^
Trang 7Removing Useless Productions
S → aSb | λ | A
A → aA
S → A is redundant as A cannot be transformed into a terminal string.
Trang 12Theorem 6.1
Let G = (V, T, S, P) be a context-free grammar
Then there exists an equivalent grammar G^ = (V^, T^, S, P^)
that does not contain any useless variables or productions
Trang 132 Repeat until no more variables are added to V1:
For every A ∈ T for which P has a production of the form
A → x1x2 xn (xi ∈ T* ∪ V1) add A to V1.
3 Take P as all the productions in P with symbols in (V ∪ T)*
Trang 14Theorem 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^)
Trang 16Example 6.5
S → aS1b S → aS1b | ab
S1 → aS1b | λ S1 → aS1b | ab
Trang 17Theorem 6.2
Let G = (V, T, S, P) be a CFG such that λ ∉ L(G)
Then there exists an equivalent grammar G^ having no
λ-productions
Trang 18Theorem 6.2 (cont’d)
Proof:
• Find the set VN of all nullable variables of G:
1 For all productions A → λ , put A into VN.
2 Repeat until no more variables are added to VN:
For all productions
B → A1A2 An (Ai ∈ VN) add B to VN.
Trang 19Theorem 6.2 (cont’d)
Proof:
• For each production in P of the form:
A → x1x2 xm (m ≥ 1, xi ∈ V∪T) put into P^ that production as well as all those generated by 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
Any production of a context-free grammar of the form:
A → B
is called a unit-production
Trang 22Theorem 6.3
Let G = (V, T, S, P) be a CFG without λ-productions
Then there exists an equivalent grammar G^ = (V, T, S, P^) that does not have any unit-productions
Trang 23Theorem 6.3 (cont’d)
Proof:
1 Put into P^ all non-unit-productions of P
2 Repeat until no more productions are added to P^:
For every A and B ∈ V such that A ⇒ * B and B → y1 | y2 | | yn ∈ P^ add A → y1 | y2 | | yn to P^.
Trang 26Theorem 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
Trang 27Theorem 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
Trang 28Two Important Normal Forms
• Chomsky normal form.
• Greibach normal form.
Trang 29Chomsky 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.
Trang 30Theorem 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
Trang 31Theorem 6.5 (cont’d)
Proof:
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:
1 Put all productions A → a into P1.
2 Repeat until no more productions are added to P1:
For each production
A → x1x2 xn (n ≥ 2, xi ∈ T ∪ V) add A → C1C2 Cn to P1
Trang 32• 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 → C1C2 Cn (n > 2) add A → C1D1, D1 → C2D2, , Dn-2 → Cn-1Dn to P^.
Trang 33Example 6.8
S → ABa
A → aaB
B → aC
Trang 34Greibach 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*.
Trang 35Theorem 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
Trang 37Example 6.9
A2 → A1A2 | b
A1 → A2A2 | a