Formal Languages ContextFree Languages Regular Languages 2 ContextFree


















































































- Slides: 82
Formal Languages Context-Free Languages
Regular Languages 2
Context-Free Languages Regular Languages 3
Context-Free Languages Context-Free Grammars Pushdown Automata stack automaton 4
Context-Free Grammars 5
Grammars express languages Example: the English language 6
7
A derivation of “the dog walks”: 8
A derivation of “a cat runs”: 9
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” } 10
Notation Production Rules Variable Terminal 11
Another Example Grammar: Derivation of sentence : 12
Language? 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 Forms sentence 19
We write: Instead of: 20
In general we write: If: 21
By default: 22
Example Grammar Derivations 23
Example Grammar Derivations 24
Another Grammar Example Grammar : Derivations: 25
Language? 26
More Derivations 27
Language of a Grammar For a grammar with start variable : String of terminals 28
Example For grammar : Since: 29
A Convenient Notation 30
Example A context-free grammar : A derivation: 31
A context-free grammar : Another derivation: 32
Describes parentheses: (((( )))) 33
Example A context-free grammar : A derivation: 34
Language? 35
A context-free grammar : Another derivation: 36
37
Example A context-free grammar : A derivation: 38
Language? 39
A context-free grammar : A derivation: 40
Interpretation? 41
Describes matched parentheses: () ((( ))) (( )) 42
Definition: Context-Free Grammars Grammar Variables Terminal symbols Start variable Productions of the form: Variable String of variables and terminals 43
44
Definition: Context-Free Languages A language is context-free if and only if there is a context-free grammar with 45
Derivation Order Leftmost derivation: Rightmost derivation: 46
Language? 47
Leftmost derivation: Rightmost derivation: 48
Language? 49
Derivation Trees 50
51
52
53
54
Derivation Tree 55
Derivation Tree yield 56
Partial Derivation Trees Partial derivation tree 57
Partial derivation tree 58
sentential form Partial derivation tree yield 59
Sometimes, derivation order doesn’t matter Leftmost: Rightmost: Same derivation tree 60
Ambiguity 61
leftmost derivation 62
leftmost derivation 63
Two derivation trees 64
The grammar is ambiguous: string has two derivation trees 65
The grammar is ambiguous: string has two leftmost derivations 66
Definition: A context-free grammar if some string is ambiguous has: two or more derivation trees 67
In other words: A context-free grammar if some string is ambiguous has: two or more leftmost derivations (or rightmost) 68
Why do we care about ambiguity? take 69
70
71
Correct result: 72
• Ambiguity is bad for programming languages • We want to remove ambiguity 73
We fix the ambiguous grammar: New non-ambiguous grammar: 74
75
Unique derivation tree 76
The grammar : is non-ambiguous: Every string has a unique derivation tree 77
Another Ambiguous Grammar IF_STMT if EXPR then STMT else STMT 78
If expr 1 then if expr 2 then stmt 1 else stmt 2 IF_STMT if expr 1 if then expr 2 STMT then stmt 1 else STMT else stmt 2 IF_STMT if expr 1 if then expr 2 then stmt 1 79
Inherent Ambiguity Some context free languages have only ambiguous grammars Example: 80
The string has two derivation trees 81
Ambiguity in natural language? 82