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