Java Reference
In-Depth Information
1 Start
Num ans
$
call
print
( ans )
2 Num ans
x SetBase Digs baseans
ans baseans
3
|
SetBaseTen Digs decans
ans decans
4 SetBase
d val
base val
5 SetBaseTen
→ λ
base
10
6 Digs up
Digs below
d next
if next base
then
3
( "Digit outside allowable range")
up below × base + next
error
7
|
d f irst
if f irst base
then
4
error
( "Digit outside allowable range")
up f irst
Figure 7.7: Strings with an optionally specified base.
This grammar avoids copying the productions that generate a string of digits;
instead, the grammar assigns and references a global variable ( base )asa side-
e ff ect of processing an o (Rule 4) or
λ
(Rule 5).
We expand our example yet again, using the terminal x to indicate that the
next digit is the base to which the subsequent digits should be interpreted. If
no x is present, then the digits should be interpreted base-10, as before. Some
examples of legal inputs and their interpretation are as follows:
Input
Meaning Value (base-10)
431$
431 10
431
x8431$
431 8
281
x5431$
431 5
116
A grammar for the new language is shown in Figure 7.7. Unit- and
λ
-
productions are introduced to set the global variable base properly.
Search WWH ::




Custom Search