Roger L Costello February 16 2014 1 Objective Show that Type 2 is a subset of Type 1 2 Grammars a brief refresher A grammar is a concise way to specify a language A language is a set of strings ID: 192760
Download Presentation The PPT/PDF document "Contextfree grammars are a subset of co..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, noncommercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Contextfree grammars are a subset of contextsensitive grammars
Roger L. CostelloFebruary 16, 2014
1Slide2
Objective: Show that Type 2 is a subset of Type 1
2Slide3
Grammars: a brief refresher
A grammar is a concise way to specify a language.A language is a set of strings. Example: This is an (infinite) language: {a, aa, aaa, …}
A grammar consists of a series of (rewrite) rules.
Each rule has a lefthand side and a righthand side. The two sides are separate by an arrow (
→
).
3Slide4
Sample Grammar
The below grammar consists of five rules. The grammar generates the language: {ab, aab, abb, aaab, aaabb, aaabbb, …}
4
S
→ AB
A → aA
A → a
B → bB
B → bSlide5
Generate a string from the grammar
5
S
→ AB
A → aA
A → a
B → bB
B → b
Here is a sequence of rules to generate: aab
S
→ AB
→ aAB
→ aaB
→
aab
GrammarSlide6
Rules with “alternates”
Notice in the above grammar there are two rules for A. Ditto for B
.
The two rules may be combined: the righthand side will consist of a series of alternatives, separated by a vertical bar (  ):
6
S
→ AB
A → aA
A → a
B → bB
B → b
Grammar
S
→ AB
A → aAA → aB → bBB → bGrammar
S → ABA → aA  aB → bB  bEquivalent Grammar
combine A’s combine B’sSlide7
“Zero” or more a’s and b’s
The above grammar requires every string in the language contain at least one a and at least one b
.
What grammar would generate the language: zero or more
a
’s followed by zero of more
b
’s?
7
Grammar
S
→ AB
A → aA  a
B → bB  bSlide8
Generate an empty string
Question: What grammar
would generate
the language:
zero
or more
a
’s followed by zero of more b’s?
Answer
:
Use rules that generate an empty string (a string of length zero).
We denote an empty string by:
ε
This grammar generates the desired language:
8
Grammar
S → ABA → aA  εB → bB  εSlide9
Generate both empty and nonempty
This rule for A generates both empty and nonempty:
9
A → aA 
ε
empty
nonemptySlide10
How to read a rule
10
A → aA 
ε
Read as:
A may be replaced by aA or by an empty string.
The arrow (
→
) is read as:
may be replaced by.Slide11
Terminal versus nonterminal symbols
11
A → aA 
ε
Nonterminal
symbols; these are symbols that may be replaced (further expanded).
T
erminal
symbols; these are symbols that may not be replaced.Slide12
Notation
Nonterminal symbols: denoted by uppercase letters.Example: Q1, Q
2
, A, P, S denote nonterminal symbols
Terminal symbols
: denoted by lowercase letters.
Example: a, b, c denote terminal symbols
12Slide13
Contextsensitive grammars
Every rule has this form:
13
Q
1
AQ
2
→
Q
1
PQ
2
context
context
A is replaced by PSlide14
Contextsensitive grammars
Every rule has this form:
Q
1
AQ
2
→
Q
1
PQ
2
That is, some symbol
A
is rewritten to some symbol P while the surrounding (context) symbols Q1 and Q2 remain unchanged. Note: P can be multiple symbols.14Slide15
Contextsensitive grammars
Every rule has this form:
Q
1
AQ
2
→
Q
1
PQ
2
That is, some symbol
A
is rewritten to some symbol P while the surrounding (context) symbols Q1 and Q2 remain unchanged. Note: P can be multiple symbols. A must be a nonterminal. Q1, Q2, and P are either nonterminals or terminals.15Slide16
Contextsensitive grammars
Every rule has this form:
Q
1
AQ
2
→
Q
1
PQ
2
That is, some symbol
A
is rewritten to some symbol P while the surrounding (context) symbols Q1 and Q2 remain unchanged. Note: P can be multiple symbols. A must be a nonterminal. Q1, Q2, and
P are either nonterminals or terminals.P must not be empty (ε).16Slide17
Contextsensitive grammars
Every rule has this form:
Q
1
AQ
2
→
Q
1
PQ
2
That is, some symbol
A
is rewritten to some symbol P while the surrounding (context) symbols Q1 and Q2 remain unchanged. Note: P can be multiple symbols. A must be a nonterminal. Q1, Q2, and
P are either nonterminals or terminals.P must not be empty (ε).None of the rules lead to empty except possibly for a rule S → ε, in which case S does not occur on the righthand side of any rules.
17Slide18
Sample contextsensitive rule
18
S
→
abc
empty
context
S is replaced by abcSlide19
Sample contextsensitive rule
19
S
→
aSQ
empty
context
S is replaced by aSQSlide20
Sample contextsensitive rule
20
bQc
→
bbcc
context
Q is replaced by bcSlide21
Sample contextsensitive rule
21
cQ
→
cc
empty
right
context
Q is replaced by cSlide22
Sample contextsensitive rule
22
cc
→
Qc
empty
left
context
c is replaced by QSlide23
Swap c and
Q23
cQ
→
cc
cc
→
Qc
Collectively, the two rules swap
c
and
Q
.Slide24
Sample contextsensitive grammar
The language generated by the below contextsensitive grammar is: an
b
n
c
n
24
→
→
→
→
abc  aSQ
bbcc
cc
Qc
S
bQccQcc1.2.3.4.
Grammar for a
n
b
n
c
nSlide25
Generate a string from the grammar
25
Derivation of a
3
b
3
c
3
S (start)
aSQ (rule 1)
aaSQQ (rule 1)
aaabcQQ (rule 1)
aaabccQ (rule 3)
aaabQcQ
(rule
4)aaabbccQ (rule 2)aaabbccc (rule 3)aaabbQcc (
rule 4)aaabbbccc (rule 2)→→ → →
abc  aSQbbccccQcSbQccQcc
1.
2.
3.
4.
Grammar for a
n
b
n
c
n
generated stringSlide26
Next on the agenda
We have seen what contextsensitive grammars look like, and the restrictions imposed on them (e.g., the P in the righthand side can’t be empty).
Now let’s turn our attention to
contextfree grammars
.
26Slide27
Contextfree grammars
Every rule has this form:
27
A
→
P
empty
context
A is replaced by PSlide28
Contextfree grammars
Every rule has this form:
A
→
P
That is, some symbol
A
is rewritten to some symbol
P
.
A
never has context – it is contextfree!
P can be multiple symbols28Slide29
Contextfree grammars
Every rule has this form:
A
→
P
That is, some symbol
A
is rewritten to some symbol
P
.
A
never has context – it is
contextfree
! P can be multiple symbols. A must be a nonterminal. P is any sequence of nonterminals and terminals.29Slide30
Contextfree grammars
Every rule
has this form:
A
→
P
That is, some symbol
A
is rewritten to some symbol
P
.
A
never has context – it is contextfree! P can be multiple symbols. A must be a nonterminal. P is any sequence of nonterminals and terminals.P may be empty (ε).30Slide31
Next on the agenda
Now we have seen contextsensitive grammars and contextfree grammars.Now it’s time to compare them.
31Slide32
Compare the two types of grammars
32
Q
1
AQ
2
→
Q
1
PQ
2
context
context
A is replaced by P
Contextsensitive
ContextfreeA → P
empty
context
A is replaced by P
A contextfree rule is a contextsensitive rule without context
,
s
o contextfree is a subset of contextsensitive; right? Slide33
Key Point
33
The
P
in a contextsensitive rule
cannot be empty
whereas the
P in a contextfree rule
can be empty
. So it is not an applestoapples comparison
and we cannot claim that contextfree is a subset of contextsensitive.Slide34
ε
Contextfree has an additional value
34
A
Q
1
Q
2
P
Q
1
Q
2
A
P
Contextsensitive
ContextfreeSlide35
What is needed?
What do we need to make the claim that a contextfree rule is a special case (subset) of
a contextsensitive rule
?
35Slide36
Contextfree without an empty P
If we can show that, for every contextfree grammar there is an equivalent grammar that doesn’t have an empty P, then we will have an applestoapples comparison.
36Slide37
Need to show this
37
ε
A
P
Contextfree rule with
ε
A
P’
Equivalent contextfree rule without
ε
transform to an equivalent grammarSlide38
2step strategy
Use a systematic procedure (i.e., algorithm) to find all the nonterminal symbols that generate empty (ε).
Modify the grammar rules: eliminate the nonterminals found in step 1 and then modify the rules that use the
eliminated
nonterminals.
38Slide39
A generates empty
39
A
→
εSlide40
A generates empty and nonempty
40
A
→
ε

