Floating Point Constants in Java. Construct a DFA that
recognizes exactly the legal floating point constants in
Java and C, as shown by the example program below.
This should ignore the optional trailing "f" or "F" for "float"
constants and the optional trailing "d" or "D" for "double"
constants. (With no optional trailing letter the constant is
double by default.) It shouild also ignore the limitation on the size
of the exponent, so the last illegal constant in the program
below would be accepted by the DFA. (This is fairly complicated
and needs to be done with care.)
// Doub: try out double constants
public class Doub {
public static void main(String[] args) {
System.out.println("-1.2e-2 = " +(-1.2e-2)); // normal
System.out.println("-.2e-2 = " +(-.2e-2)); // no digit before .
System.out.println("-1.e-2 = " +(-1.e-2)); //no digit after .
System.out.println("-1e-2 = " +(-1e-2)); // no .
System.out.println("-1.2E-2 = " +(-1.2E-2)); // cap E
System.out.println("-1.2 = " +(-1.2)); // no e or E
System.out.println("-.2 = " +(-.2)); // no e, no digit before .
System.out.println("-1. = " +(-1.)); // no e, no digit after .
// same as above without signs
System.out.println("1.2e2 = " +(1.2e2)); // normal
System.out.println(".2e2 = " +(.2e2)); // no digit before .
System.out.println("1.e2 = " +(1.e2)); //no digit after .
System.out.println("1e2 = " +(1e2)); // no .
System.out.println("1.2E2 = " +(1.2E2)); // cap E
System.out.println("1.2 = " +(1.2)); // no e or E
System.out.println(".2 = " +(.2)); // no e, no digit before .
System.out.println("1. = " +(1.)); // no e, no digit after .
// System.out.println("-.e-2 = " +(-.e-2)); // only ., ILLEGAL
System.out.println( // too many digits
"-33333333333333333333.2222222222222222222222222e-2 = "
+(-33333333333333333333.2222222222222222222222222e-2));
// System.out.println("-1.0e-444 = " +(-1.0e-444)); // ILLEGAL
}
}
Output:
% java Doub
-1.2e-2 = -0.012
-.2e-2 = -0.0020
-1.e-2 = -0.01
-1e-2 = -0.01
-1.2E-2 = -0.012
-1.2 = -1.2
-.2 = -0.2
-1. = -1.0
1.2e2 = 120.0
.2e2 = 20.0
1.e2 = 100.0
1e2 = 100.0
1.2E2 = 120.0
1.2 = 1.2
.2 = 0.2
1. = 1.0
-33333333333333333333.2222222222222222222222222e-2 = -3.3333333333333331E17
The two commented out constants produced error messages:
Doub.java:13: illegal start of expression
System.out.println("-.e-2 = " +(-.e-2));
^
Doub.java:26: floating point number too small
System.out.println("-1.0e-444 = " +(-1.0e-444));
^