Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sample code for finite state machine 1

Status
Not open for further replies.

sedawk

Programmer
Feb 5, 2002
247
US
Hi,

Where can I get a neat sample code for finite state machine that will be used in a lexicon scanner? Did a goole but too much text, no immediate codes found. So seek help from you guys. Maybe you can put it on FAQ.

Thanks.
 
This is how we used to do it in the early days before lex existed
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* First the events */
enum LexType
{
   eIll, /* Illegal char */
   eNum, /* Number */
   eAlf, /* Alphabetic */
   eSpc, /* Whitespace */
   eEof, /* End of file */
   eMax
};

/* State/Event lookup - assumes ASCII */
enum LexType lookup[] =
{
   eEof, eIll, eIll, eIll, eIll, eIll, eIll, eIll, /* 00-07 */
   eIll, eIll, eSpc, eIll, eIll, eSpc, eIll, eIll, /* 08-0F */
   eIll, eIll, eIll, eIll, eIll, eIll, eIll, eIll, /* 10-17 */
   eIll, eIll, eEof, eIll, eIll, eIll, eIll, eIll, /* 18-1F */
   eSpc, eIll, eIll, eIll, eIll, eIll, eIll, eIll, /* 20-27 */
   eIll, eIll, eIll, eIll, eIll, eIll, eIll, eIll, /* 28-2F */
   eNum, eNum, eNum, eNum, eNum, eNum, eNum, eNum, /* 30-37 */
   eNum, eNum, eIll, eIll, eIll, eIll, eIll, eIll, /* 38-3F */
   eIll, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, /* 40-47 */
   eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, /* 48-4F */
   eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, /* 50-57 */
   eAlf, eAlf, eAlf, eIll, eIll, eIll, eIll, eIll, /* 58-5F */
   eIll, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, /* 60-67 */
   eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, /* 68-6F */
   eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, eAlf, /* 70-77 */
   eAlf, eAlf, eAlf, eIll, eIll, eIll, eIll, eIll  /* 78-7F */
};

FILE* srcFile;
char srcBuff[128];/* source buffer */
char* src;
int  srcIx;       /* position in buffer */
int  srcMax;      /* size of buffer */

/* Initialize */
void SrcInit (int argc, const char* argv[])
{
   if (argc < 2)
   {
      printf ("Input File required\n");
      exit (0);
   }
   srcFile = fopen (argv[1], "r");
   if (srcFile == NULL)
   {
      printf ("Unable to open %s\n", argv[1]);
      exit (0);
   }
   /* Set conditions for kicking something into the buffer */
   srcIx = 1;
   srcMax = 0;
}
/* Get the current character */
char SrcGet ()
{
   return src[srcIx];
}
/* Get the next character */
char SrcNext ()
{
   if (++srcIx >= srcMax)
   {
      src = fgets (srcBuff, 128, srcFile);
      if (!src)
      {
         /* Mark EOF */
         src = srcBuff;
         src[0] = 0;
      }
      srcIx = 0;
      srcMax = strlen (src);
   }

   /* (char) 0 used to denote EOF */
   return src[srcIx];
}

void SkipToNextWord ()
{
   char ch;

   ch = SrcGet ();
   while (lookup[ch] == eIll || lookup[ch] == eSpc)
      ch = SrcNext ();
}
/* Get the next word */
void SrcNextWord (char* word, enum LexType lextok)
{
   int wordLen;
   char ch;
   /* Assume the buffer is big enough */

   /* Get the word */
   wordLen = 0;
   ch = SrcGet ();
   while (lookup[ch] == lextok)
   {
      word[wordLen++] = ch;
      ch = SrcNext ();
   }
   word[wordLen] = '\0';
}
int main(int argc, const char* argv[])
{
   char theWord[128];
   int  resume;

   SrcInit (argc, argv);
   SrcNext ();
   resume = 1; /* true */
   do {
      SkipToNextWord ();
      switch (lookup[SrcGet()])
      {
      case eAlf:
         SrcNextWord (theWord, eAlf);
         printf ("Word %s\n", theWord);
         break;

      case eNum:
         SrcNextWord (theWord, eNum);
         printf ("Number %s\n", theWord);
         break;

      case eEof:
         resume = 0;
         break;
      }
   } while (resume);
	return 0;
}
 
so this approach is not a strictly state machine. anyway, thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top