aSlide41
B generates empty
41
A
→
ε
B
→
ASlide42
Procedure
Find the nonterminals that directly generate empty, i.e., those of this form: X
→
ε
Then find the nonterminals which have on their righthand side
exclusively
symbols found in step 1, e.g.,
Y
→
X
Then find the nonterminals which have on their righthand side
exclusively
symbols found in step 1 or step 2Repeat until no new nonterminals are found.
42Slide43
Closure algorithm
The procedure described on the previous slide is called a closure algorithm.We will find all the
nonterminal
symbols that produce empty
(
ε
)
by using a closure algorithm.
43Slide44
2 steps to identify the nonterminals
Our closure algorithm identifies nonterminals that generate empty using these two steps:
Initialization
:
If
a rule has
ε
on its righthand side, then the rule’s lefthand side nonterminal generates empty. Inference rule:
If all the righthand side members of a rule
produce
empty, then the rule’s lefthand side nonterminal produces empty
.
44Slide45
Which nonterminals generate empty?
Let’s use the closure algorithm on the below grammar. The closure algorithm finds all the nonterminals that generate empty.
45
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
dSSAABCD
Goal: Find the nonterminals that generate empty (
ε
)Slide46
Round 1 (Initialization)
46
Rule
Produces empty?
S
→
A B
S
→
C
A
→ ε
A produces emptyA → a
B
→
A
C
→
A D
D
→
dSlide47
Round 2 (inference)
47
Rule
Produces empty?
S
→
A B
S
→
C
A
→ ε
A produces emptyA → a
B
→
A
B
produces empty (because A produces empty)
C
→
A D
D
→
dSlide48
Round 3 (inference)
48
Rule
Produces empty?
S
→
A B
S
produces empty (because A and B produce empty)
S
→
C
A →
εA produces emptyA →
a
B
→
A
B
produces empty (because A produces empty)
C
→
A D
D
→
dSlide49
Round 4
49
Round 4 adds no additional members to the set.
Rule
Produces empty?
S
→
A B
S
produces empty (because A and B produce empty)
S
→
C
A
→
ε
A
produces empty
A
→
a
B
→
A
B
produces empty (because A produces empty)
C
→
A D
D
→
dSlide50
Nonterminals that generate empty
50
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
d
SSAABCDNonterminals that generate empty: {A, B, S}Slide51
Make the grammar contextsensitivecompliant
Our goal is to modify the grammar so that it is a contextsensitive grammar.
It will be both contextsensitive and contextfree
51
→
→
→
→
→
→
→
A B
C
ε
a
A
A DdSSAABCD
Grammar that conforms to the rules of contextsensitive grammars.
Original
ModifiedSlide52
Remove rules with ε
on the righthand sideRecall that contextsensitive grammars do not allow empty rules, except the start symbol may be empty. So we need to remove the empty
rules:
52
→
→
→
→
→
→
→
A B
C
ε
a
A
A DdSSAABCDRemove this ruleSlide53
Remove references
to empty nonterminalsSuppose a grammar has this empty rule:
53
X
→
ε
R
emove it, per the previous slide.
The following rule has
X
on its righthand side:
Y
→
X Z
So we must remove the
X
:
Y
→
Z
Slide54
Nonterminal could have
empty and nonempty rulesSuppose X
has an empty and nonempty rule:
54
X
→
ε
 x
