constructing and optimizing finite state machines