Java Reference
In-Depth Information
1 Start
Num ans
$
call
print
( ans )
2 Num ans
oOctDigs octans
ans octans
3
|
DecDigs decans
ans decans
4 DecDigs up
DecDigs below d next
up below ×
10
+ next
5
|
d f irst
up f irst
6 OctDigs up
OctDigs below
d next
if next
8
1
then
( "Non-octal digit" )
up below ×
error
8
+ next
7
|
d f irst
if
f irst
8
2
then
error
( "Non-octal digit" )
up f irst
Figure 7.5: Grammar with cloned productions.
7.2.2 Rule Cloning
Our first approach observes that a similar sequence of input symbols—a string
of digits—should be treated di
erently depending on context. Following that
observation, we can clone the productions in the grammar to derive similar
syntax but with di
ff
erent semantic actions. We therefore construct two kinds
of digit strings, one derived fromOctDigs and the other derived fromDecDigs,
to obtain the grammar and semantic actions shown in Figure 7.5. Rules 4 and 5
interpret strings of digits base-10; Rules 6 and 7 generate the same syntactic
strings but interpret their meaning base-8. Moreover, the separation of octal
and decimal digit recognition allows checks in the semantic actions of Rules 6
and 7 that the octal digits are in the proper range.
With this example, we see that grammars are modified for reasons other
than the parsing issues raised in Chapters 4, 5, and 6. Often, a translation task
can become significantly easier if the grammar can be modified to accommo-
date convenient flow of semantic information.
Rule cloning is an improvement over our initial attempt at syntax-directed
translation for this example. However, rule cloning cloning inflates a gram-
ff
 
Search WWH ::




Custom Search