1 / 29

Simplification of Grammars

Simplification of Grammars. Lecture 17 Naveen Z Quazilbash. Overview. Attendance Motivation Simplification of Grammars Eliminating useless variables Eliminating null productions Eliminating unit productions Quiz result. Motivation for grammar simplification. Parsing Problem

miyoko
Download Presentation

Simplification of 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 Grammars Lecture 17 Naveen Z Quazilbash

  2. Overview • Attendance • Motivation • Simplification of Grammars • Eliminating useless variables • Eliminating null productions • Eliminating unit productions • Quiz result

  3. Motivation for grammar simplification • Parsing Problem • Given a CFG G and string w, determine if wϵ L(G). Fundamental problem in compiler design and natural language processing • If G is in general form then the procedure maybe very inefficient. So the grammar is “transformed” into a simpler form to make the parsing problem easier.

  4. Simplification of Grammars • It involves the removal of: • Useless variables • ε-productions • Unit productions

  5. Useless variables: There are two types of useless variables: • Variables that cannot be reached • Variables that do not derive any strings

  6. ε-productions E.g.: Aε • Note that if we remove these productions, the language no longer includes the empty string.

  7. Unit productions: They are of the form AB Or AA

  8. 1) Unreachable Variables • E.g.: SBS|B|E ADA|D|S BCB|C CaC|a DbD|b EcE|c

  9. To find unreachable variables, draw a dependency graph • Dependency Graph: • Vertices of the graph are variables • The graph doesn’t include alphabet symbols, such as “a” or “b” • If there is a production A…..B…, i.e., the left side is A and the right side includes B, then there is an edge AB

  10. A variable is reachable if there is a path from S to this variable • S itself is always reachable • After identifying unreachable variables, remove all productions with unreachable left side.

  11. SBS|B|E ADA|D|S BCB|C CaC|a DbD|b EcE|c • Drawing its dependency graph: • Reachable: S, B, C, E B C S E A D

  12. Grammar without unreachable variables: SBS|B|E BCB|C CaC|a EcE|c • Ex: Determine its language!!

  13. 2) Variables that don’t terminate • A variable A terminates if either: • There is a production A…. with no variables on the right, e.g. Aaabc, OR • There is a production A… where all variables on the right terminate; e.g. AaBbaC, where B and C terminate. • Note: to find all variables that terminate, keep looking for such productions until you cannot find any new ones.

  14. TASK Example: SA|BC|DE AaA|bA BbB|b CEF DdD|BD|BA EaE|a FcFc|c • Remove all productions that include a variable that doesn’t terminate. • Note: We remove a production if it has such a variable on either side.

  15. Solution x SA|BC|DE X AaA|bA x BbB|b x CEF X DdD|BD|BA x EaE|a x FcFc|c

  16. SBC • BbB|b • CEF • EaE|a • FcFc|c • Ex: Determine its language.

  17. 3) Eliminating ε-Productions • Nullable variables: A variable is nullable if either: • There is a production Aε, or • There is a production AB1B2…Bn(only variables, no symbols), where all variables on the right side are nullable. • Note: to find all nullable variables, keep looking for such productions, until you cannot find any new ones.

  18. TASK SSAB|SBC|BC AaA|a BbB|bC|C CcC|ε • First we find variables that can lead to the empty string: C=>ε B=>C=>ε S=>BC=>B=>C=>ε

  19. x SSAB|SBC|BC AaA|a xBbB|bC|C xCcC|ε • Thus, S, B, and C can lead to ε; they are called nullablevariables

  20. For each production that has nullable variables, consider all possible ways to skip some of these variables and add the corresponding productions. • E.g. WaWXaYZb, suppose that X, Y and Z are nullable; then there are 8 ways to skip some of them. • WaWab|aWXab|aWaYb|aWaZb|aWXaYb|aWXaZb| aWaYZb|aWXaYZb

  21. Back to our grammar where S,B and C are nullable: SA|AB|SA|SAB|S|B|C|SB|BC|SBC AaA|a Bb|bB|bC|C Cc|cC|ε • Now, we can remove the ε- productions without changing the language. • The only possible change is losing the empty string, if it is in the original language.

  22. So our grammar without null productions becomes: SA|AB|SA|SAB|S|B|C|SB|BC|SBC AaA|a Bb|bB|bC|C Cc|cC

  23. 4) Eliminating Unit Productions SAa|B Aa|bc|B BA|bb|C|cC Ca|C • First, for every variable, we find all single variables that can be reached from it: • For S: S=>B=>A, S=>B=>C • For A: A=>B=>C • For B: B=>A, B=>C • For C: NONE (C itself doesn’t count)

  24. For finding reachable single variables, what should we do?

  25. Use Dependency Graph! • Drawing Dependency Graph: • Vertices of the graph are variables. • If there is a unit production AB, then there is an edge AB. • A single variable is reachable from A if there is a pth from A to B.

  26. Dependency Graph: B S C A

  27. To construct an equivalent grammar without unit productions: • Remove all unit productions • For each pair A=>*B, where B is a single variable reachable from A, consider all productions Bp1|p2|…|pn; and add the corresponding productions A p1|p2|…|pn. • for example, since A=>*B and Bbb|cC, add the productions Abb|cC

  28. SAa|B Aa|bc|B BA|bb|C|cC Ca|C SAa Bbb|cC Aa|bc Ca • Note that the variable B has become useless and we need to remove it! • Sbb|cC|a|bc|a • Ba|bc|a • Abb|cC|a • Ca Old non-unit productions new productions

  29. Summary • Main steps of simplifying a grammar: • Remove useless variables, which cannot be reached or do not terminate. • Remove ε- productions. • Remove unit productions. • Remove useless variables again!

More Related