#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define debug(fmt,args,...) fprintf(stderr,fmt,##args)
int get_input_type(char * word)
{
if(strcmp(word," ") == 0)
return 0;
if(strcmp(word,"\t") == 0)
return 0;
if(strcmp(word,"#") == 0)
return 1;
if(strcmp(word,"define") == 0)
return 2;
if(strcmp(word,"\n") == 0)
return 4;
return 3;
}
char c;
char word[64];
char word_buf[64];
char buf[128];
struct macro
{
char name[64];
char value[64];
} macros[16];
int macro_counter = 0;
void act_print_word(void)
{
int i = 0;
for(i=0;i<macro_counter;i++)
{
if(strcmp(word,macros[i].name) == 0)
{
printf("%s",macros[i].value);
return;
}
break;
}
if(i == macro_counter)
printf("%s",word);
return;
}
void act_save_to_buf(void)
{
strcat(buf,word);
return;
}
void act_print_buf_and_word(void)
{
printf("%s",buf);
printf("%s",word);
return;
}
void act_save_word(void)
{
strcat(word_buf,word);
return;
}
void act_get_macro_name(void)
{
printf("name = <%s>\n",word_buf);
strcpy(macros[macro_counter].name,word_buf);
strcpy(word_buf,"");
return;
}
void act_get_macro_value(void)
{
printf("value = <%s>\n",word_buf);
strcpy(macros[macro_counter].value,word_buf);
strcpy(word_buf,"");
macro_counter++;
return;
}
void act_null(void)
{
return;
}
enum {s0 = 0,s1,s2,s3,s4,s5,s6};
int state_transition[7][5] =
{
s0, s1, s0, s0, s0,
s1, s0, s2, s0, s0,
s3, s0, s0, s0, s0,
s3, s4, s4, s4, s0,
s5, s5, s4, s4, s0,
s5, s6, s6, s6, s0,
s6, s6, s6, s6, s0,
};
#define a0 act_print_word
#define a1 act_save_to_buf
#define a2 act_print_buf_and_word
#define a3 act_save_word
#define a4 act_get_macro_name
#define a5 act_get_macro_value
#define a6 act_null
#if 1
typedef void (*PF)(void);
PF act_table[7][5] =
{
a0, a1, a0, a0, a0,
a1, a2, a1, a2, a2,
a1, a2, a2, a2, a2,
a1, a3, a3, a3, a2,
a4, a4, a3, a3, a5,
a6, a3, a3, a3, a5,
a3, a3, a3, a3, a5,
};
#endif
void getword(char * word)
{
char c;
c = getchar();
if(c == EOF)
{
*word = '\0';
return;
}
// if c is not a alpha
if(isalpha(c) == 0) // or if(!isalpha(c))
{
*word = c;
word ++;
*word = '\0';
return;
}
do
{
*word++ = c;
c = getchar();
}while(isalnum(c) || c == '_');
// current c is $ or anything else
ungetc(c,stdin);
*word = '\0';
return;
}
int main(void)
{
int state = 0;
while(1)
{
int input = 0;
void (*pf)(void);
//char word[64];
//c = getchar();
//input = get_input_type(c);
getword(word);
input = get_input_type(word);
//printf("c = %c,input = %d\n",c,input); it's debug sentence.
//if(c == EOF)
// break;
if(strcmp(word,"") == 0)
break;
pf = act_table[state][input];
pf();
state = state_transition[state][input];
debug("word = <%s>,input = %d,state = %d\n",word,input,state);
}
return 0;
}