1 Context-Free Languages
2 Regular Languages
3 Context-Free Languages
4 Pushdown Automata Context-Free Grammars stack automaton
5 Context-Free Grammars
6 Example A context-free grammar : A derivation:
7 A context-free grammar : Another derivation:
8 (((( ))))
9 A context-free grammar : A derivation: Example
10 A context-free grammar : Another derivation:
11
12 A context-free grammar : A derivation: Example
13 A context-free grammar : A derivation:
14 () ((( ))) (( ))
15 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables
16 Definition: Context-Free Languages A language is context-free if and only if there is a grammar with
17 Derivation Order Leftmost derivation: Rightmost derivation:
18 Leftmost derivation: Rightmost derivation:
19 Derivation Trees
20
21
22
23
24 Derivation Tree
25 yield Derivation Tree
26 Partial Derivation Trees Partial derivation tree
27 Partial derivation tree
28 Partial derivation tree sentential form yield
29 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
30 Ambiguity
31 leftmost derivation
32 leftmost derivation
33 Two derivation trees
34 The grammar is ambiguous: stringhas two derivation trees
35 stringhas two leftmost derivations The grammar is ambiguous:
36 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees
37 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)
38 Why do we care about ambiguity? take
39
40
41 Correct result:
42 We want to remove ambiguity Ambiguity is bad for programming languages
43 We fix the ambiguous grammar: New non-ambiguous grammar:
44
45 Unique derivation tree
46 The grammar : is non-ambiguous: Every string has a unique derivation tree
47 Inherent Ambiguity Some context free languages have only ambiguous grammars Example:
48 The string has two derivation trees