Overview: This recitation has two parts: First, to finish handling a double at the lexical level by converting it into an internal double constant. Second, the recitation includes a number of problems about formal grammars to familiarize you with this topic. Floating point constants: double at the lexical level: This is the most complex token in common languages. You are to convert to an internal double "from scratch" while processing the input double character-at-a-time, using your program from Recitation 2. Thus, unlike in Recitation 2, you will not output the characters in the recognized double, but will convert to an internal double type and will then output that double as a check on your work. Hints on calculating the value of a scanned double: In C it would be possible to use a library function to convert a string of characters representing a floating point number to an actual double. You could use sscanf for example.Java also has library functions (such as Double.parseDouble(String)) that must not be used. In ordinary programming it is usually a good idea to use library functions rather than rewrite them from scratch. In this recitation, however, we are studying some of the low-level language mechanisms, and for this assignment you are not to use a library function. Instead, you should realize that given the code: char ch = '4'; int i = ch -'0';the variable i takes on the integer value 4. (The same trick above also works in Java.) At each stage of reading the initial digits of the constant, you can multiply by 10 and add in the next integer value. Then you have to take the "." and the exponent part appropriately into account. You should not use the pow function in C or the static Math.pow() function in Java in order to handle the exponent, but you should handle this "from scratch" also. (You don't need the full power of pow, but just some multiplies or divides by 10. You can test your program with the same list of numbers used in the previous recitation:
11.5e+2 11.75E2 11.75 .75 .23 .84e-2 666 345.578e5 1234.5678 .75e2 Raw number: 75.000000, combined Exponent: 0, value:"75.000000" 11.5e-2 Raw number: 115.000000, combined Exponent: -3, value:"0.115000" 11.5e+2 Raw number: 115.000000, combined Exponent: 1, value:"1150.000000" 11.75E2 Raw number: 1175.000000, combined Exponent: 0, value:"1175.000000" 345.578e5 Raw number: 345578.000000, combined Exponent: 2, value:"34557800.000000" Problems on Formal Grammars:
What you should submit: Refer to the submissions directions and to deadlines at the top of this page. If two people are working together, be sure that both signed in as a pair and that both names appear at the top of the submission.
Revision date: 2013-01-27. (Please use ISO 8601, the International Standard.) |