Java Reference
In-Depth Information
1
Start
→
Num
ans
$
call
print
(
ans
)
2 Num
ans
→
SignalOctal Digs
octans
ans
←
octans
3
|
SignalDecimal Digs
decans
ans
←
decans
4 SignalOctal
→
o
base
←
8
5 SignalDecimal
→ λ
base
←
10
6 Digs
up
→
Digs
below
d
next
up
←
below
×
base
+
next
7
|
d
f irst
up
←
first
Figure 7.6: Use of
λ
-rules to force semantic action.
mar with productions that are not necessary from a syntactic point of view.
While those extra productions accommodate di
erentiated semantic actions
at appropriate points in the parse, our next approach avoids such redundancy.
ff
Bottom-up parsers normally are prepared to execute semantic actions only on
reductions. If a semantic action is desired on the shift of some symbol
X
,then
a
unit production
of the form A
→X
can be introduced, with occurrences of
X
replaced by A in the grammar's productions. The semantic action can then
be associated with the reduction of A
→X
. Similarly, if a semantic action is
desired
between
two symbols
→λ
can be introduced into the grammar. The semantic action is associated with
that rule, and all occurrences of
X
m
and
X
n
, then a production of the form A
X
n
.
We can apply these idea to the grammar of Figure 7.4 to obtain the grammar
shown in Figure 7.6:
X
m
X
n
are replaced by
X
m
A
•
The o is replaced by the nonterminal SignalOctal, which derives the o
and whose semantic action sets the global variable
base
←
8.
•
Correspondingly, if the o is not present, the productionSignalDecimal
→λ
sets
base
←
10.