Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars.

Similar presentations


Presentation on theme: "Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars."— Presentation transcript:

1 Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars

2 Fall 2006Costas Buch - RPI2 A Substitution Rule Substitute Equivalent grammar

3 Fall 2006Costas Buch - RPI3 Equivalent grammar Substitute

4 Fall 2006Costas Buch - RPI4 In general: Substitute equivalent grammar

5 Fall 2006Costas Buch - RPI5 Nullable Variables Nullable Variable: Example: Nullable variable

6 Fall 2006Costas Buch - RPI6 Substitute Removing After we remove all the all the nullable variables disappear (except for the start variable)

7 Fall 2006Costas Buch - RPI7 Unit-Productions Unit Production: (a single variable in both sides) Example: Unit Productions

8 Fall 2006Costas Buch - RPI8 Substitute Removal of unit productions:

9 Fall 2006Costas Buch - RPI9 Remove can be removed immediately Unit productions of form

10 Fall 2006Costas Buch - RPI10 Substitute

11 Fall 2006Costas Buch - RPI11 Remove repeated productions Final grammar

12 Fall 2006Costas Buch - RPI12 Useless Productions Some derivations never terminate... Useless Production

13 Fall 2006Costas Buch - RPI13 Another grammar: Not reachable from S Useless Production

14 Fall 2006Costas Buch - RPI14 In general: If there is a derivation Then variable is useful Otherwise, variable is useless consists of terminals

15 Fall 2006Costas Buch - RPI15 A production is useless if any of its variables is useless Productions useless Variables useless

16 Fall 2006Costas Buch - RPI16 Example Grammar: Removing Useless Variables and Productions

17 Fall 2006Costas Buch - RPI17 First: find all variables that can produce strings with only terminals or Round 1: Round 2: (possible useful variables) (the right hand side of production that has only terminals) (the right hand side of a production has terminals and variables of previous round) This process can be generalized

18 Fall 2006Costas Buch - RPI18 Then, remove productions that use variables other than

19 Fall 2006Costas Buch - RPI19 Second: Find all variables reachable from Use a Dependency Graph where nodes are variables unreachable

20 Fall 2006Costas Buch - RPI20 Keep only the variables reachable from S Final Grammar Contains only useful variables

21 Fall 2006Costas Buch - RPI21 Removing All Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables This sequence guarantees that unwanted variables and productions are removed

22 Fall 2006Costas Buch - RPI22 Normal Forms for Context-free Grammars

23 Fall 2006Costas Buch - RPI23 Chomsky Normal Form Each productions has form: variable or terminal

24 Fall 2006Costas Buch - RPI24 Examples: Not Chomsky Normal Form Chomsky Normal Form

25 Fall 2006Costas Buch - RPI25 Conversion to Chomsky Normal Form Example: Not Chomsky Normal Form We will convert it to Chomsky Normal Form

26 Fall 2006Costas Buch - RPI26 Introduce new variables for the terminals:

27 Fall 2006Costas Buch - RPI27 Introduce new intermediate variable to break first production:

28 Fall 2006Costas Buch - RPI28 Introduce intermediate variable:

29 Fall 2006Costas Buch - RPI29 Final grammar in Chomsky Normal Form: Initial grammar

30 Fall 2006Costas Buch - RPI30 From any context-free grammar (which doesn’t produce ) not in Chomsky Normal Form we can obtain: an equivalent grammar in Chomsky Normal Form In general:

31 Fall 2006Costas Buch - RPI31 The Procedure First remove: Nullable variables Unit productions (Useless variables optional)

32 Fall 2006Costas Buch - RPI32 Then, for every symbol : In productions with length at least 2 replace with Add production New variable: Productions of form do not need to change!

33 Fall 2006Costas Buch - RPI33 Replace any production with New intermediate variables:

34 Fall 2006Costas Buch - RPI34 Observations Chomsky normal forms are good for parsing and proving theorems It is easy to find the Chomsky normal form for any context-free grammar

35 Fall 2006Costas Buch - RPI35 Greinbach Normal Form All productions have form: symbolvariables

36 Fall 2006Costas Buch - RPI36 Examples: Greinbach Normal Form Not Greinbach Normal Form

37 Fall 2006Costas Buch - RPI37 Conversion to Greinbach Normal Form: Greinbach Normal Form

38 Fall 2006Costas Buch - RPI38 Observations Greinbach normal forms are very good for parsing strings (better than Chomsky Normal Forms) However, it is difficult to find the Greinbach normal of a grammar


Download ppt "Fall 2006Costas Buch - RPI1 Simplifications of Context-Free Grammars."

Similar presentations


Ads by Google