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