1 context-free languages. 2 regular languages 3 context-free languages
Post on 21-Dec-2015
233 views
TRANSCRIPT
1
Context-Free Languages
2
Regular Languages
}{ nnba }{ Rww
3
Regular Languages
}{ nnba }{ Rww
Context-Free Languages
4
Context-Free Languages
PushdownAutomata
Context-FreeGrammars
stack
automaton
5
Context-Free Grammars
6
A context-free grammar G:
S
aSbS
aaabbbaaaSbbbaaSbbaSbS
7
S
aSbS
}0:{)( nbaGL nn
(((( ))))
8
A context-free grammar G:
S
bSbS
aSaS
abaabaabaSabaabSbaaSaS
9
S
bSbS
aSaS
}{)( RwwGL
10
A context-free grammar G:
S
SSS
aSbS
abababaSb
abSaSbSaSbSSSS
11
S
SSS
aSbS
}prefixany
),()(
),()(:{)(
v
vnvn
wnwnwGL
ba
ba
() ((( ))) (( ))
12
Definition of Context-Free Grammars
Any grammar G=(V,T,S,P)
P: production rules of the form
V: variablesT: terminal symbolsS: start variable
xA
x: string of variables and terminals
13
Definition of Context-Free Languages
A language L is context-free
if and only if
there is a grammar G with L = L(G)
14
Derivation Order ABS .1
aabaaBbaaBaaABABS54321
aabaaAbAbABbABS32541
A
aaAA
.3
.2
B
BbB
.5
.4
Leftmost derivation:
Rightmost derivation:
15
Derivation Trees
16
ABS |aaAA |BbB
ABS
S
BA
17
ABS |aaAA |BbB
aaABABS
S
BA
a a A
18
ABS |aaAA |BbB
aaABbaaABABS
S
BA
a a A B b
19
ABS |aaAA |BbB
aaBbaaABbaaABABS
S
BA
a a A B b
20
ABS |aaAA |BbB
aabaaBbaaABbaaABABS
S
BA
a a A B b
Derivation Tree
21
S
BA
a a A B b
aabaaBbaaABbaaABABS
yield
aab
baa
sentence
Derivation Tree
22
Partial Derivation Trees
ABS
S
BA
Partial derivation tree
ABS |aaAA |BbB
23
aaABABS
S
BA
a a A
Partial derivation tree
sententialform
yield
aaAB
24
aabaaBbaaBaaABABS aabaaAbAbABbABS
S
BA
a a A B b
Same Derivation Tree
Often, derivation order doesn’t matter
25
Ambiguity
26
aEEEEEE |)(||
aaa
E
EE
EE
a
a a
aaaEaa
EEaEaEEE
*
27
aEEEEEE |)(||
aaa
E
EE
a a
EE a
aaaEaa
EEaEEEEEE
28
E
EE
a a
EE a
E
EE
EE
a
a a
aEEEEEE |)(||
aaa
29
The grammar
aEEEEEE |)(||
is ambiguous:
string aaa has two derivation trees
30
The grammar aEEEEEE |)(||
is ambiguous:
string aaa has two leftmost derivations
aaaEaa
EEaEEEEEE
aaaEaa
EEaEaEEE
*
31
In general:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
G
)(GLw
32
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
G
)(GLw
rightmost
33
Why do we care about ambiguity?
E
EE
a a
EE a
E
EE
EE
a
a a
aaa
take 2a
34
E
EE
EE
E
EE
EE
222
2
2 2 2 2
2
35
E
EE
EE
E
EE
EE
6222
2
2 2 2 2
2
8222
4
2 2
2
6
2 2
24
8
36
E
EE
EE
6222
2
2 2
4
2 2
2
6
The right solution:
37
We want to remove the ambiguity
Ambiguity is bad for programming languages
Therefore:
38
We fix the ambiguous grammar:
aEEEEEE |)(||
New grammar:
aF
EF
FT
FTT
TE
TEE
)(
39
aF
EF
FT
FTT
TE
TEE
)(
aaaFaaFFa
FTaTaTFTTTEE
E
E T
T F
F
a
T
F
a
a
aaa
40
E
E T
T F
F
a
T
F
a
a
aaa
Unique derivation tree
41
The grammar
aF
EF
FT
FTT
TE
TEE
)(
Is not ambiguous
42
Inherent Ambiguity
Some context free languageshave only ambiguous grammars
Example: }{}{ mmnmnn cbacbaL
||11
aAbA
AcSS
||22
bBcB
BaSS
21 | SSS
43
The string nnn cba
has two derivation trees
S
1S
S
2S
1S c 2Sa