[Overview] [Previous] [Next]

Implementing a DFA, part 2

If you are not allowed to use a go to statement, you can fake it with a combination of a loop and a case statement:
state := q0;
loop
   case state of
      q0 :  read char;
            if eof then accept string;
            if char = 0 then state := q2;
            if char = 1 then state := q1;
      
      q1 :  read char;
            if eof then reject string;
            if char = 0 then state := q3;
            if char = 1 then state := q0;
      
      q2 :  read char;
            if eof then reject string;
            if char = 0 then state := q0;
            if char = 1 then state := q3;
      
      q3 :  read char;
            if eof then reject string;
            if char = 0 then state := q1;
            if char = 1 then state := q2;
   end case;
end loop;


Copyright © 1996 by David Matuszek
Last modified Jan 29, 1996