1 / 16

Simplification of Context-Free Grammars

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)

marcin
Download Presentation

Simplification of Context-Free Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simplification of Context-Free Grammars

  2. Simplification of Context-Free Grammars Some useful substitution rules. • Removing useless productions. • Removing -productions. • Removing unit-productions. Removing Left Recursion

  3. 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^

  4. Example G = ({A, B}, {a, b}, A, P) A  a | aaA | abBc B b G^ = (V, T, S, P^) A  a| aaA| abbc

  5. 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

  6. 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.

  7. 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

  8. 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.

  9. Example 1 S  aS1b S  aS1b | ab S1  aS1b |  S1 aS1b | ab

  10. Example 2 • S  ABC • A  aAA |l • B  bBB |l • Cc Here A & B are nullable So S ABC|AC|BC|C A aAA | aA | a B  bBB | bB |b Cc

  11. 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}

  12. Removing Unit-Productions Any production of a context-free grammar of the form: A  B is called a unit-production.

  13. Example S  Aa | B B  A | bb A  a | bc | B

  14. 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

  15. Example Simplify the following grammar : S aA | aBB A aaA | l B bB | bbC C B Obtain its Language also.

  16. 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)*))

More Related