home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dec92.zip / 1012098B < prev    next >
Text File  |  1992-05-26  |  4KB  |  200 lines

  1. /****************************************************/
  2. /*    LISTING 2. SYNDOS.C                         */
  3. /* Syntactically parse a testfile of restricted DOS */
  4. /*      pathanames. Use the DFA state machine and   */
  5. /*    the state table defined in dostbl1.h        */
  6. /* Print out each component after parsing        */
  7. /* Parsing rule:   a: (\ax*)+ (.x)(x)(x) e        */
  8. /*                                                  */
  9. /*  Copyright August 29, 1991 by Alan Cline        */
  10. /*      Carolla Development, Dublin, Ohio           */
  11. /****************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include "dfa.h"
  18. #include "dostbl1.h"
  19.  
  20. #define TESTFILE    "testfile.dat"
  21. #define DELIMSTR    ":.\\"
  22.  
  23. // Global storage to test parsing
  24. char drive;
  25. char pathname[MAXBUFLEN];
  26. char fname[9];
  27. char ext[5];
  28. char *tokeptr;
  29. char tokebuf[15];
  30.  
  31. short reset(void);
  32.  
  33.  
  34. void main(void)
  35. {
  36.     DFA_CLASS syndos_dfa;
  37.     FILE *fp;
  38.     short retval = OK;
  39.  
  40.     // Open the test names file
  41.     fp = fopen(TESTFILE, READ_TEXT);
  42.  
  43.     while (!feof(fp) && retval != ERR) {
  44.         // Init each component variable
  45.         drive = EMPTY;
  46.         pathname[0] = EMPTY;
  47.         fname[0] = EMPTY;
  48.         ext[0] = EMPTY;
  49.         tokeptr = memset(tokebuf, 0, sizeof(tokebuf));
  50.  
  51.         // Initialize the DFA and begin parsing
  52.         // Pass dfa, state table, override-tokenizer,
  53.         //    input file, delimeters
  54.         InitStateMachine(&syndos_dfa, &syndos_stt,
  55.                     NULL, fp, DELIMSTR);
  56.  
  57.         retval = StateMachine(&syndos_dfa);
  58.     }
  59.  
  60.     // End test run; close file and quit
  61.     fclose(fp);
  62.     return;
  63. }
  64.  
  65. /*********************************/
  66. /* Application-specific routines */
  67. /*********************************/
  68.  
  69.  
  70. // Define the character-oriented tokenizer
  71. char *get_token(
  72.     DFA_CLASS *o)
  73. {
  74.     *o->wp = (char) getc(INFILE);
  75.  
  76.     // force EOR as front token
  77.     if (*o->wp == EOR) {
  78.         if (o->cp != (int) EOR){
  79.             ungetc((int) *o->wp, INFILE);
  80.             o->cp = (int) EOR;
  81.         }
  82.         else
  83.             o->cp = 0;
  84.     }
  85.  
  86.     return(o->wp);
  87. }
  88.  
  89.  
  90. /***************************/
  91. /* DEFAULT TARGET ROUTINES */
  92. /***************************/
  93.  
  94. // Check if the character is alphabetic
  95. short alpha(
  96.     char *token)
  97. {
  98.     return(isalpha((int) token[0]));
  99. }
  100.  
  101. // Check if the character is alphanumeric
  102. short alphanum(
  103.     char *token)
  104. {
  105.     return(isalnum((int) token[0]));
  106. }
  107.  
  108. // Check if character is a dot
  109. short isdot(
  110.     char *token)
  111. {
  112.     return(*token == '.');
  113. }
  114.  
  115.  
  116. /***************************/
  117. /* DEFAULT ACTION ROUTINES */
  118. /***************************/
  119.  
  120. // Move a character into the work buffer
  121. // then increment the ptrs.
  122. short pushchar(
  123.     char *token)
  124. {
  125.     *tokeptr = token[0];
  126.     tokeptr++;
  127.     *tokeptr = '\0';
  128.     return(1);
  129. }
  130.  
  131. // NULL terminate workbuffer;
  132. // Return ptr to start of workbuf
  133. short reset(void)
  134. {
  135.     tokeptr = memset(tokebuf, 0, sizeof(tokebuf));
  136.     return(1);
  137. }
  138.  
  139.  
  140. /*****************************************/
  141. /* Application-specific action routines  */
  142. /*****************************************/
  143.  
  144. // Save the drive letter tokenized;
  145. // Reset the dfa workbuffer
  146. short appendpath(
  147.     char *token)
  148. {
  149.     strncat(pathname, tokebuf, strlen(tokebuf));
  150.     strncat(pathname, token, 1);
  151.     reset();
  152.     return(1);
  153.  
  154. }
  155.  
  156. // Print out each component of the parsed pathname
  157. short cleanup(
  158.    char *token)
  159. {
  160.     IGNORE(token);
  161.     printf("File: %s\n\tDrive: %c\n",
  162.             fname, drive);
  163.     printf("\tPath: %s\n\tExtension: %s\n",
  164.             pathname, ext);
  165.     return(1);
  166. }
  167.  
  168. // Save the drive letter
  169. short savedrive(
  170.     char *token)
  171. {
  172.     IGNORE(token);
  173.     drive = (toupper((int) tokebuf[0]));
  174.     reset();
  175.     return(1);
  176. }
  177.  
  178. // Save the file extension
  179. short saveext(
  180.     char *token)
  181. {
  182.     IGNORE(token);
  183.     strcpy(ext, tokebuf);
  184.     reset();
  185.     return(1);
  186. }
  187.  
  188. // Save the filename
  189. short savefname(
  190.     char *token)
  191. {
  192.     IGNORE(token);
  193.     strcpy(fname, tokebuf);
  194.     reset();
  195.     return(1);
  196. }
  197.  
  198.  
  199.  
  200.