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