DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
Using Simple Finite State Machines - Fsm(C++)

A Simple Translator

The next example is intended to show how an application might might make use of the integral values returned by action routines.

Consider the problem of changing the case of every lower case letter in the range 'a' through 'e' occurring in some character string. A machine to do this has the transition diagram of Figure 3:

Translator State Transition Diagram

In the following program, the first parameter name is omitted in no_change and make_upper to avoid the compiler warning about unused parameters:

       #include <Fsm.h>
       #include <iostream.h>
       #include <ctype.h>
       int no_change(Fsm&,unsigned input){
           return input;
       }
       int make_upper(Fsm&,unsigned input){
           return toupper(input);
       }
       char* translate(char* s,Fsm& t){
           char* p=s;
           while(*p!=0){
               *p=t.fire(*p);
               p++;
           }
           return s;
       }
       char string[]="$$ablewasI$$ereIsawElba$$";
       main(){
           Fsm t(1,0,no_change);  // "t" for "translator"
           t.trans(0,"[a-e]",0,make_upper);
           cout << translate(string,t) << "\ n";
       }

This program prints the string $$ABlEwAsI$$ErEIsAwElBA$$.


Next topic: A Simple Dynamically-Programmed Fsm
Previous topic: Acceptor State Transition Diagram

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005