Class notes for Friday 2/8/99 lecture
 by Chia-Li Yu

Topic:  Finite Automata
            Translate FA into real program
            Transition table

Finite Automata : Simple machines that match char strings
 

* Graphics:
-----------
  O denotes states
  -->O      start state
  Oo        final (accepting) state
  a
  --->      transition

* Examples of FA:

Ex1.
//(Not(EOL))* EOL
                         _Not(EOL)
                        ( )
-->O--->O--->O--->Oo
         /         /

Ex2.
(ab(c+))+

                                    _c
          a       b        c    (  )
-->O--->O--->O--->Oo
     (_______________)
                   a

The FA can exist independently or group together.
EX. '+'

           +                             
 -->O--->Oo
  independent exist

                         _Not(EOL)
                        ( )
-->O--->O--->O--->Oo
         /         /
group with Ex.1

(a) Deterministic Finite Automation (DFA)
     Every state has at most one transition for a given character
(b) Non-Deterministic Finite Automation  (NFA)
     (1) A state may have multiple transition for one character
     (2) A transition may have a label of Lambda

Ex.

(1)       '/' | '+'
                   +                         /
         -->O--->Oo       -->O--->Oo

(2)       ('+'|'-'|Lambda)
            +
           --->Oo
          (  -
         O--->Oo
          (____Oo
          Lambda

Translate the logical diagram into a program:
Ex.  Logical Diagram for a program:

                         _Not(EOL)
                        ( )
-->O--->O--->O--->Oo
         /         /
 

codes:
if(Getchar() == '/'){
    if(Getchar() == '/'){
       while (Getchar != '\n'){
          ....................
       }
    return (comment);
    }
    else Error();
}
else Error();

==> Graph is much more chear than codes

We can use transition table to make our code clear:
 
T[state][char] = { 1. state or 2. error flag}

                       _Not(EOL)
                        ( )
-->O--->O--->O--->Oo
      |   /         /
      |--->Oo--->Oo
        +          +
 
/ EOL a b +
state 1 2 5
2 3
3 3 4 3 3
4
5 6
6

ST = start state;
while(true){
    CH = Getchar();
    if(CH == EOF)  break;
    next = T[ST][CH]
    if(next == Error) break;
    ST = next;
}
if(Isfinal(ST))
    return (valid token);
else
    return (invalid token);

Ex.
 +  +  +  +  +  +  +  + = 4 double plus together
|____|____|____|___|
state   state state state
  6         6      6      6

If there is no extened token found, just go back to the first token