970 likes | 2.24k Views
Simplification of Context-Free Grammars. Simplification of Context-Free Grammars. Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions. Removing Left Recursion. Some Useful Substitution Rule. G = (V, T, S, P)
E N D
Simplification of Context-Free Grammars Some useful substitution rules. • Removing useless productions. • Removing -productions. • Removing unit-productions. Removing Left Recursion
Some Useful Substitution Rule 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^
Example G = ({A, B}, {a, b}, A, P) A a | aaA | abBc B b G^ = (V, T, S, P^) A a| aaA| abbc
Removing Useless Productions I. S aSb | | A A aA S A is redundant as A cannot be transformed into a terminal string. II. S A A aA | λ B bA B is useless as it will never be reached from S
Removing Useless Productions G = (V, T, S, P) A V is useful iff there is w L(G) if it is: • generating ie. A * w • reachable ie. S * xAy So in the process of removing useless productions, we first check that a symbol is generating or not and then check for reachability.
Example G = ({S, A, B, C}, {a, b}, S, P) S aS | A | C A a B aa C aCb S aS | A A a S aS | A A a B aa Here C is not generating And B is not reachable
Removing -Productions • Any production of a context-free grammar of the form: A is called a -production. • Any variable A for which the derivation: A * is possible is called nullable.
Example 1 S aS1b S aS1b | ab S1 aS1b | S1 aS1b | ab
Example 2 • S ABC • A aAA |l • B bBB |l • Cc Here A & B are nullable So S ABC|AC|BC|C A aAA | aA | a B bBB | bB |b Cc
Example S ABaC S ABaC | BaC | AaC | ABa | aC | Aa |Ba | a A BC A B | C | BC B b | B b C D | C D D d D d VN = {A, B, C}
Removing Unit-Productions Any production of a context-free grammar of the form: A B is called a unit-production.
Example S Aa | B B A | bb A a | bc | B
Example S Aa A a | bc B bb S Aa | B B A | bb A a | bc | B S * A S * B A * B B * A S a | bc | bb A bb B a | bc
Example Simplify the following grammar : S aA | aBB A aaA | l B bB | bbC C B Obtain its Language also.
Solution • B & C are not genarating remove thm • Remove the l production of A. • We get S aA |a A aaA |aa L(G) = (a(aa)*))