Java Reference
In-Depth Information
1. Consider a right-recursive formulation for Digs of Figure 7.3, resulting
in the following grammar.
1 Start
→
Digs
ans
$
call
print
(
ans
)
2 Digs
up
→
d
next
Digs
below
up
←
below
×
10
+
next
3
|
d
f irst
up
←
f irst
Are the semantic actions still correct? If so, explain why they are still
valid; if not, provide a set of semantic actions that does the job properly.
2. The grammar in Figure 7.8 is almost faithful to the language originally
defined in Figure 7.7. To appreciate the di
ff
erence, compare how each
grammar treats the input string x5$.
(a) In what respects do the grammars generate di
erent languages?
(b) Modify the grammar in Figure 7.8 to maintain its style of semantic
processing but to respect the language definition in Figure 7.7.
ff
3. The language generated by the grammar in Figure 7.8 uses the terminal x
to introduce the base. Amore common convention is to separate the base
from the string of digits by some terminal symbol. Instead of x8431
to represent 431
8
, a language following the common convention would
use 8x431.
(a) Design an LALR(1) grammar for such a language and specify the
semantic actions that compute the string's numeric value. In your
solution, allow the absence of a base specification to default to base
10, as in Figure 7.8.
(b) Discuss the tradeo
s of this language and your grammar as com-
pared with the language and grammar of Figure 7.8.
ff