1 / 49

Simplifications of Context-Free Grammars

Simplifications of Context-Free Grammars. A Substitution Rule. equivalent grammar. substitute B. In general:. substitute B. equivalent grammar. Useless Productions. never terminates. Useless Production. Another grammar:. Not reachable from S. Useless Production. In general:. If.

maxowens
Download Presentation

Simplifications 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. Simplifications of Context-Free Grammars

  2. A Substitution Rule equivalent grammar substitute B

  3. In general: substitute B equivalent grammar

  4. Useless Productions

  5. never terminates...

  6. Useless Production

  7. Another grammar:

  8. Not reachable from S Useless Production

  9. In general: If Then is useful Otherwise is useless

  10. Removing Useless Productions Example Grammar

  11. First: find all variables that produce strings with only terminals Round 1: Round 2:

  12. Keep only the variables that produce terminal symbols

  13. Second: Find all variables reachable from S Dependency Graph not reachable

  14. Keep only the variables reachable from S Final Grammar

  15. Nullable Variables Nullable Variable:

  16. Removing Nullable Variables Example Grammar: Nullable variable

  17. Substitute Final Grammar

  18. Unit-Productions Unit Production:

  19. Removing Unit Productions Observation: Is removed immediately

  20. Example Grammar:

  21. substitute

  22. substitute

  23. Remove repeated productions Final grammar

  24. Removing All • 1. Remove Nullable Variables • 2. Remove Unit-Productions • 3. Remove Useless Variables

  25. Normal FormsforContext-free Grammars

  26. Chomsky Normal Form All productions have form: OR variable variable terminal

  27. Examples: Chomsky Normal Form NOT Chomsky Normal Form

  28. Convertion to Chomsky Normal Form • Example: NOT Chomsky Normal Form

  29. Introduce variables for terminals:

  30. Introduce intermediate variables:

  31. Final grammar in Chomsky Normal Form: Initial grammar

  32. In general: From any context-free grammar Not in Chomsky Normal Form We can obtain: An equivalent grammar in Chomsky Normal Form

  33. The Procedure: • Remove • nullable variables • unit productions

  34. For every symbol : Add new variable Add production In productions, replace with

  35. Replace any production: with: New variables:

  36. Theorem: For any context-free grammar there is an equivalent grammar in Chomsky Normal Form

  37. Greinbach Normal Form All rules have form: variables symbol

  38. Examples: NOT Greinbach Normal Form Greinbach Normal Form

  39. Conversion to Greinbach Normal Form Greinbach Normal Form

  40. Theorem: For any context-free grammar there is an equivalent grammar in Greinbach Normal Form

  41. An ApplicationofChomsky Normal Forms

  42. The CYK Membership Algorithm Inputs: • Grammar • in Chomsky Normal Form • String w Output: find if

  43. Algorithm: Input Examples Grammar : String :

  44. Therefore: Time Complexity: It can be easily converted to a parser

More Related