#include #include enum { State_Plain, State_OneSlash, State_Escaped, State_CXX_Comment, State_C_Comment, State_One_Star, State_Quoted, State_Quoted_Escaped, State_SingleQuoted, State_SingleQuoted_Escaped, }; #define trans(x) do { \ printf("Changing to %s\n", #x); \ state = x; \ } while (false); int main(void) { unsigned int state = State_Plain; char c; while ((c = getchar()) != EOF) { switch (state) { case State_Plain: switch (c) { case '/': trans(State_OneSlash); break; case '\\': trans(State_Escaped); break; case '\"': trans(State_Quoted); break; case '\'': trans(State_SingleQuoted); break; } break; case State_OneSlash: switch (c) { case '/': trans(State_CXX_Comment); break; case '*': trans(State_C_Comment); break; default: trans(State_Plain); break; } break; case State_Escaped: trans(State_Plain); break; case State_CXX_Comment: switch (c) { case '\n': trans(State_Plain); break; } break; case State_C_Comment: switch (c) { case '*': trans(State_One_Star); break; } break; case State_One_Star: switch (c) { case '/': trans(State_Plain); break; default: trans(State_C_Comment); break; } break; case State_Quoted: switch (c) { case '\\': trans(State_Quoted_Escaped); break; case '\"': trans(State_Plain); break; } break; case State_Quoted_Escaped: trans(State_Plain); break; case State_SingleQuoted: switch (c) { case '\\': trans(State_SingleQuoted_Escaped); break; case '\'': trans(State_Plain); break; } break; case State_SingleQuoted_Escaped: state = State_SingleQuoted; break; } } return 0; }