Download presentation
Presentation is loading. Please wait.
1
1 Normal Forms for Context-free Grammars
2
2 Chomsky Normal Form All productions have form: variable and terminal
3
3 Examples: Not Chomsky Normal Form Chomsky Normal Form
4
4 Convertion to Chomsky Normal Form Example: Not Chomsky Normal Form
5
5 Introduce variables for terminals:
6
6 Introduce intermediate variable:
7
7
8
8 Final grammar in Chomsky Normal Form: Initial grammar
9
9 From any context-free grammar not in Chomsky Normal Form we can obtain: An equivalent grammar in Chomsky Normal Form In general:
10
10 The Procedure First remove: Nullable variables Unit productions
11
11 For every symbol : In productions: replace with Add production New variable:
12
12 Replace any production with New intermediate variables:
13
13 Theorem: For any context-free grammar there is an equivalent grammar in Chomsky Normal Form
14
14 Observations Chomsky normal forms are good for parsing and proving theorems It is very easy to find the Chomsky normal form of any context-free grammar
15
15 Greinbach Normal Form All productions have form: symbolvariables
16
16 Examples: Greinbach Normal Form Not Greinbach Normal Form
17
17 Conversion to Greinbach Normal Form: Greinbach Normal Form
18
18 Theorem: For any context-free grammar there is an equivalent grammar in Greinbach Normal Form
19
19 Observations Greinbach normal forms are very good for parsing It is hard to find the Greinbach normal form of any context-free grammar
20
20 An Application of Chomsky Normal Forms
21
21 The CYK Membership Algorithm Input: Grammar in Chomsky Normal Form String Output: find if
22
22 The Algorithm Grammar : String : Input example:
23
23
24
24
25
25
26
26
27
27 Therefore: Time Complexity: The CYK algorithm can be easily converted to a parser Observation:
28
28 Pushdown Automata PDAs
29
29 Pushdown Automaton -- PDA Input String Stack States
30
30 Initial Stack Symbol Stack bottom special symbol
31
31 The States Input symbol Pop symbol Push symbol
32
32 top input stack Replace
33
33 Push top input stack
34
34 Pop top input stack
35
35 No Change top input stack
36
36 Non-Determinism These are allowed transitions in a Non-deterministic PDA (NPDA)
37
37 NPDA: Non-Deterministic PDA Example:
38
38 Execution Example: Input current state Time 0 Stack
39
39 Input Time 1 Stack
40
40 Input Stack Time 2
41
41 Input Stack Time 3
42
42 Input Stack Time 4
43
43 Input Stack Time 5
44
44 Input Stack Time 6
45
45 Input Stack Time 7
46
46 Input Time 8 accept Stack
47
47 A string is accepted if there is a computation such that: All the input is consumed The last state is a final state At the end of the computation, we do not care about the stack contents
48
48 The input string is accepted by the NPDA:
49
49 is the language accepted by the NPDA: In general,
50
50 Another NPDA example NPDA
51
51 Execution Example: Input Time 0 Stack
52
52 Input Time 1 Stack
53
53 Input Time 2 Stack
54
54 Input Time 3 Stack Guess the middle of string
55
55 Input Time 4 Stack
56
56 Input Time 5 Stack
57
57 Input Time 6 Stack accept
58
58 Rejection Example: Input Time 0 Stack
59
59 Input Time 1 Stack
60
60 Input Time 2 Stack
61
61 Input Time 3 Stack Guess the middle of string
62
62 Input Time 4 Stack
63
63 Input Time 5 Stack There is no possible transition. Input is not consumed
64
64 Another computation on same string: Input Time 0 Stack
65
65 Input Time 1 Stack
66
66 Input Time 2 Stack
67
67 Input Time 3 Stack
68
68 Input Time 4 Stack
69
69 Input Time 5 Stack No final state is reached
70
70 There is no computation that accepts string
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.