[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
R4RS number syntax
Here is the new number syntax for R4RS. I wrote a reference
parser for it and found one minor glitch, which I don't want
to try to fix. It seems to take about three hundred lines
of commented Scheme code to parse numbers while building the
correct value and exactness.
Some examples of numbers:
#E#o-1234
#X#I-50
#e#x-00abc/34
#e#d-1###.###L-06@-.4##f+0000003
4+5i
+45i
+i
-i
00## (this is the glitch I found; see below)
Some examples of non-numbers:
-#x1 (sign must follow radix and exactness prefixes)
1/2e4 (exponents not allowed with ratio syntax)
#b101e3 (exponents illegal if radix is not decimal)
#x3.4 (decimal point illegal if radix is not decimal)
45i (sign required in front of imaginary part)
i (sign required)
3#.4 (insouciant digits can't be followed by souciant)
## (an insouciant digit can't be the first)
Some examples of semantics:
1e6 is equivalent to 1000000.0 and should be inexact
#e1e6 is equivalent to 1000000
#i3/4 is equivalent to .75
3/4 is equivalent to (/ 3 4)
4+0i is equivalent to 4
4-6.3i is equivalent to (make-rectangular 4 -6.3)
123@.41 is equivalent to (make-polar 123 .41)
1### is equivalent to 1000.0
00## is equivalent to 0.0
5. is equivalent to 5.0
Peace, Will
Number syntax for R4RS
<number> --> <num 2> | <num 8> | <num 10> | <num 16>
The following rules for <num R>, <complex R>, <real R>, <ureal R>,
<uinteger R>, and <prefix R> should be replicated for R = 2, 8, 10,
and 16. There are no rules for <decimal 2>, <decimal 8>, and
<decimal 16>, which means that numbers containing decimal points
or exponents must be in decimal radix.
<num R> --> <prefix R> <complex R>
<complex R> --> <real R>
| <real R> @ <real R>
| <real R> + <ureal R> i
| <real R> - <ureal R> i
| <real R> + i
| <real R> - i
| + <ureal R> i
| - <ureal R> i
| + i
| - i
<real R> --> <sign> <ureal R>
<ureal R> --> <uinteger R>
| <uinteger R> / <uinteger R>
| <decimal R>
<decimal 10> --> <uinteger 10> <suffix>
| . <digit 10>+ #* <suffix>
| <digit 10>+ . <digit 10>* #* <suffix>
| <digit 10>+ #* . #* <suffix>
<uinteger R> --> <digit R>+ #*
<prefix R> --> <radix R> <exactness>
| <exactness> <radix R>
<suffix> --> <empty>
| <exponent marker> <sign> <digit>+
<exponent marker> --> e | s | f | d | l
<sign> --> <empty> | + | -
<exactness> --> <empty> | #i | #e
<radix 2> --> #b
<radix 8> --> #o
<radix 10> --> <empty> | #d
<radix 16> --> #x
<digit 2> --> 0 | 1
<digit 8> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7
<digit 10> --> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<digit 16> --> <digit 10> | a | b | c | d | e | f