1 Context-Free Languages
2 Regular Languages
3 Context-Free Languages
4 Pushdown Automata Context-Free Grammars stack automaton
5 Context-Free Grammars
6 Grammars Grammars express languages Example: the English language
7
8 A derivation of “the dog walks”:
9 A derivation of “a cat runs”:
10 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” }
11 Notation VariableTerminal Production Rules
12 Another Example Grammar: Derivation of sentence :
13 Grammar: Derivation of sentence :
14 Other derivations:
15 Language of the grammar
16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules
17 Example Grammar :
18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence
19 We write: Instead of:
20 In general we write: If:
21 By default:
22 Example Grammar Derivations
23 Grammar Example Derivations
24 Another Grammar Example Grammar : Derivations:
25 More Derivations
26 Language of a Grammar For a grammar with start variable : String of terminals
27 Example For grammar : Since:
28 A Convenient Notation
29 Example A context-free grammar : A derivation:
30 A context-free grammar : Another derivation:
31 (((( )))) Describes parentheses:
32 A context-free grammar : A derivation: Example
33 A context-free grammar : Another derivation:
34
35 A context-free grammar : A derivation: Example
36 A context-free grammar : A derivation:
37 () ((( ))) (( )) Describes matched parentheses:
38 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable
39 Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with
40 Derivation Order Leftmost derivation: Rightmost derivation:
41 Leftmost derivation: Rightmost derivation:
42 Derivation Trees
43
44
45
46
47 Derivation Tree
48 yield Derivation Tree
49 Partial Derivation Trees Partial derivation tree
50 Partial derivation tree
51 Partial derivation tree sentential form yield
52 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
53 Ambiguity
54 leftmost derivation
55 Rightmost derivation
56 Two derivation trees
57 The grammar is ambiguous: stringhas two derivation trees
58 stringhas two leftmost derivations The grammar is ambiguous:
59 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees
60 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost)
61 Why do we care about ambiguity? take
62
63
64 Correct result:
65 We want to remove ambiguity Ambiguity is bad for programming languages