Page 1
Context-Free Languages
Hinrich Schütze
CIS, LMU, 2013-11-25
Slides based on RPI CSCI 2400
Thanks to Costas Busch
Page 2
Take-awayDefinition context-free grammarDefinition context-free languageDerivation, sentential form, sentenceDerivation treesAmbiguityContext-free grammars for natural
language
2
Page 3
TerminologyFor our purposes in this class:Context free grammar=Constituency grammar=Phrase structure grammar
3
Page 4
4
GrammarsGrammars express languages
Example: the English language
Page 6
6
A derivation of “the dog walks”:
Page 7
7
A derivation of “a cat runs”:
Page 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” }
Page 9
9
Notation
Variable Terminal
Production Rules
Page 10
10
Another ExampleGrammar:
Derivation of sentence :
Page 12
12
Grammar:
Derivation of sentence :
Page 13
13
Other derivations:
Page 14
14
Language of the grammar
Page 15
15
More Notation
Grammar
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
Page 16
16
Example
Grammar :
Page 17
17
More NotationSentential Form: A sentence that contains variables and terminals
Example:
Sentential Forms sentence
Page 18
18
We write:
Instead of:
Page 19
19
In general we write:
If:
Page 21
21
Example Grammar Derivations
Page 22
22
Grammar
Example
Derivations
Page 23
23
Another Grammar ExampleGrammar :
Page 25
25
Grammar :
Derivations:
Page 26
26
More Derivations
Page 27
27
Language of a Grammar
For a grammar with start variable :
String of terminals
Page 28
28
ExampleFor grammar :
Since:
Page 29
29
A Convenient Notation
Page 30
30
Revisit first grammar A context-free grammar :
A derivation:
Page 31
31
A context-free grammar :
Another derivation:
Page 32
32
(((( ))))Describes parentheses:
Page 33
33
A context-free grammar :
Example
Page 35
35
A context-free grammar :
Another derivation:
Page 37
37
A context-free grammar :
Example
Page 39
39
A context-free grammar :
Two derivations:
Page 40
40
Interpretation?
Page 41
41
() ((( ))) (( ))
Describes matched parentheses:
Page 42
42
Definition: Context-Free Grammars
Grammar
Productions of the form:
String of variables and terminals
Variables Terminalsymbols
Startvariable
Variable
Page 44
44
Definition: Context-Free Languages
A language is context-free
if and only if
there is a context-free grammar with
Page 45
45
Derivation Order
Leftmost derivation:
Rightmost derivation:
Page 47
47
Leftmost derivation:
Rightmost derivation:
Page 49
49
Derivation Trees
Page 54
54
Derivation Tree
Page 55
55
yield
Derivation Tree
Page 56
56
Partial Derivation Trees
Partial derivation tree
Page 57
57
Partial derivation tree
Page 58
58
Partial derivation tree
sententialform
yield
Page 59
59
Same derivation tree
Sometimes, derivation order doesn’t matter
Leftmost:
Rightmost:
Page 61
61
leftmost derivation
Page 62
62
leftmost derivation
Page 63
63
Two derivation trees
Page 64
64
The grammar
is ambiguous:
string has two derivation trees
Page 65
65
string has two leftmost derivations
The grammar
is ambiguous:
Page 66
66
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
Page 67
67
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
(or rightmost)
Page 68
68
Why do we care about ambiguity?
take
Page 71
71
Correct result:
Page 72
72
• We want to remove ambiguity
• Ambiguity is bad for programming languages
Page 73
73
We fix the ambiguous grammar:
New non-ambiguous grammar:
Page 75
75
Unique derivation tree
Page 76
76
The grammar :
is non-ambiguous:
Every string hasa unique derivation tree
Page 77
77
Another Ambiguous Grammar
IF_STMT if EXPR then STMT
if EXPR then STMT else STMT
Ambiguity?
Page 78
78
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
expr1 then
elseif expr2 then
STMT
stmt1
if
IF_STMT
expr1 then else
if expr2 then
STMT stmt2if
stmt1
stmt2
Page 79
79
Inherent Ambiguity
Some context free languageshave only ambiguous grammars
Example:
Page 80
80
The string
has two derivation trees
Page 81
81
Ambiguity in natural language?
Page 82
Take-awayDefinition context-free grammarDefinition context-free languageDerivation, sentential form, sentenceDerivation treesAmbiguityContext-free grammars for natural
language
82