Listing 4: Example of the simple C/C++ source code comment filtering FSM

#include <stdio.h>
#include <iostream.h>
#include "FSM.h"
#include "Filter_eventhandler.h"

// Maximal number of transitions
const int MaxNumTransitions = 20;    

int main( int argc, char *argv[] )
{
 // Check the input arguments
 // not shown
 
 // Create an instance of a filter 
 // events handler (without error handling)
 Filter_EventHandler *ptrHandler = 0;    
 ptrHandler = new Filter_EventHandler(MaxNumTransitions,
                                      argv[1], 
                                      argv[2]);

 // Create an instance of the FSM
 FSM myFSM(ptrHandler, MaxNumTransitions, VALID);

 // Define the FSM's transitions
 myFSM.defineTransition(VALID, VALID, OTHER, Ind_writeChar);
 myFSM.defineTransition(VALID, VALID, NEWLINE, Ind_writeChar);
 myFSM.defineTransition(VALID, VALID, ASTERISK, Ind_writeChar);
 myFSM.defineTransition(VALID, WAIT_COMMENT, SLASH, Ind_ignoreChar);
 myFSM.defineTransition(WAIT_COMMENT, VALID, OTHER, Ind_writeSlash);
 myFSM.defineTransition(WAIT_COMMENT, GARBAGE1, SLASH,
                        Ind_ignoreChar);
 myFSM.defineTransition(GARBAGE1, VALID, INTERNAL_ERROR,
                        Ind_InternalError);

// not shown: the rest of 
// the define Transition() calls 
 ...
 int char_read, event;

 char_read = ptrHandler->getCharacter();
 while (char_read != EOF)
 {
    // Convert a character into an event
    switch (char_read)
    {
        case SLASH:
        case ASTERISK:
        case NEWLINE: event = char_read;
                      break;
        default: event = OTHER;
    }
    // execute transition and 
    // associated actions
    if (myFSM.control(event, (void *)&char_read) == FALSE)
       break;

    // Read a character from an input file
    char_read = ptrHandler->getCharacter();
 }

 // Clean up ...
 if ( ptrHandler )
    delete ptrHandler;

 return(0);
}
//End of File