Each of the following two grammars generates the language of all strings of as with positive even lengths. Grammar 1 is
S -> a S a S -> a a
and Grammar 2 is
S -> a a S S -> a a
Of these two grammars, one of them is suitable for shift-reduce parsing, and in fact you can successfully use any of the SLR(1), LR(1), or LALR(1) parser construction techniques. The other will not work using any of the three techniques or any other technique that uses limited lookahead.
First, determine which of the two grammars can be parsed with just
one symbol of lookahead. Consider an input that is a long, even-length string of
as, such as aaaaaaaa or aaaaaaaaaa. With each of
the two grammars, there will be some point at which a reduction by the
production S -> a a
needs to occur. At what point
in the string will this need to happen using grammar 1? At what point
when using grammar 2? This should tell you which grammar is suitable
for use with one symbol of lookahead. (If not, you can try creating
canonical LR(1) parsers for each and see which you get stuck on.)
Now, for the workable grammar, construct a parser using whichever of SLR(1), LR(1), or LALR(1) you prefer. Augment the grammar, draw the state machine, and then construct the parsing tables.
Do exercise 5.2.5 on page 317. Ignore the hint in exercise
5.2.4. A hint for 5.2.5 would be as follows: 101.101
can
also be thought of as representing 5 5/8.