/*          LISTING 6. DFA.C                        */
/* Deterministic Finite State Automaton (DFA) 	    */
/* Traverse a DFA state table, checking for target  */
/*      tokens. If the token matches the target,    */
/* 	the pass (action) fcn is invoked. If not,   */
/*	the fail function is invoked. In all cases, */
/*	the machine moves to the next state (table  */
/*	indexed in state table)		            */

#include <stdio.h>
#include <memory.h>
#include "dfa.h"

short need_token;	// block tokenizer flag
short linenbr;		// keep track of stt line

short StateMachine(
    DFA_CLASS *o)       // ptr to dfa object
{
    int pass;           // token match or fail
    int retval;         // return value
    char *token;		// token to match against

// Loop through the state table;
// Match tokens and call appropriate functions
// Jump to next state until state == OUT

    need_token = YES;
    pass = GOOD;

    do {
        // Retrieve token from input buffer
	// Don't get another token if target not hit
	if (pass && need_token)
	    token = GET_TOKEN;

        // Turn on tokenizer if it was turned off
	need_token = YES;

	// Match token against target desired in STT
	pass = TARGET(token);

	// Execute the pass function if token matches
	// Execute the fail function otherwise
	if (pass)
	    retval = GOODFCN(token);
    	else
            retval = BADFCN(token);

	if (retval == ERR)
	    break;
    } while ((retval = NEXT_STATE(pass)) != OUT);

    return(retval);
}
                      