The
X
in the following rule could generate either empty or
x
:
Y
→
X Z
Recall that we will remove
X
→
ε
s
o there must be one rule for
Y
that omits
X
and one that does not:
Y
→
Z  X Z
X
is empty
X
is nonemptySlide55
Recap
Consider this rule: Q →
V N
Suppose the closure algorithm determines that
V
is in the set of nonterminals that generate empty.
If
V
is empty then
Q
generates
N
, so we need this rule:
Q
→
NSuppose V also has a nonempty rule.If V is nonempty then Q generates V N, so we need this rule: Q → V NHere is Q’s modified rule: Q → N  V N
55Slide56
Resume modifying our grammar
Now that we understand how to modify the rules, let’s resume making contextsensitivecompliant our sample grammar.
56Slide57
Modify the rule for C
57
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
d
SSAABCDOn the righthand side of this rule is A. A generates empty so we erase A. However,
A
also generates
a
so
C
could generate
a D
. Here is the modified rule:
C
→
D  A DSlide58
Modify the rule for S
58
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
d
SSAABCDBoth symbols on the righthand side of this rule generate empty. A generates empty and it also generates a
.
B
generates
A
. So this rule is capable of generating
ε
,
a
and
aa
. Here is the modified rule: S
→
A  A BSlide59
Here is the modified grammar
59
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
d
SSAABCD
→
→
→
→
→
→
A  A B
C
a
A
D  A D
d
S
S
A
B
C
D
Original
ModifiedSlide60
No empty rules
60
→
→
→
→
→
→
A  A B
C
a
A
D  A D
d
SSA
BCDModifiedNo empty rules, as required by contextsensitive grammars – Yea!Slide61
Lost the ability to generate empty
61
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
d
SSAABCD→
→
→
→
→
→
A  A B
C
a
A
D  A D
d
S
S
A
B
C
D
The modified grammar does not generate empty
But the original grammar does generate empty
W
e need to add this rule: S
→
εSlide62
Here’s the final, modified grammar
62
→
→
→
→
→
→
→
ε
A  A B
C
a
A
D  A Dd
SSSABCDSlide63
Equivalent grammars
63
→
→
→
→
→
→
→
ε
A  A B
C
a
A
D  A Dd
SSSABCD
→
→
→
→
→
→
→
A B
C
ε
a
A
A D
d
S
S
A
A
B
C
D
Original
Modified
equivalentSlide64
It’s contextsensitivecompliant
64
→
→
→
→
→
→
→
ε
A  A B
C
a
A
D  A Dd
SSSABCDModified
There are no empty rules except for the start symbol (S). Therefore, it is a contextsensitive grammar.
It’s
also
contextfreecompliantSlide65
How we modified the grammar to be contextsensitivecompliant
Using a closure algorithm, we found all the nonterminals that generate empty.We modified the rules so that none of them generated empty:
If a rule’s righthand side is
ε
, delete it.
If a rule’s righthand side contains a nonterminal that is in the set produced by the closure algorithm, create a rule without the nonterminal. If the nonterminal also has a nonempty rule, create a rule with the nonterminal.
If the original grammar generates empty, add this rule: S
→
ε
65Slide66
Contextfree is a subset of contextsensitive
We now have a procedure for converting every contextfree grammar into an equivalent
contextfree
grammar that complies with the contextsensitive rules.
Therefore, contextfree grammars are a restricted form of contextsensitive grammars.
Therefore, contextfree grammars are a subset of contextsensitive grammars.
66Slide67
Type 2 is a subset of Type 1
67Slide68
Type 2 is a “proper” subset of Type 1
Not only is Type 2 a subset of Type 1, it is a proper subset
. This means that there are grammars in Type 1 that are not in Type 2:
68
a
n
b
n
c
nSlide69
Language generated by a grammar
A grammar generates a language; that is, a set of strings.For example, this simple grammar: S
→
ε
 aS
