Download presentation
Presentation is loading. Please wait.
Published byMatilda Ada Bridges Modified over 9 years ago
1
Context-Free Languages Hinrich Schütze CIS, LMU, 2013-11-25 Slides based on RPI CSCI 2400 Thanks to Costas Busch
2
Take-away Definition context-free grammar Definition context-free language Derivation, sentential form, sentence Derivation trees Ambiguity Context-free grammars for natural language 2
3
Terminology For our purposes in this class: Context free grammar = Constituency grammar = Phrase structure grammar 3
4
4 Grammars Grammars express languages Example: the English language
5
5
6
6 A derivation of “the dog walks”:
7
7 A derivation of “a cat runs”:
8
8 Language of the grammar: L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }
9
9 Notation VariableTerminal Production Rules
10
10 Another Example Grammar: Derivation of sentence :
11
11 Language?
12
12 Grammar: Derivation of sentence :
13
13 Other derivations:
14
14 Language of the grammar
15
15 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
16
16 Example Grammar :
17
17 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence
18
18 We write: Instead of:
19
19 In general we write: If:
20
20 By default:
21
21 Example Grammar Derivations
22
22 Grammar Example Derivations
23
23 Another Grammar Example Grammar :
24
24 Language?
25
25 Grammar : Derivations:
26
26 More Derivations
27
27 Language of a Grammar For a grammar with start variable : String of terminals
28
28 Example For grammar : Since:
29
29 A Convenient Notation
30
30 Revisit first grammar A context-free grammar : A derivation:
31
31 A context-free grammar : Another derivation:
32
32 (((( )))) Describes parentheses:
33
33 A context-free grammar : Example
34
34 Language?
35
35 A context-free grammar : Another derivation:
36
36
37
37 A context-free grammar : Example
38
38 Language?
39
39 A context-free grammar : Two derivations:
40
40 Interpretation?
41
41 () ((( ))) (( )) Describes matched parentheses:
42
42 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
43
43
44
44 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
45
45 Derivation Order Leftmost derivation: Rightmost derivation:
46
46 Language?
47
47 Leftmost derivation: Rightmost derivation:
48
48 Language?
49
49 Derivation Trees
50
50
51
51
52
52
53
53
54
54 Derivation Tree
55
55 yield Derivation Tree
56
56 Partial Derivation Trees Partial derivation tree
57
57 Partial derivation tree
58
58 Partial derivation tree sentential form yield
59
59 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
60
60 Ambiguity
61
61 leftmost derivation
62
62 leftmost derivation
63
63 Two derivation trees
64
64 The grammar is ambiguous: stringhas two derivation trees
65
65 stringhas two leftmost derivations The grammar is ambiguous:
66
66 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees
67
67 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)
68
68 Why do we care about ambiguity? take
69
69
70
70
71
71 Correct result:
72
72 We want to remove ambiguity Ambiguity is bad for programming languages
73
73 We fix the ambiguous grammar: New non-ambiguous grammar:
74
74
75
75 Unique derivation tree
76
76 The grammar : is non-ambiguous: Every string has a unique derivation tree
77
77 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT Ambiguity?
78
78 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2
79
79 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
80
80 The string has two derivation trees
81
81 Ambiguity in natural language?
82
Take-away Definition context-free grammar Definition context-free language Derivation, sentential form, sentence Derivation trees Ambiguity Context-free grammars for natural language 82
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.