CS381
Second Mid Term
Friday Nov 8, 2002
Fall 2002
Olin 155
9:059:55
This is a 50minute in class closed book exam.
All questions are straightforward and you
should have no trouble doing them.
Please show all work and write legibly.
Thank you.
1
. (a) Give a contextfree grammar for the language
}
'
'
*
)
(

{
s
b
and
s
a
of
number
equal
an
has
x
and
b
a
x
x
+
∈
(b) For each variable in the grammar describe the strings that can be generated from the
variable by a statement such as
s
b
and
s
a
of
number
equal
an
has
x
iff
x
S
'
'
⇒
Solution
:
bSa
aSb
SS
S



ε
→
s
b
and
s
a
of
number
equal
an
has
x
iff
x
S
'
'
⇒
Clearly any string generated has an equal number of a’s and b’s since the righthand side
of each production has an equal number of a’s and b’s.
Thus, it remains to show only
that all such strings are generated.
Let x have an equal number of a’s and b’s.
If x=ε then clearly x is generated.
Assume all
x with equal number of a’s and b’s and x<n are generated.
Let x be of length n.
Write
k
x
x
x
x
2
1
=
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '05
 HOPCROFT
 Formal language, Contextfree grammar, equal number

Click to edit the document details