Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#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;
}