/* dfa.c - Don Yang (uguu.org) Strip C comments using deterministic finite automaton. How this works is fairly obvious once you draw out the diagram. Each array element in dfa[] is a state, and each word in the string are the edges: First character in word desinates the character to match (. = any) The letters are the actions when a character matches s = write space e = echo character f = write forward slash A digit ends an edge definition, and is the next state to go to. 06/15/01 */ #include #include static char *dfa[] = { /* state 0 */ "/1 'e4 \"e6 .e0", /* state 1 */ "*ss2 /f1 .fe0", /* state 2 */ "*s3 .s2", /* state 3 */ "*s3 /s0 .s2", /* state 4 */ "'e0 \\e5 .e4", /* state 5 */ ".e4", /* state 6 */ "\"e0 \\e7 .e6", /* state 7 */ ".e6"}; static int state = 0; static int input; static char *cursor; int main(void) { while( (input = getchar()) != EOF ) { /* Find edge with matching character */ for(cursor = dfa[state]; *cursor; cursor++) { if( *cursor == ' ' ) continue; if( *cursor == '.' || (int)(*cursor) == input ) break; for(; !isdigit(*cursor); cursor++); } /* Execute commands */ for(; !isdigit(*cursor); cursor++) { if( *cursor == 's' ) putchar(input < ' ' ? input : ' '); if( *cursor == 'e' ) putchar(input); if( *cursor == 'f' ) putchar('/'); } state = *cursor - '0'; } return 0; }