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

  1. /*          LISTING 6. DFA.C                        */
  2. /* Deterministic Finite State Automaton (DFA)         */
  3. /* Traverse a DFA state table, checking for target  */
  4. /*      tokens. If the token matches the target,    */
  5. /*     the pass (action) fcn is invoked. If not,   */
  6. /*    the fail function is invoked. In all cases, */
  7. /*    the machine moves to the next state (table  */
  8. /*    indexed in state table)                    */
  9.  
  10. #include <stdio.h>
  11. #include <memory.h>
  12. #include "dfa.h"
  13.  
  14. short need_token;    // block tokenizer flag
  15. short linenbr;        // keep track of stt line
  16.  
  17. short StateMachine(
  18.     DFA_CLASS *o)       // ptr to dfa object
  19. {
  20.     int pass;           // token match or fail
  21.     int retval;         // return value
  22.     char *token;        // token to match against
  23.  
  24. // Loop through the state table;
  25. // Match tokens and call appropriate functions
  26. // Jump to next state until state == OUT
  27.  
  28.     need_token = YES;
  29.     pass = GOOD;
  30.  
  31.     do {
  32.         // Retrieve token from input buffer
  33.     // Don't get another token if target not hit
  34.     if (pass && need_token)
  35.         token = GET_TOKEN;
  36.  
  37.         // Turn on tokenizer if it was turned off
  38.     need_token = YES;
  39.  
  40.     // Match token against target desired in STT
  41.     pass = TARGET(token);
  42.  
  43.     // Execute the pass function if token matches
  44.     // Execute the fail function otherwise
  45.     if (pass)
  46.         retval = GOODFCN(token);
  47.         else
  48.             retval = BADFCN(token);
  49.  
  50.     if (retval == ERR)
  51.         break;
  52.     } while ((retval = NEXT_STATE(pass)) != OUT);
  53.  
  54.     return(retval);
  55. }
  56.                       
  57.