Uploaded by damian.vather

CHAPTER 18 Solutions

advertisement
CHAPTER 18 (1997 edition)
Chapter 18, page 429, number 1(v)
The given CFG has the productions:
SAB
ABSB
BAAS
ACC
BCC
CSS
Aab
Cbbb.
(Note that Λ cannot be generated.)
We are going to use Theorem 42 to decide whether or not any words are generated by the above grammar.
Look at the reformulation of the algorithm in the study guide. First of all we convert the CFG into CNF:
SAB
ABR1
R1SB
BAR2
R2AS
ACC
BCC
CSS
CXX
Aab
Cb
Xb.
By repetition of Step 1 we get:
Choose Aa in order to eliminate A. We get:
SaB
R1SB
BaR2
R2aS
BCC
CSS
CXX
Cb
Xb.
Choose Cb in order to eliminate C. We get:
SaB
R1SB
BaR2
R2aS
Bbb
Xb.
Choose Xb in order to eliminate X. We get:
SaB
R1SB
BaR2
R2aS
Bbb.
Now, choose Bbb to eliminate B. We get:
Sabb
R1Sbb
R2aS
Finally, choose Sabb to eliminate S.
So, according to Step 2 of Theorem 42, the CFG generates at least one word, so the CFL is nonempty.
Chapter 18, page 429, number 3(viii)
The CFG has productions:
SXX
XSSa.
Apply Theorem 42. Use Xa to eliminate X. We then get Saa. Use Saa to eliminate S. We get no
productions. It follows that the CFG generates at least one word. We are left with no nonterminals, so
none of the nonterminals are unproductive.
Apply Theorem 43. There are no nonterminals to eliminate from the productions, so we have:
SXX
XSSa
Is X useful? Paint all X's blue (well okay, just underline them) and we get:
SXX
XSS│a
and then
SXX
XSS│a.
S is blue, so X is useful.
Is S useful? Paint all S's blue and we get:
SXX
XSS│a
and so, trivially, S is useful.
Apply Theorem 44. There are no productions to eliminate, so we have:
SXX
XSS│a.
Is X self-embedded? Change X to  on the left sides. We get:
SXX
 SS│a.
Paint all X's blue. We get:
SXX
 SS│a
and then
SXX
 SS│a
followed by
SXX
 SS│a.
Thus X is self-embedded and the CFL is infinite. As X is useful, we can stop at this point. We opt
however to also check whether S is self-embedded; remember all teachers' favourite maxim: "Practice
makes perfect".
Is S self-embedded? Change S to  on the left sides:
 XX
XSS│a
Paint all S's blue (in our case underline them):
 XX
XSS│a
and then
 XX
XSS│a
followed by
 XX
XSS│a.
It follows that S is self-embedded and the CFL is infinite.
Chapter 18, page 430, number 5
Cohen defines live and dead productions on page 351.
Now, if S is the only nonterminal, all live productions have the form SSS and dead productions are of
the form St where t is some terminal in the grammar. We give two solutions to this problem.
Here is our first solution. We observe that S is useful as it generates at least one word (namely t above we are given that there is at least one dead production). Also, S is self-embedded due to the presence of
the production SSS so that infinitely many words can be generated (by repeating the production SSS
any number of times). Therefore the language generated by this CFG is infinite.
Our second solution shows how to apply the algorithm in the proof of Theorem 44 to show that the
language generated by the CFG is infinite.
Step 1 of the algorithm given in the proof of Theorem 44, involves the algorithm of Theorem 43. Step 1
of Theorem 43 involves the algorithm of Theorem 42 and that algorithm starts off by converting the given
CFG into CNF. So, during the application of Theorem 42 to prove that the CFG generates at least one
word, we convert the given CFG into CNF. As S is the only nonterminal, productions of the form SSS
and St will also be present in the grammar converted into CNF. It follows from the presence of the
production St that the grammar generates at least one word, so S is not unproductive. Now apply the
algorithm of Theorem 43. There are no nonterminals to eliminate (as the only nonterminal S is not
unproductive), so we start again with the original set of productions. Is S useful? Well, paint all S's blue,
then obviously S is blue, so S is useful.
We are now in a position to apply the algorithm of Theorem 44. There are no productions to eliminate
since all nonterminals (well O.K., only S) are useful, so we start with the original productions. Is S
self-embedded? According to the algorithm, change S to  on the left sides of all productions. In
particular, the production SSS will now become  SS. Paint all the S's blue. It is clear that 
becomes blue in the next step because of the existence of the production  SS, so S is self-embedded.
According to the algorithm of Theorem 44, the CFL is infinite.
Chapter 18, page 431, number 13(ii)
The grammar for PLUS-TIMES, given on page 415, is:
SE
ET+E│T
TF*T│F
F(E)│i.
The expression we wish to parse is: i*i+i*i. Let us see:
S
E
T
T+E
F*T+E
i*T+E
i*F+E
i*i+E
i*i+T
i*i+T+E
i*i+F*T
i*i+F
i*i+i*T
i*i+(E)*T
i*i+i*F
i*i+i*i
i * i + i *F * T
i*i+i*(E)
i*F*T+E
i*(E)+E
(E)*T+E
F+E
So a left-most derivation is:
SET+EF*T+Ei*T+Ei*F+Ei*i+E
i*i+Ti*i+F*Ti*i+i*Ti*i+i*Fi*i+i*i
Note that in the figure above we terminated the first rightmost branch at the non-terminal T. Strictly
speaking this is not correct as this branch can be expanded three more levels downwards before all
branches are terminated. As all branches eventually terminate, we opted not to draw these expansions in.
You however, should in general not follow such shortcuts but rather give completely motivated answers.
Check the validity of this remark by filling in the missing expansions on the above figure. Also, give
reasons for terminating all the rightmost branches.
Chapter 18, page 431, number 14(ii)
The expression is ((i)+((i))). We will only follow a correct path - you should fill in the whole tree.
.
Write down a derivation of ((i)+((i))) from the above.
Download