generates this set of strings:
{ε
, a, aa, aaa, …}
That is the language generated by the grammar.
Notice that
ε
is an element of the language (recall that
ε
is a string of length zero).
69Slide70
εdetecting procedure
It is useful to know if ε is an element of the language generated by a grammar.
We need a procedure that can take
any arbitrary grammar
and determine if
ε
is an element of the language generated by
the grammar:
70
grammar
procedure
ε
is (not) an element of the language generated by the grammarSlide71
Implementing the ε
detecting procedure71
grammar
procedure
ε
is (not) an element of the language generated by the grammar
This can be implemented using the closure algorithm.Slide72
Here’s the implementation
72
grammar
Is the start symbol in the set?
ε
is (not) an element of the language generated by the grammar
set of nonterminals that generate empty
closure
algorithmSlide73
Recap of the implementation
Recall the closure algorithm: it produces the set of nonterminals that generate empty.For our sample grammar it produced: {A, B, S}
The start symbol (
S
) generates
ε
.
Therefore, ε is an element of the language generated by the grammar.
73Slide74
Decision procedure
We now have a procedure for deciding, for any arbitrary contextfree grammar, if the empty string is a member of the language generated by the grammar.This procedure is called a
decision procedure
.
74Slide75
Big accomplishments
In these slides we have accomplished much. We have:shown that Type 2 (contextfree) grammars are a subset of Type 1 (contextsensitive) grammarscreated a decision procedure that is capable of deciding, for any
arbitrary
grammar, if
ε
is an element of the language generated
by the grammar.
75Slide76
Formalize the closure algorithm
The next slide describes the closure algorithm very succinctly.I find great beauty and elegance in it. There’s no fluff in it; I call it “pure knowledge”.
76Slide77
Closure algorithm (formal)
U
1
is the set of
all
the empty
nonterminals:
U
1
=
{X

X
→ ε}U2 is the set of all the empty nonterminals (that is, U1) plus all the nonterminals that have a righthand side containing exclusively nonterminals from U1:U2 = U1 ∪
{X  X → P for some P containing exclusively nonterminals from U
1
}
U
i+1
is the set of
all the
nonterminals from
U
i
plus all the nonterminals that have a righthand side containing exclusively nonterminals from
U
i
:
U
i+1
=
U
i
∪
{X

X
→
P
for some
P
containing exclusively nonterminals from
U
i
}
There
is some index
k
for which
U
k+1
=
U
k
.
That is, additional rounds do not result in finding more nonterminals that produce empty
.
The set of nonterminals that generate empty is
U
k
.
77Slide78
Comments, questions
I hope you found this minitutorial helpful.If you found any typos or errors in the material, please notify me. If you found any parts confusing, please notify me
.
Email me at:
roger.costello@gmail.com
Thanks!
78