Noeud:Exercises on Gperf, Noeud « Previous »:Using Gperf with the GNU Build System, Noeud « Up »:Scanning with Gperf



Exercises on Gperf

In this section, we address some issues left opened by the previous section, Advanced Use of Gperf.

Testing
Write an Autotest test suite for your numeral module. Voir Software Testing with Autotest, for all the details on designing and implementing test suites
Overflow
Augment the previous algorithm with overflow detection.
Non Standard Numbers
The algorithm presented above produces invalid results when the numbers are presented in a perfectly human understandable form, but nonstandard. For instance 2 000 2 000 000 000 is to be said "two trillion two billion" but people would understand "two trillion two thousand millions", which our module does not recognize properly:
          $ m4 -m numeral
          numeral(two trillion two thousand millions)
          =>2000001002000
          

This phenomenon is the same as we already observed with hundreds, see Advanced Use of Gperf. Hint: a stack might be helpful.

Invalid Numbers
Try to diagnose invalid numbers, which humans would reject. For instance:
          $ m4 -m numeral
          numeral(forty-eleven)
          =>51
          

Hint: without an actual grammar (FIXME: ref to Bison.), it might not be possible, or at least, extremely clumsy.