Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2004COMP 3351 Simplifications of Context-Free Grammars.

Similar presentations


Presentation on theme: "Fall 2004COMP 3351 Simplifications of Context-Free Grammars."— Presentation transcript:

1 Fall 2004COMP 3351 Simplifications of Context-Free Grammars

2 Fall 2004COMP 3352 A Substitution Rule Substitute Equivalent grammar

3 Fall 2004COMP 3353 A Substitution Rule Equivalent grammar Substitute

4 Fall 2004COMP 3354 In general: Substitute equivalent grammar

5 Fall 2004COMP 3355 Nullable Variables Nullable Variable:

6 Fall 2004COMP 3356 Removing Nullable Variables Example Grammar: Nullable variable

7 Fall 2004COMP 3357 Substitute Final Grammar

8 Fall 2004COMP 3358 Unit-Productions Unit Production: (a single variable in both sides)

9 Fall 2004COMP 3359 Removing Unit Productions Observation: Is removed immediately

10 Fall 2004COMP Example Grammar:

11 Fall 2004COMP Substitute

12 Fall 2004COMP Remove

13 Fall 2004COMP Substitute

14 Fall 2004COMP Remove repeated productions Final grammar

15 Fall 2004COMP Useless Productions Some derivations never terminate... Useless Production

16 Fall 2004COMP Another grammar: Not reachable from S Useless Production

17 Fall 2004COMP In general: if then variable is useful otherwise, variable is useless contains only terminals

18 Fall 2004COMP A production is useless if any of its variables is useless Productions useless Variables useless

19 Fall 2004COMP Removing Useless Productions Example Grammar:

20 Fall 2004COMP First: find all variables that can produce strings with only terminals Round 1: Round 2:

21 Fall 2004COMP Keep only the variables that produce terminal symbols: (the rest variables are useless) Remove useless productions

22 Fall 2004COMP Second: Find all variables reachable from Use a Dependency Graph not reachable

23 Fall 2004COMP Keep only the variables reachable from S Final Grammar (the rest variables are useless) Remove useless productions

24 Fall 2004COMP Removing All Step 1: Remove -productions Step 2: Remove Unit-productions Step 3: Remove Useless productions

25 Fall 2004COMP Normal Forms for Context-free Grammars

26 Fall 2004COMP Chomsky Normal Form Each productions has form: variable or terminal

27 Fall 2004COMP Examples: Not Chomsky Normal Form Chomsky Normal Form

28 Fall 2004COMP Conversion to Chomsky Normal Form Example: Not Chomsky Normal Form

29 Fall 2004COMP Introduce variables for terminals:

30 Fall 2004COMP Introduce intermediate variable:

31 Fall 2004COMP Introduce intermediate variable:

32 Fall 2004COMP Final grammar in Chomsky Normal Form: Initial grammar

33 Fall 2004COMP 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:

34 Fall 2004COMP The Procedure First remove: Nullable variables Unit productions

35 Fall 2004COMP Then, for every symbol : In productions: replace with Add production New variable:

36 Fall 2004COMP Replace any production with New intermediate variables:

37 Fall 2004COMP Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Chomsky Normal Form

38 Fall 2004COMP Observations Chomsky normal forms are good for parsing and proving theorems It is very easy to find the Chomsky normal form for any context-free grammar

39 Fall 2004COMP Greibach Normal Form All productions have form: symbolvariables

40 Fall 2004COMP Examples: Greibach Normal Form Not Greibach Normal Form

41 Fall 2004COMP Conversion to Greibach Normal Form: Greibach Normal Form

42 Fall 2004COMP Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Greibach Normal Form

43 Fall 2004COMP Observations Greibach normal forms are very good for parsing It is hard to find the Greibach normal form of any context-free grammar

44 Fall 2004COMP The CYK Parser

45 Fall 2004COMP The CYK Membership Algorithm Input: Grammar in Chomsky Normal Form String Output: find if

46 Fall 2004COMP The Algorithm Grammar : String : Input example:

47 Fall 2004COMP 33547

48 Fall 2004COMP 33548

49 Fall 2004COMP 33549

50 Fall 2004COMP 33550

51 Fall 2004COMP Therefore: Time Complexity: The CYK algorithm can be easily converted to a parser (bottom up parser) Observation:


Download ppt "Fall 2004COMP 3351 Simplifications of Context-Free Grammars."

Similar presentations


Ads by Google