home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_02 / v7n2031a.txt < prev    next >
Text File  |  1988-12-04  |  2KB  |  69 lines

  1. /*
  2.  *  lexgen.c - Portable program to GENerate LEXical tables.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #if __STDC__            /* if ANSI C compiler   */
  9. #include <limits.h>
  10. #endif
  11.  
  12. #ifndef UCHAR_MAX       /* define it ourselves as last resort. */
  13. #define UCHAR_MAX   ((unsigned)255)
  14. #endif
  15.  
  16. #include "chclass.h"
  17.  
  18. #define OUTFILE "cctable.c"
  19. #define UALPHA  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  20. #define LALPHA  "abcdefghijklmnopqrstuvwxyz"
  21. #define DIGIT   "0123456789"
  22.  
  23. char    _cctable[1];        /* dummy to satisfy extern in chclass.h */
  24.  
  25.  
  26. main(int argc, char **argv){
  27.     unsigned char *gentab = (unsigned char *)calloc(UCHAR_MAX+1, sizeof(char));
  28.     void    settab(unsigned char *table, int bit, char *chars);
  29.     void    prntab(unsigned char *table, FILE *fout);
  30.     FILE    *fout;
  31.  
  32.     if( (fout = fopen(OUTFILE, "w")) == NULL){
  33.         fprintf(stderr, "%s: Can't open '%s' for output\n", argv[0], OUTFILE);
  34.         exit(EXIT_FAILURE);
  35.         }
  36.     
  37.     settab(gentab, _CCID1_,     UALPHA LALPHA "_");
  38.     settab(gentab, _CCID_,      UALPHA LALPHA "_" DIGIT);
  39.     settab(gentab, _CCEXP_,     "eE");
  40.     settab(gentab, _CCSIGN_,    "-+");
  41.     prntab(gentab, fout);
  42.     fclose(fout);
  43. }
  44.  
  45. void    settab(unsigned char *table, int  bit, char *chars) {
  46.     while(*chars)
  47.         table[*chars++]   |= bit;       /* turn on correct bit  */
  48. }
  49.  
  50. void    prntab(unsigned char *table, FILE *fout) {
  51.     unsigned    i;
  52.  
  53.     fprintf(fout, "\
  54. /*\n\
  55.  *  %s - character classification table.\n\
  56.  */\n\
  57. \n\
  58. char    _cctable[]  =   {\n\
  59. ",                                          OUTFILE);
  60.  
  61.     for(i = 0; i <= UCHAR_MAX; ++i) {
  62.         if(i)
  63.             fputs(",", fout);
  64.         fprintf(fout, (i%8) ? " " : "\n    ");
  65.         fprintf(fout, "0x%0X", table[i]);
  66.         }
  67.     fprintf(fout, "\n    };\n");
  68.     }
  69.