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