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
6Grammars
- Grammars express languages
- Example the English language
7 8- A derivation of the dog walks
9- A derivation of a cat runs
10L a cat runs, a cat walks,
the cat runs, the cat walks,
a dog runs, a dog walks, the
dog runs, the dog walks
11Notation
Production Rules
Variable
Terminal
12Another Example
- Grammar
- Derivation of sentence
13- Grammar
- Derivation of sentence
14 15 16More Notation
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
17Example
18More Notation
- Sentential Form
- A sentence that contains
- variables and terminals
- Example
Sentential Forms
sentence
19 20 21 22Example
Grammar
Derivations
23Example
Grammar
Derivations
24Another Grammar Example
Derivations
25More Derivations
26Language of a Grammar
- For a grammar
- with start variable
String of terminals
27Example
Since
28A Convenient Notation
29Example
A context-free grammar
A derivation
30 A context-free grammar
Another derivation
31(((( ))))
Describes parentheses
32Example
A context-free grammar
A derivation
33A context-free grammar
Another derivation
34(No Transcript)
35Example
A context-free grammar
A derivation
36A context-free grammar
A derivation
37Describes matched parentheses
() ((( ))) (( ))
38Definition Context-Free Grammars
Grammar
Variables
Terminal symbols
Start variable
Productions of the form
String of variables and terminals
Variable
39(No Transcript)
40Definition Context-Free Languages
- A language is context-free
- if and only if
- there is a context-free grammar
- with
41Derivation Order
42(No Transcript)
43Derivation Trees
44 45(No Transcript)
46(No Transcript)
47(No Transcript)
48Derivation Tree
49Derivation Tree
yield
50Partial Derivation Trees
Partial derivation tree
51Partial derivation tree
52sentential form
Partial derivation tree
yield
53Sometimes, derivation order doesnt matter
Leftmost
Rightmost
Same derivation tree
54Ambiguity
55leftmost derivation
56leftmost derivation
57(No Transcript)
58The grammar
is ambiguous
string
has two derivation trees
59The grammar
is ambiguous
string
has two leftmost derivations
60Definition
A context-free grammar is ambiguous if
some string has two or
more derivation trees
61In other words
A context-free grammar is ambiguous if
some string has two or
more leftmost derivations
(or rightmost)
62Why do we care about ambiguity?
take
63(No Transcript)
64(No Transcript)
65Correct result
66- Ambiguity is bad for programming languages
- We want to remove ambiguity
67We fix the ambiguous grammar
New non-ambiguous grammar
68(No Transcript)
69Unique derivation tree
70The grammar
is non-ambiguous
Every string has a unique
derivation tree
71Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
72If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
if
expr1
then
STMT
else
if
expr2
then
stmt1
stmt2
IF_STMT
if
expr1
then
else
STMT
stmt2
if
expr2
then
stmt1
73Inherent Ambiguity
- Some context free languages
- have only ambiguous grammars
Example
74The string
has two derivation trees