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



Exercises on Flex

Free Radix
The scanner we described knows four different input radices for numbers: decimal, binary, octal, and hexadecimal. M4 supports a fifth mode for arbitrary radix between 2 and 36: 0rradix:number. Implement this mode in ylscan.l.
C Source Statistics
Using Flex, implement a clone of csize, a simple program performing statistics on C sources. For instance running it on the Yleval module gives:
          $ csize -h yleval.h yleval.c ylscan.c ylparse.h ylparse.c
           total    blank lines w/   nb, nc    semi- preproc. file
           lines    lines comments    lines   colons  direct.
          ------+--------+--------+--------+--------+--------+----
              69       10       27       33        8        9 yleval.h
             237       35       37      165       60       13 yleval.c
            1730      296      279     1185      391      303 ylscan.c
              54        7        1       47        7       36 ylparse.h
            1290      178      237      915      288      279 ylparse.c
            3380      526      581     2345      754      640 total
          

C Strings
Extend the previous program with statistics on C strings.
Beswitch
We have already studied keyword recognizers, see Looking for Keywords. In particular, while Gperf is based on hash tables, we showed how a similar application could be based on a cascade of switch. Implement one such program, beswitch, thanks to Flex. beswitch must be a drop-in replacement of Gperf.

Your scanner must be better than that of Gperf, in particular, it shall not be confused by /* %} */ or " %} " in the prologue. You are likely to need several start conditions, such as C_CODE, PROLOGUE, COMMENT, STRING, etc. The simple BEGIN will no longer be sufficient, and you will probably need some form of sub-scanner recursive calls: see %option stack, yy_stack_push, and yy_stack_pop in (FIXME: Flex ref.).