## 9.2.Finite State Machines

Definition 8.1 (Finite State Machine)

A FSM consists of a set of states S, a set of input symbols I, a set of end states E, a set of error states F, a action function A and a transaction function T.
S
a set of states and # of members of S < ∞, start state, and  S
I
a set of input symbols and # of members of I < ∞
E
a set of end states and E S
F
a set of error states and F S and E F = Ø
A
a action function A with A: S × I ``actions'' A( state, input symbol ) action
T
a function T with T: S × I S new_state = T( state, input symbol )

The tuple (S, I, E, F, T, A) is called a FSM.

FSM can be used to recognize patterns.

For example:

```@ s sub 0 @    = 0
S             = { 0, 1, 2, 3, 4, 5, 6 }
I             = { , '0'-'9', 'a'-'z',  'A'-'Z', whiteSpace }
E             = { 6 }
F             =
A             A(6,  )        = 'print "I found ``plan 9'' and exit
A(?, ? )       = do nothing for other input symbols and states
T             T(0, 'p' )     = 1
T(1, 'l' )     = 2
T(2, 'a' )     = 3
T(3, 'n' )     = 4
T(4,' ')       = 5
T(5, '9' )     = 6
for all other input symbols and states:
T(i, s )       = 0
```

A graphical representation:

• = 0 The FSM detects also ``grumbleplan 9''!

Next iteration: = -1 The FSM doesn't detect ``  plan 9 .  wordSeperator.

Next iteration: = -1 The FSM detects `` plxplan 9 .  wordSeperator. Next iteration: = -1 The FSM detects only `` plan 9 .  wordSeperator.

The definition ot the FSM:

```S             = { -1, 0, 1, 2, 3, 4, 5, 6, 7 }
@ s sub 0 @   = -1
I             { x | x is a character }
E             { 7 }
F
A             A(7,  }        = print Heuraka and exit.
A(?, ? }       = do nothing
T             T(-1, WS)      = 0
T(-1, x)       = -1      for x  wordSeperator
T(0, WS)       = 0
T(0, 'p')      = 1
T(1, 'l')      = 2
T(1, x)        = -1      for x  I \ { 'l' }
T(2, 'a')      = 3
T(2, x)        = -1      for x  I \ { 'a' }
T(3, 'n')      = 4
T(3, x)        = -1      for x  I \ { 'n' }
T(4, ' ')      = 5
T(4, x)        = -1      for x  I \ { ' ' }
T(5, '9')      = 6
T(5, x)        = 0       for x  I \ wordSeperator
T(5, x)        = -1      for x  I \ { '9' }
T(6, WS)       = 7
T(6, x)        = -1      for x  I \ wordSeperator
```

Define a FSM which recognizes sequences of letters that have subsequences

aeiou.

For example:

```--   adventitious
--   facetious
--   sacrilegious
```

A graphical representation: The definition:

```@ s sub 0 @   = 0
S             = { 0, 1, 2, 3, 4, 5, 7, 8 }
I             = { x | x is a character }
E             = { 7 }
F             = { 8 }
A             A(7,  }        = print the word
A(8,  }        = exit
A(1, x )       = store x,          for x  I \ { 'a' }  wordSeperator
A(2, x )       = store x,          for x  I \ { 'e' }  wordSeperator
A(3, x )       = store x,          for x  I \ { 'i' }  wordSeperator
A(4, x )       = store x,          for x  I \ { 'o' }  wordSeperator
A(5, x )       = store x,          for x  I \ { 'u' }  wordSeperator
A(6, x )       = store x,          for x  I \ wordSeperator
T             T(0, WS)       = 0
T(0, x)        = 1       for x  I \ { 'a' }
T(0, 'a')      = 2
T(1, 'a')      = 2
T(1, x)        = 1       for x  I \ { 'a' }
T(1, x)        = 8       for x  wordSeperator
T(2, 'e')      = 3
T(2, x)        = 1       for x  I \ { 'e' }
T(2, x)        = 8       for x  wordSeperator
T(3, 'i')      = 4
T(3, x)        = 1       for x  I \ { 'i' }
T(3, x)        = 8       for x  wordSeperator
T(4, 'o')      = 5
T(4, x)        = 1       for x  I \ { 'o' }
T(4, x)        = 8       for x  wordSeperator
T(5, 'u')      = 6
T(5, x)        = 1       for x  I \ { 'u' }
T(5, x)        = 8       for x  wordSeperator
T(6, x)        = 7       for x  wordSeperator
T(6, x)        = 1       for x  I \ wordSeperator
```

• Draw one FSM for the following problem:

Find the words

```--   Kenya
--   Kansas
--   Kauder
--   Kunta Kinte
```
in an input stream.

• Draw one FSM for the following problem:

Find the words with

```--   the first letter must be an 'p'
--   the second letter must be an 'r'
--   the third letter can be an 'o' or 'e'
--   the rest ot the word is 'klap'
```

in an input stream.

• Draw one FSM for the following problem:

Find the words with the substring

```--   Yps and
--   Ilon
```

in an input stream.