Simplification of Context-Free Grammars • Some useful substitution rules... Then there exists an equivalent grammar G^ = V^, T^, S, P^ that does not contain any useless variables or prod
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.
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 7Removing Useless Productions
S aSb | | A
A aA
S A is redundant as A cannot be transformed into a
terminal string.
Trang 8Removing Useless Productions
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 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 14Theorem 6.1 (cont’d)
Proof:
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.
Trang 16Example 6.5
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.
For all productions
B A1A2 An (Ai VN)
Trang 19Theorem 6.2 (cont’d)
Proof:
• For each production in P of the form:
put into P^ that production as well as all those generated by
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 24Example 6.6
S Aa | B
B A | bb
A a | bc | B
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 G 1 = (V 1 , T, S, P 1 ).
• V 1 = V {B a | a T} P 1 = P {B a 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 P 1 :
For each production
A x 1 x2 xn (n 2, x i TV)
add A C 1 C 2 C n to P 1
where Ci = xi if xi V or C i = Ba if xi = a
Trang 32Theorem 6.5 (cont’d)
Proof:
Construct G^ = (V^, T, S, P^) from G1 = (V1, T, S, P1).
• V^ = V 1
• 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
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