costas buch - rpi1 simplifications of context-free grammars

Download Costas Buch - RPI1 Simplifications of Context-Free Grammars

Post on 20-Dec-2015

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1
  • Costas Buch - RPI1 Simplifications of Context-Free Grammars
  • Slide 2
  • Costas Buch - RPI2 A Substitution Rule Substitute Equivalent grammar
  • Slide 3
  • Costas Buch - RPI3 A Substitution Rule Equivalent grammar Substitute
  • Slide 4
  • Costas Buch - RPI4 In general: Substitute equivalent grammar
  • Slide 5
  • Costas Buch - RPI5 Nullable Variables Nullable Variable:
  • Slide 6
  • Costas Buch - RPI6 Removing Nullable Variables Example Grammar: Nullable variable
  • Slide 7
  • Costas Buch - RPI7 Substitute Final Grammar
  • Slide 8
  • Costas Buch - RPI8 Unit-Productions Unit Production: (a single variable in both sides)
  • Slide 9
  • Costas Buch - RPI9 Removing Unit Productions Observation: Is removed immediately
  • Slide 10
  • Costas Buch - RPI10 Example Grammar:
  • Slide 11
  • Costas Buch - RPI11 Substitute
  • Slide 12
  • Costas Buch - RPI12 Remove
  • Slide 13
  • Costas Buch - RPI13 Substitute
  • Slide 14
  • Costas Buch - RPI14 Remove repeated productions Final grammar
  • Slide 15
  • Costas Buch - RPI15 Useless Productions Some derivations never terminate... Useless Production
  • Slide 16
  • Costas Buch - RPI16 Another grammar: Not reachable from S Useless Production
  • Slide 17
  • Costas Buch - RPI17 In general: if then variable is useful otherwise, variable is useless contains only terminals
  • Slide 18
  • Costas Buch - RPI18 A production is useless if any of its variables is useless Productions useless Variables useless
  • Slide 19
  • Costas Buch - RPI19 Removing Useless Productions Example Grammar:
  • Slide 20
  • Costas Buch - RPI20 First: find all variables that can produce strings with only terminals Round 1: Round 2:
  • Slide 21
  • Costas Buch - RPI21 Keep only the variables that produce terminal symbols: (the rest variables are useless) Remove useless productions
  • Slide 22
  • Costas Buch - RPI22 Second: Find all variables reachable from Use a Dependency Graph not reachable
  • Slide 23
  • Costas Buch - RPI23 Keep only the variables reachable from S Final Grammar (the rest variables are useless) Remove useless productions
  • Slide 24
  • Costas Buch - RPI24 Removing All Step 1: Remove Nullable Variables Step 2: Remove Unit-Productions Step 3: Remove Useless Variables
  • Slide 25
  • Costas Buch - RPI25 Normal Forms for Context-free Grammars
  • Slide 26
  • Costas Buch - RPI26 Chomsky Normal Form Each productions has form: variable or terminal
  • Slide 27
  • Costas Buch - RPI27 Examples: Not Chomsky Normal Form Chomsky Normal Form
  • Slide 28
  • Costas Buch - RPI28 Convertion to Chomsky Normal Form Example: Not Chomsky Normal Form
  • Slide 29
  • Costas Buch - RPI29 Introduce variables for terminals:
  • Slide 30
  • Costas Buch - RPI30 Introduce intermediate variable:
  • Slide 31
  • Costas Buch - RPI31 Introduce intermediate variable:
  • Slide 32
  • Costas Buch - RPI32 Final grammar in Chomsky Normal Form: Initial grammar
  • Slide 33
  • Costas Buch - RPI33 From any context-free grammar (which doesnt produce ) not in Chomsky Normal Form we can obtain: An equivalent grammar in Chomsky Normal Form In general:
  • Slide 34
  • Costas Buch - RPI34 The Procedure First remove: Nullable variables Unit productions
  • Slide 35
  • Costas Buch - RPI35 Then, for every symbol : In productions: replace with Add production New variable:
  • Slide 36
  • Costas Buch - RPI36 Replace any production with New intermediate variables:
  • Slide 37
  • Costas Buch - RPI37 Theorem: For any context-free grammar (which doesnt produce ) there is an equivalent grammar in Chomsky Normal Form
  • Slide 38
  • Costas Buch - RPI38 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
  • Slide 39
  • Costas Buch - RPI39 Greinbach Normal Form All productions have form: symbolvariables
  • Slide 40
  • Costas Buch - RPI40 Examples: Greinbach Normal Form Not Greinbach Normal Form
  • Slide 41
  • Costas Buch - RPI41 Conversion to Greinbach Normal Form: Greinbach Normal Form
  • Slide 42
  • Costas Buch - RPI42 Theorem: For any context-free grammar (which doesnt produce ) there is an equivalent grammar in Greinbach Normal Form
  • Slide 43
  • Costas Buch - RPI43 Observations Greinbach normal forms are very good for parsing It is hard to find the Greinbach normal form of any context-free grammar
  • Slide 44
  • Costas Buch - RPI44 The CYK Parser
  • Slide 45
  • Costas Buch - RPI45 The CYK Membership Algorithm Input: Grammar in Chomsky Normal Form String Output: find if
  • Slide 46
  • Costas Buch - RPI46 The Algorithm Grammar : String : Input example:
  • Slide 47
  • Costas Buch - RPI47
  • Slide 48
  • Costas Buch - RPI48
  • Slide 49
  • Costas Buch - RPI49
  • Slide 50
  • Costas Buch - RPI50
  • Slide 51
  • Costas Buch - RPI51 Therefore: Time Complexity: The CYK algorithm can be easily converted to a parser (bottom up parser) Observation: