1 Context-Free Languages
Jan 21, 2016
1
Context-Free Languages
2
Regular Languages
}0:{ nba nn }{ Rww
**ba *)( ba
3
Regular Languages
}{ nnba }{ Rww
Context-Free Languages
4
Context-Free Languages
PushdownAutomata
Context-FreeGrammars
stack
automaton
5
Context-Free Grammars
6
GrammarsGrammars express languages
Example: the English language
verbpredicate
nounarticlephrasenoun
predicatephrasenounsentence
_
_
7
walksverb
runsverb
dognoun
catnoun
thearticle
aarticle
8
A derivation of “the dog walks”:
walksdogthe
verbdogthe
verbnounthe
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
9
A derivation of “a cat runs”:
runscata
verbcata
verbnouna
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
10
Language of the grammar:
L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }
11
Notation
dognoun
catnoun
Variable Terminal
Production Rules
12
Another ExampleGrammar:
Derivation of sentence :
S
aSbS
abaSbS
ab
aSbS S
13
aabbaaSbbaSbS
aSbS S
aabb
S
aSbSGrammar:
Derivation of sentence :
14
Other derivations:
aaabbbaaaSbbbaaSbbaSbS
aaaabbbbaaaaSbbbb
aaaSbbbaaSbbaSbS
15
Language of the grammar
S
aSbS
}0:{ nbaL nn
16
More Notation
Grammar PSTVG ,,,
:V
:T
:S
:P
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
17
Example
Grammar :
S
aSbSG
PSTVG ,,,
}{SV },{ baT
},{ SaSbSP
18
More NotationSentential Form: A sentence that contains variables and terminals
Example:
aaabbbaaaSbbbaaSbbaSbS
Sentential Forms sentence
19
We write:
Instead of:
aaabbbS*
aaabbbaaaSbbbaaSbbaSbS
20
In general we write:
If:
nww*
1
nwwww 321
21
By default: ww*
22
Example
S
aSbS
aaabbbS
aabbS
abS
S
*
*
*
*
Grammar Derivations
23
baaaaaSbbbbaaSbb
aaSbbS
S
aSbS
Grammar
Example
Derivations
24
Another Grammar ExampleGrammar :
A
aAbA
AbS
Derivations:
aabbbaaAbbbaAbbAbS
abbaAbbAbS
bAbS
⇒⇒⇒⇒
⇒⇒⇒
⇒⇒
G
25
More Derivations
aaaabbbbbaaaaAbbbbb
aaaAbbbbaaAbbbaAbbAbS
bbaS
bbbaaaaaabbbbS
aaaabbbbbS
nn
26
Language of a Grammar
For a grammar with start variable :
GS
}:{)( wSwGL
String of terminals
27
ExampleFor grammar :
A
aAbA
AbS
}0:{)( nbbaGL nn
Since: bbaS nn
G
28
A Convenient Notation
A
aAbA|aAbA
thearticle
aarticle
theaarticle |
29
Example A context-free grammar :
S
aSbS
aabbaaSbbaSbS
G
A derivation:
30
A context-free grammar :
S
aSbS
aaabbbaaaSbbbaaSbbaSbS
G
Another derivation:
31
S
aSbS
)(GL
(((( ))))
}0:{ nba nn
Describes parentheses:
32
S
bSbS
aSaS
abbaabSbaaSaS
A context-free grammar :G
A derivation:
Example
33
S
bSbS
aSaS
abaabaabaSabaabSbaaSaS
A context-free grammar :G
Another derivation:
34
S
bSbS
aSaS
)(GL }*},{:{ bawwwR
35
S
SSS
aSbS
ababSaSbSSSS
A context-free grammar :G
A derivation:
Example
36
S
SSS
aSbS
abababaSbabSaSbSSSS
A context-free grammar :G
A derivation:
37
S
SSS
aSbS
}prefixanyin
)()( and
),()(:{
v
vnvn
wnwnw
ba
ba
() ((( ))) (( ))
)(GL
Describes matched parentheses:
38
Definition: Context-Free Grammars
Grammar
Productions of the form:
xAString of variables and terminals
),,,( PSTVG
Variables Terminalsymbols
Startvariable
Variable
39
*},:{)(*
TwwSwGL
),,,( PSTVG
40
Definition: Context-Free Languages
A language is context-free
if and only if
there is a context-free grammar with
L
G)(GLL
41
Derivation Order ABS .1
A
aaAA
.3
.2
B
BbB
.5
.4
aabaaBbaaBaaABABS54321
Leftmost derivation:
aabaaAbAbABbABS32541
Rightmost derivation:
42
|AB
bBbA
aABS
Leftmost derivation:
abbbbabbbbB
abbBbbBabAbBabBbBaABS
Rightmost derivation:
abbbbabbBbb
abAbabBbaAaABS
43
Derivation Trees
44
ABS
ABS |aaAA |BbB
S
BA
45
ABS |aaAA |BbB
aaABABS
a a A
S
BA
46
ABS |aaAA |BbB
aaABbaaABABS S
BA
a a A B b
47
ABS |aaAA |BbB
aaBbaaABbaaABABS S
BA
a a A B b
48
ABS |aaAA |BbB
aabaaBbaaABbaaABABS S
BA
a a A B b
Derivation Tree
49
aabaaBbaaABbaaABABS
yield
aab
baa
S
BA
a a A B b
Derivation Tree
ABS |aaAA |BbB
50
Partial Derivation Trees
ABS
S
BA
Partial derivation tree
ABS |aaAA |BbB
51
aaABABS
S
BA
a a A
Partial derivation tree
52
aaABABS
S
BA
a a A
Partial derivation tree
sententialform
yield
aaAB
53
aabaaBbaaBaaABABS
aabaaAbAbABbABS S
BA
a a A B b
Same derivation tree
Sometimes, derivation order doesn’t matter
Leftmost:
Rightmost:
54
Ambiguity
55
aEEEEEE |)(|| aaa
E
EE
EE
a
a a
aaaEaa
EEaEaEEE
*
leftmost derivation
56
aEEEEEE |)(|| aaa
E
EE
a a
EE a
aaaEaa
EEaEEEEEE
leftmost derivation
57
aEEEEEE |)(|| aaa
E
EE
a a
EE a
E
EE
EE
a
a a
Two derivation trees
58
The grammar aEEEEEE |)(|| is ambiguous:
E
EE
a a
EE a
E
EE
EE
a
a a
string aaa has two derivation trees
59
string aaa has two leftmost derivations
aaaEaa
EEaEEEEEE
aaaEaa
EEaEaEEE
*
The grammar aEEEEEE |)(|| is ambiguous:
60
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
G
)(GLw
61
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
G
)(GLw
(or rightmost)
62
Why do we care about ambiguity?
E
EE
a a
EE a
E
EE
EE
a
a a
aaa
take 2a
63
E
EE
EE
E
EE
EE
222
2
2 2 2 2
2
64
E
EE
EE
E
EE
EE
6222
2
2 2 2 2
2
8222
4
2 2
2
6
2 2
24
8
65
E
EE
EE
6222
2
2 2
4
2 2
2
6
Correct result:
66
• We want to remove ambiguity
• Ambiguity is bad for programming languages
67
We fix the ambiguous grammar:
aEEEEEE |)(||
New non-ambiguous grammar:
aF
EF
FT
FTT
TE
TEE
)(
68
aF
EF
FT
FTT
TE
TEE
)(
aaaFaaFFa
FTaTaTFTTTEE
E
E T
T F
F
a
T
F
a
a
aaa
69
E
E T
T F
F
a
T
F
a
a
aaa
Unique derivation tree
70
The grammar :
aF
EF
FT
FTT
TE
TEE
)(
is non-ambiguous:
Every string hasa unique derivation tree
G
)(GLw
71
Another Ambiguous Grammar
IF_STMT if EXPR then STMT| if EXPR then STMT else STMT
72
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
expr1 then
elseif expr2 then
STMT
stmt1
if
IF_STMT
expr1 then else
if expr2 then
STMT stmt2if
stmt1
stmt2
73
Inherent Ambiguity
Some context free languageshave only ambiguous grammars
Example: }{}{ mmnmnn cbacbaL
||11
aAbA
AcSS
||22
bBcB
BaSS
21 | SSS
74
The string nnn cba
has two derivation trees
S
1S
S
2S
1S c 2Sa