Upload
kalaivani-manoharan
View
216
Download
1
Embed Size (px)
Citation preview
7/28/2019 Chapter 4 PDA
1/62
Chapter 4
Context-Free Grammars and
Languages
1
7/28/2019 Chapter 4 PDA
2/62
2
Context-Free Languages
7/28/2019 Chapter 4 PDA
3/62
3
Regular Languages
0:{ nbann
{ Rww
**ba *)( ba+
7/28/2019 Chapter 4 PDA
4/62
4
Regular Languages
{ nnba { Rww
Context-Free Languages
7/28/2019 Chapter 4 PDA
5/62
5
Context-Free Languages
PushdownAutomata
Context-Free
Grammars
stack
automaton
7/28/2019 Chapter 4 PDA
6/62
6
Context-Free Grammars
7/28/2019 Chapter 4 PDA
7/627
Grammars
Grammars express languages
Example: the English language
verbpredicate
nounarticlephrasenoun
predicatephrasenounsentence
_
_
7/28/2019 Chapter 4 PDA
8/628
walksverb
runsverb
dognouncatnoun
thearticle
aarticle
7/28/2019 Chapter 4 PDA
9/629
Example 1:
A derivation of the dog walks:
walksdogthe
verbdogthe
verbnounthe
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
7/28/2019 Chapter 4 PDA
10/6210
Example 2:
A derivation of a cat runs:
runscata
verbcata
verbnouna
verbnounarticle
verbphrasenoun
predicatephrasenounsentence
_
_
7/28/2019 Chapter 4 PDA
11/6211
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 }
7/28/2019 Chapter 4 PDA
12/6212
Notation
dognouncatnoun
Variable Terminal
Production Rules
7/28/2019 Chapter 4 PDA
13/6213
Another Example
Grammar:
Derivation of sentence :
S
aSS
aaSbS
a
aSS S
7/28/2019 Chapter 4 PDA
14/6214
aabaaSbbaSbS
aSbS S
aab
S
aSSGrammar:
Derivation of sentence :
7/28/2019 Chapter 4 PDA
15/6215
Other derivations:
aaabbaaaSbbbaaSbbaSbS
aaaabbbbaaaaSbbbb
aaaSbbaaSbbaSbS
7/28/2019 Chapter 4 PDA
16/6216
Language of the grammar
S
aSS
0:{ = nbaL nn
7/28/2019 Chapter 4 PDA
17/6217
More Notation
Grammar ( )PSTVG ,,,=
:
:
:S
:P
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
7/28/2019 Chapter 4 PDA
18/6218
Example
Grammar :
S
aSSG
( )PSTVG ,,,=
{S= ,{ ba=
,{ = SaSbSP
7/28/2019 Chapter 4 PDA
19/6219
More Notation
Sentential Form:
A sentence that containsvariables and terminals
Example:
aaabbaaaSbbbaaSbbaSbS
Sentential Forms sentence
7/28/2019 Chapter 4 PDA
20/6220
We write:
Instead of:
aaabbS*
aaabbaaaSbbbaaSbbaSbS
7/28/2019 Chapter 4 PDA
21/6221
In general we write:
If:
nww
*
1
nwwww
321
7/28/2019 Chapter 4 PDA
22/62
22
By default: ww*
7/28/2019 Chapter 4 PDA
23/62
23
Example
S
aSS
aaabbS
aabbS
abS
S
*
*
*
*
Grammar Derivations
7/28/2019 Chapter 4 PDA
24/62
24
Another Grammar Example
Grammar :
A
aAbA
AbS
Derivations:
aabbaaAbbbaAbbAbS
abbaAbbAbSbAbS
G
7/28/2019 Chapter 4 PDA
25/62
25
A Convenient Notation
A
aAA|aAbA
thearticle
aarticle
thaarticle |
7/28/2019 Chapter 4 PDA
26/62
26
Definition: Context-Free Grammars
Grammar
Productions of the form:
AString of variables
and terminals
),,,( PSTVG =
Variables Terminalsymbols Startvariable
Variable
7/28/2019 Chapter 4 PDA
27/62
27
Definition: Context-Free Languages
A language is context-free
if and only if
there is a context-free grammar
with
L
G)(GLL =
7/28/2019 Chapter 4 PDA
28/62
28
Derivation Order
ABS.1
A
aaA
.3
.2
B
BB
.5
.4
aaaaBbaaBaaABABS54321
Leftmost derivation:
aaaaAbAbABbABS32541
Rightmost derivation:
ABS
7/28/2019 Chapter 4 PDA
29/62
29
|AB
bBbA
aABS
Leftmost derivation:
abbbbabbbbBabbBbbBabAbBabBbBaABS
Rightmost derivation:
abbbbabbBbb
abAabBbaAaABS
7/28/2019 Chapter 4 PDA
30/62
30
Derivation Trees
7/28/2019 Chapter 4 PDA
31/62
31
ABS
ABS |aaAA |BbB
S
B
7/28/2019 Chapter 4 PDA
32/62
32
ABS |aaAA |BbB
aaABABS S
B
7/28/2019 Chapter 4 PDA
33/62
33
ABS |aaAA |BbB
aaABaaABABS S
B
B b
7/28/2019 Chapter 4 PDA
34/62
34
ABS |aaAA |BbB
aaBaaABbaaABABS S
B
B b
7/28/2019 Chapter 4 PDA
35/62
35
ABS |aaAA |BbB
aaaaBbaaABbaaABABS S
B
B b
Derivation Tree
| |
7/28/2019 Chapter 4 PDA
36/62
36
aaaaBbaaABbaaABABS
yield
aabaa=
S
B
B b
Derivation Tree
ABS |aaAA |BbB
P i l D i i T
7/28/2019 Chapter 4 PDA
37/62
37
Partial Derivation Trees
ABSS
B
Partial derivation tree
ABS |aaAA |BbB
7/28/2019 Chapter 4 PDA
38/62
38
aaABABS
S
B
Partial derivation tree
sentential
7/28/2019 Chapter 4 PDA
39/62
39
aaABABS
S
B
Partial derivation tree
sentential
form
yield
aaAB
Sometimes derivation order doesnt matter
7/28/2019 Chapter 4 PDA
40/62
40
aaaaBbaaBaaABABS
aaaaAbAbABbABS S
B
B b
Same derivation tree
Sometimes, derivation order doesn t matter
Leftmost:
Rightmost:
7/28/2019 Chapter 4 PDA
41/62
41
Ambiguity
aEEEEEE |)(|| +
7/28/2019 Chapter 4 PDA
42/62
42
aEEEEEE |)(|| +
aaa +
E
EE
EE
+
aaaEaa
EEaEaEEE
*++
+++
leftmost derivation
aEEEEEE |)(|| +
7/28/2019 Chapter 4 PDA
43/62
43
aEEEEEE |)(|| +
aaa +
E
EE
+
EE
aaaEaa
EEaEEEEEE
++
++
leftmost derivation
aEEEEEE |)(|| +
7/28/2019 Chapter 4 PDA
44/62
44
aEEEEEE |)(|| +
aaa +
E
EE
+
EE
E
EE
EE
+
Two derivation trees
Th EEEEEE |)(||
7/28/2019 Chapter 4 PDA
45/62
45
The grammar aEEEEEE |)(|| +
is ambiguous:
E
EE
+
a
EE
E
EE
EE
+
a
string aaa + has two derivation trees
Th EEEEEE |)(||
7/28/2019 Chapter 4 PDA
46/62
46
string aaa + has two leftmost derivations
aaaEaa
EEaEEEEEE
++
++
aaaEaa
EEaEaEEE
*++
+++
The grammar aEEEEEE |)(|| +
is ambiguous:
D fi iti
7/28/2019 Chapter 4 PDA
47/62
47
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
G
)(GLw
h d
7/28/2019 Chapter 4 PDA
48/62
48
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
G
)(GLw
(or rightmost)
Wh d b t bi it ?
7/28/2019 Chapter 4 PDA
49/62
49
Why do we care about ambiguity?
E
EE
+
EE
E
EE
EE
+
aaa +
take 2=a
7/28/2019 Chapter 4 PDA
50/62
50
E
EE
+
EE
E
EE
EE
+
222+
2
2 2 2 2
2
7/28/2019 Chapter 4 PDA
51/62
51
E
EE
+
EE
E
EE
EE
+
222 =+
2
2 2 2 2
2
222 =+
2 2
2
2 2
2
7/28/2019 Chapter 4 PDA
52/62
52
E
EE
EE
+
222 =+
2 2
2 2
Correct result:
7/28/2019 Chapter 4 PDA
53/62
53
We want to remove ambiguity
Ambiguity is bad for programming languages
We fix the ambiguous grammar:
7/28/2019 Chapter 4 PDA
54/62
54
We fix the ambiguous grammar:
aEEEEEE |)(|| +
New non-ambiguous grammar:
aF
EF
F
FT
TE
TEE
+
)(
FTaTaTFTTTEE +++++
7/28/2019 Chapter 4 PDA
55/62
55
aF
EF
F
FT
TE
TEE
+
)(
aaaFaaFFa +++
E
E +
F
FF
aaa+
U i d i ti t
7/28/2019 Chapter 4 PDA
56/62
56
E
E +
F
FF
aaa+
Unique derivation tree
The grammar : TEE +G
7/28/2019 Chapter 4 PDA
57/62
57
The grammar :
aF
EF
F
FT
TE
TEE
+
)(
is non-ambiguous:Every string has
a unique derivation tree
G
)(GLw
Another Ambiguous Grammar
7/28/2019 Chapter 4 PDA
58/62
58
Another Ambiguous Grammar
IF_STMT if EXPR then STMT| if EXPR then STMT else STMT
If expr1 then if expr2 then stmt1 else stmt2
7/28/2019 Chapter 4 PDA
59/62
59
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
7/28/2019 Chapter 4 PDA
60/62
APPLICATIONS OFCONTEXT-FREE GRAMMARS
60
7/28/2019 Chapter 4 PDA
61/62
CFGs and Programming Languages
Important uses of formal languages: to define precisely a programming language.
to construct an efficient translator for it.
RLs are used for simple patterns, while CFLs for morecomplicated aspects.
61
CFGs and Programming Languages
7/28/2019 Chapter 4 PDA
62/62
CFGs and Programming Languages
S-grammars:
::= if
::= then ::= else