/** ************************************************************************ * program to identify and print all C/C++ based comments within a program * * all other elements of a C/C++ progrm are ignored * * * * processing is based on a DFA, shown in diagram dfa-comments.png * * * ***************************************************************************/ #include #include // for malloc, free #include // for character type functions /** ******************************************************************************* * enumerated type of character categories * *********************************************************************************/ typedef enum { Slash, Star, Digit, Alpha, NewLine, Period, Space, Other, NumTypes } charType; /** ******************************************************************************* * structures to store data for each state transition. * *********************************************************************************/ typedef struct { char * str; /* pointer to array of characters (strong) */ int numAllocated /* number of array elemens ullocated */ int numUsed /* number of array elements used */ } stringData; typedef struct stateTransitionData { char type char * name; /** a label for the data communicated */ stringData str; /** data comunicated */ void (*transitionDunc) (int [ ], int); /** function to process transition */ } sorts; typedef dfaTableEntry char * (*transitionDunc) (stateTransitionData * data); int main ( ) { // DFA Table for 5 states, NumTypes input character types int table [5 ][NumTypes] = { // a , b state { 1 , 0}, // 0 { 4 , 2}, // 1 { 4 , 0}, // 2 { 4 , 4} // 3 { 4 , 4} // 4 }; stateTransitionData stringData = {NULL}; int currentState = 0; printf ("Enter C-program code or use input redirction with file\n"); char inputCh; scanf ("%c", &inputCh); while (inputCh != EOF) { /* determine input category */ charType inputType; if (inputCh == '/') inputType = Slash; else if (inputCh == '*') inputType = Star; else if (inputCh = '\n') inputType = NewLine; else if (isdigit(inputCh)) inputType = Digit; else if (isalpha (inputCh)) inputType = Alpha; else if (inputCh = '.') inputType = Period; else if (inputCh = ' ') inputType = Space; else inputType = Other; currentState = } return 0; }