home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_10_12
/
1012106a
< prev
next >
Wrap
Text File
|
1992-05-26
|
2KB
|
57 lines
/* 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);
}