Theory of Computation
Theory of Computation
Context-Free Languages
2
Regular Languages
} {
n n
b a
} {
R
ww
3
Regular Languages
} {
n n
b a
} {
R
ww
Context-Free Languages
4
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
5
Context-Free Grammars
6
Example
A context-free grammar :
S
aSb S
aabb aaSbb aSb S
G
A derivation:
7
A context-free grammar :
S
aSb S
aaabbb aaaSbbb aaSbb aSb S
G
Another derivation:
8
S
aSb S
= ) (G L
(((( ))))
} 0 : { > n b a
n n
9
S
bSb S
aSa S
abba abSba aSa S
A context-free grammar : G
A derivation:
Example
10
S
bSb S
aSa S
abaaba abaSaba abSba aSa S
A context-free grammar : G
Another derivation:
11
S
bSb S
aSa S
= ) (G L
}*} , { : { b a w ww
R
e
12
S
SS S
aSb S
ab abS aSbS SS S
A context-free grammar : G
A derivation:
Example
13
S
SS S
aSb S
abab abaSb abS aSbS SS S
A context-free grammar : G
A derivation:
14
S
SS S
aSb S
} prefix any in
) ( ) ( and
), ( ) ( : {
v
v n v n
w n w n w
b a
b a
>
=
() ((( ))) (( ))
= ) (G L
15
Definition: Context-Free Grammars
Grammar
Productions of the form:
x A
x
is string of variables and terminals
) , , , ( P S T V G =
Variables Terminal
symbols
Start
variables
16
Definition: Context-Free Languages
A language is context-free
if and only if
there is a grammar with
L
G ) (G L L =
17
Derivation Order
AB S . 1
A
aaA A
. 3
. 2
B
Bb B
. 5
. 4
aab aaBb aaB aaAB AB S
5 4 3 2 1
Leftmost derivation:
aab aaAb Ab ABb AB S
3 2 5 4 1
Rightmost derivation:
18
| A B
bBb A
aAB S
Leftmost derivation:
abbbb abbbbB
abbBbbB abAbB abBbB aAB S
Rightmost derivation:
abbbb abbBbb
abAb abBb aA aAB S
19
Derivation Trees
20
AB S
AB S
| aaA A | Bb B
S
B A
21
AB S
| aaA A | Bb B
aaAB AB S
a a
A
S
B A
22
AB S
| aaA A | Bb B
aaABb aaAB AB S
S
B A
a a
A B b
23
AB S
| aaA A | Bb B
aaBb aaABb aaAB AB S
S
B A
a a
A B b
24
AB S
| aaA A | Bb B
aab aaBb aaABb aaAB AB S
S
B A
a a
A B b
Derivation Tree
25
aab aaBb aaABb aaAB AB S
yield
aab
b aa
=
S
B A
a a
A B b
Derivation Tree
AB S
| aaA A | Bb B
26
Partial Derivation Trees
AB S
S
B A
Partial derivation tree
AB S | aaA A
| Bb B
27
aaAB AB S
S
B A
a a
A
Partial derivation tree
28
aaAB AB S
S
B A
a a
A
Partial derivation tree
sentential
form
yield
aaAB
29
aab aaBb aaB aaAB AB S
aab aaAb Ab ABb AB S
S
B A
a a
A B b
Same derivation tree
Sometimes, derivation order doesnt matter
Leftmost:
Rightmost:
30
Ambiguity
31
a E E E E E E | ) ( | | - +
a a a - +
E
E E
E E
+
a
a
a
-
a a a E a a
E E a E a E E E
* + - +
- + + +
leftmost derivation
32
a E E E E E E | ) ( | | - +
a a a - +
E
E E
+
a
a
-
E E
a
a a a E a a
E E a E E E E E E
- + - +
- + - + -
leftmost derivation
33
a E E E E E E | ) ( | | - +
a a a - +
E
E E
+
a
a
-
E E
a
E
E E
E E
+
a
a
a
-
Two derivation trees
34
The grammar
a E E E E E E | ) ( | | - +
is ambiguous:
E
E E
+
a
a
-
E E
a
E
E E
E E
+
a
a
a
-
string a a a - + has two derivation trees
35
string a a a - + has two leftmost derivations
a a a E a a
E E a E E E E E E
- + - +
- + - + -
a a a E a a
E E a E a E E E
* + - +
- + + +
The grammar
a E E E E E E | ) ( | | - +
is ambiguous:
36
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
G
) (G L we
37
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
G
) (G L we
(or rightmost)
38
Why do we care about ambiguity?
E
E E
+
a
a
-
E E
a
E
E E
E E
+
a
a
a
-
a a a - +
take 2 = a
39
E
E E
+
-
E E
E
E E
E E
+
-
2 2 2 - +
2
2 2 2 2
2
40
E
E E
+
-
E E
E
E E
E E
+
-
6 2 2 2 = - +
2
2 2 2 2
2
8 2 2 2 = - +
4
2 2
2
6
2 2
2 4
8
41
E
E E
E E
+
-
6 2 2 2 = - +
2
2 2
4
2 2
2
6
Correct result:
42
We want to remove ambiguity
Ambiguity is bad for programming languages
43
We fix the ambiguous grammar:
a E E E E E E | ) ( | | - +
New non-ambiguous grammar:
a F
E F
F T
F T T
T E
T E E
+
) (
44
a F
E F
F T
F T T
T E
T E E
+
) (
a a a F a a F F a
F T a T a T F T T T E E
- + - + - +
- + + + + +
E
E T +
T
-
F
F
a
T
F
a
a
a a a - +
45
E
E T +
T
-
F
F
a
T
F
a
a
a a a - +
Unique derivation tree
46
The grammar :
a F
E F
F T
F T T
T E
T E E
+
) (
is non-ambiguous:
Every string has
a unique derivation tree
G
) (G L we
47
Inherent Ambiguity
Some context free languages
have only ambiguous grammars
Example:
} { } {
m m n m n n
c b a c b a L =
|
|
1 1
aAb A
A c S S
|
|
2 2
bBc B
B aS S
2 1
| S S S
48
The string
n n n
c b a
has two derivation trees
S
1
S
S
2
S
1
S
c
2
S
a