Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton 4
Context-Free Grammars 5
A context-free grammar G: 6
A context-free grammar G: 8
9
A context-free grammar G: 10
Definition of Context-Free Grammars Any grammar G=(V, T, S, P) V: variables T: terminal symbols S: start variable P: production rules of the form : string of variables and terminals 12
Definition of Context-Free Languages A language L is context-free if and only if there is a grammar G with L = L(G) 13
Derivation Order Leftmost derivation: Rightmost derivation: 14
Derivation Trees 15
16
17
18
19
Derivation Tree 20
sentence Derivation Tree yield 21
Partial Derivation Trees Partial derivation tree 22
sentential form Partial derivation tree yield 23
Often, derivation order doesn’t matter Same Derivation Tree 24
Ambiguity 25
26
27
28
The grammar is ambiguous: string has two derivation trees 29
The grammar is ambiguous: string has two leftmost derivations 30
In general: A context-free grammar if some string is ambiguous has: two or more derivation trees 31
In other words: A context-free grammar if some string is ambiguous has: two or more leftmost derivations rightmost 32
Why do we care about ambiguity? take 33
34
35
The right solution: 36
Therefore: Ambiguity is bad for programming languages We want to remove the ambiguity 37
We fix the ambiguous grammar: New grammar: 38
39
Unique derivation tree 40
The grammar Is not ambiguous 41
Inherent Ambiguity Some context free languages have only ambiguous grammars Example: 42