Download presentation
Presentation is loading. Please wait.
1
1 Simplifications of Context-Free Grammars
2
2 A Substitution Rule substitute B equivalent grammar
3
3 In general: substitute B equivalent grammar
4
4 Useless Productions
5
5 never terminates...
6
6 Useless Production
7
7 Another grammar:
8
8 Not reachable from S Useless Production
9
9 In general: If Then is uselessOtherwise is useful
10
10 Removing Useless Productions Example Grammar
11
11 First:find all variables that produce strings with only terminals Round 1: Round 2:
12
12 Keep only the variables that produce terminal symbols
13
13 Second: Find all variables reachable from S Dependency Graph not reachable
14
14 Keep only the variables reachable from S Final Grammar
15
15 Nullable Variables Nullable Variable:
16
16 Removing Nullable Variables Example Grammar: Nullable variable
17
17 Substitute Final Grammar
18
18 Unit-Productions Unit Production:
19
19 Removing Unit Productions Observation: Is removed immediately
20
20 Example Grammar:
21
21 substitute
22
22
23
23 substitute
24
24 Remove repeated productions Final grammar
25
25 Removing All 1. Remove Nullable Variables 2. Remove Unit-Productions 3. Remove Useless Variables
26
26 Normal Forms for Context-free Grammars
27
27 Chomsky Normal Form All productions have form: variable OR terminal
28
28 Examples: Chomsky Normal Form NOTChomsky Normal Form
29
29 Convertion to Chomsky Normal Form Example: Chomsky Normal Form NOT
30
30 Introduce variables for terminals:
31
31 Introduce intermediate variables:
32
32 Final grammar in Chomsky Normal Form: Initial grammar
33
33 From any context-free grammar Not in Chomsky Normal Form We can obtain: An equivalent grammar in Chomsky Normal Form In general:
34
34 The Procedure: Remove nullable variables unit productions
35
35 For every symbol : Add new variable In productions, replace with Add production
36
36 Replace any production: with: New variables:
37
37 Theorem: For any context-free grammar there is an equivalent grammar in Chomsky Normal Form
38
38 Greinbach Normal Form All rules have form: symbol variables
39
39 Examples: Greinbach Normal Form Greinbach Normal Form NOT
40
40 Conversion to Greinbach Normal Form Greinbach Normal Form
41
41 Theorem: For any context-free grammar there is an equivalent grammar in Greinbach Normal Form
42
42 An Application of Chomsky Normal Forms
43
43 The CYK Membership Algorithm Inputs: Grammar in Chomsky Normal Form String w Output: find if
44
44 Algorithm: Input Examples Grammar : String :
45
45
46
46
47
47
48
48
49
49 Therefore: Time Complexity: It can be easily converted to a parser
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.