languages and finite automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · it...
TRANSCRIPT
![Page 1: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/1.jpg)
1
Context-Free Languages
![Page 2: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/2.jpg)
2
Regular Languages
}{ nnba }{ Rww
![Page 3: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/3.jpg)
3
Regular Languages
}{ nnba }{ Rww
Context-Free Languages
![Page 4: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/4.jpg)
4
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
![Page 5: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/5.jpg)
5
Context-Free Grammars
![Page 6: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/6.jpg)
6
Example
A context-free grammar :
S
aSbS
aabbaaSbbaSbS
G
A derivation:
![Page 7: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/7.jpg)
7
A context-free grammar :
S
aSbS
aaabbbaaaSbbbaaSbbaSbS
G
Another derivation:
![Page 8: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/8.jpg)
8
S
aSbS
)(GL
(((( ))))
}0:{ nba nn
![Page 9: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/9.jpg)
9
S
bSbS
aSaS
abbaabSbaaSaS
A context-free grammar :G
A derivation:
Example
![Page 10: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/10.jpg)
10
S
bSbS
aSaS
abaabaabaSabaabSbaaSaS
A context-free grammar :G
Another derivation:
![Page 11: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/11.jpg)
11
S
bSbS
aSaS
)(GL }*},{:{ bawwwR
![Page 12: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/12.jpg)
12
S
SSS
aSbS
ababSaSbSSSS
A context-free grammar :G
A derivation:
Example
![Page 13: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/13.jpg)
13
S
SSS
aSbS
abababaSbabSaSbSSSS
A context-free grammar :G
A derivation:
![Page 14: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/14.jpg)
14
S
SSS
aSbS
}prefixanyin
)()( and
),()(:{
v
vnvn
wnwnw
ba
ba
() ((( ))) (( ))
)(GL
![Page 15: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/15.jpg)
15
Definition: Context-Free Grammars
Grammar
Productions of the form:
xA
x is string of variables and terminals
),,,( PSTVG
Variables Terminal
symbols
Start
variable
![Page 16: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/16.jpg)
16
Definition: Context-Free Languages
A language is context-free
if and only if
there is a grammar with
L
G )(GLL
![Page 17: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/17.jpg)
17
Derivation Order
ABS .1
A
aaAA
.3
.2
B
BbB
.5
.4
aabaaBbaaBaaABABS54321
Leftmost derivation:
aabaaAbAbABbABS32541
Rightmost derivation:
![Page 18: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/18.jpg)
18
|AB
bBbA
aABS
Leftmost derivation:
abbbbabbbbB
abbBbbBabAbBabBbBaABS
Rightmost derivation:
abbbbabbBbb
abAbabBbaAaABS
![Page 19: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/19.jpg)
19
Derivation Trees
![Page 20: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/20.jpg)
20
ABS
ABS |aaAA |BbB
S
BA
![Page 21: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/21.jpg)
21
ABS |aaAA |BbB
aaABABS
a a A
S
BA
![Page 22: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/22.jpg)
22
ABS |aaAA |BbB
aaABbaaABABS
S
BA
a a A B b
![Page 23: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/23.jpg)
23
ABS |aaAA |BbB
aaBbaaABbaaABABS
S
BA
a a A B b
![Page 24: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/24.jpg)
24
ABS |aaAA |BbB
aabaaBbaaABbaaABABS
S
BA
a a A B b
Derivation Tree
![Page 25: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/25.jpg)
25
aabaaBbaaABbaaABABS
yield
aab
baa
S
BA
a a A B b
Derivation Tree
ABS |aaAA |BbB
![Page 26: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/26.jpg)
26
Partial Derivation Trees
ABS
S
BA
Partial derivation tree
ABS |aaAA |BbB
![Page 27: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/27.jpg)
27
aaABABS
S
BA
a a A
Partial derivation tree
![Page 28: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/28.jpg)
28
aaABABS
S
BA
a a A
Partial derivation tree
sentential
form
yield
aaAB
![Page 29: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/29.jpg)
29
aabaaBbaaBaaABABS
aabaaAbAbABbABS
S
BA
a a A B b
Same derivation tree
Sometimes, derivation order doesn’t matter
Leftmost:
Rightmost:
![Page 30: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/30.jpg)
30
Ambiguity
![Page 31: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/31.jpg)
31
aEEEEEE |)(||
aaa
E
EE
EE
a
a a
aaaEaa
EEaEaEEE
*
leftmost derivation
![Page 32: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/32.jpg)
32
aEEEEEE |)(||
aaa
E
EE
a a
EE a
aaaEaa
EEaEEEEEE
leftmost derivation
![Page 33: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/33.jpg)
33
aEEEEEE |)(||
aaa
E
EE
a a
EE a
E
EE
EE
a
a a
Two derivation trees
![Page 34: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/34.jpg)
34
The grammar aEEEEEE |)(||
is ambiguous:
E
EE
a a
EE a
E
EE
EE
a
a a
string aaa has two derivation trees
![Page 35: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/35.jpg)
35
string aaa has two leftmost derivations
aaaEaa
EEaEEEEEE
aaaEaa
EEaEaEEE
*
The grammar aEEEEEE |)(||
is ambiguous:
![Page 36: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/36.jpg)
36
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
G
)(GLw
![Page 37: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/37.jpg)
37
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
G
)(GLw
(or rightmost)
![Page 38: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/38.jpg)
38
Why do we care about ambiguity?
E
EE
a a
EE a
E
EE
EE
a
a a
aaa
take 2a
![Page 39: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/39.jpg)
39
E
EE
EE
E
EE
EE
222
2
2 2 2 2
2
![Page 40: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/40.jpg)
40
E
EE
EE
E
EE
EE
6222
2
2 2 2 2
2
8222
4
2 2
2
6
2 2
24
8
![Page 41: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/41.jpg)
41
E
EE
EE
6222
2
2 2
4
2 2
2
6
Correct result:
![Page 42: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/42.jpg)
42
• We want to remove ambiguity
• Ambiguity is bad for programming languages
![Page 43: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/43.jpg)
43
We fix the ambiguous grammar:
aEEEEEE |)(||
New non-ambiguous grammar:
aF
EF
FT
FTT
TE
TEE
)(
![Page 44: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/44.jpg)
44
aF
EF
FT
FTT
TE
TEE
)(
aaaFaaFFa
FTaTaTFTTTEE
E
E T
T F
F
a
T
F
a
a
aaa
![Page 45: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/45.jpg)
45
E
E T
T F
F
a
T
F
a
a
aaa
Unique derivation tree
![Page 46: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/46.jpg)
46
The grammar :
aF
EF
FT
FTT
TE
TEE
)(
is non-ambiguous:
Every string has
a unique derivation tree
G
)(GLw
![Page 47: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/47.jpg)
47
Inherent Ambiguity
Some context free languages
have only ambiguous grammars
Example: }{}{ mmnmnn cbacbaL
|
|11
aAbA
AcSS
|
|22
bBcB
BaSS
21 | SSS
![Page 48: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/48.jpg)
48
The string nnn cba
has two derivation trees
S
1S
S
2S
1S c2Sa
![Page 49: Languages and Finite Automata › ~ryan › cse4083 › busch › class08.pdf · 2017-02-10 · It does not, of course, follow from this that L is inherently ambiguous as there might](https://reader035.vdocument.in/reader035/viewer/2022070805/5f0395397e708231d409c421/html5/thumbnails/49.jpg)
It does not, of course, follow from this that L is inherently ambiguous as there might exist some other unambiguous grammars for it. But in some way L1 and L2 have conflicting requirements. A rigorous argument, though, is quite technical. One proof can be found in Harrison 1978.
Linz, 6th, page 149
49