Skip to content

Lecture No. 07

Dated: 14-03-2025

Table Encoding of Finite Automaton

A finite automaton1 can be encoded as a table. This is called transition table.

a b
0 1 err
1 2 1
2 err err

cs606_e_7_1.svg

int finite_automaton () {

    int trans_table[NSTATES][NCHARS];
    int accept_states[NSTATES];

    int state = INITIAL;

    while (state != err) {
        c = input.read();
        if (c == EOF)
            break;

        state = trans_table[state][c];
    }

    return accept_states[state];
}

Non Deterministic Finite Automaton

Each regular expression2 can be used to encode a token3 and is converted to a non deterministic finite automaton4 which are joined using \(\epsilon\) moves into a single deterministic finite automaton1 which can later be encoded in a transition table.
A non deterministic finite automaton4 can have multiple transitions for one input in a given table.
cs606_e_7_2.svg

Sometimes it might not even need an input character for transition.
cs606_e_7_3.svg

The acceptance of non deterministic finite automaton4 for a given string4 is determined if it can reach a final state or not.

Deterministic Finite Automaton

This one has only one transition per input per state without \(\epsilon\) moves.
Both NFAs4 and DFAs1 can recognize same set4 of languages5 though DFAs are easier but can be exponentially larger than NFAs. Therefore, NFAs are the key for automating RE -> DFA construction.

RE2NFA4 Construction

The algorithm for regular expression2 to DFA conversion is called Thompson's Construction. The algorithm appeared in CACM 1968. It works as follows

  • Builds a NFA4 for each regular expression.2
  • The NFAs4 are combined using \(\epsilon\) moves.
  • The NFA4 is converted into DFA.1
  • The number of states is reduced in the DFA1 using the Hopcroft’s algorithm.

Example

Consider a NFA4 for the regular expression2 \(ab\).

cs606_e_7_4.svg

NFA4 for \(a\)

cs606_e_7_5.svg

NFA4 for \(b\)

cs606_e_7_6.svg

NFA4 for \(ab\). The NFA4 for \(a\) appears first due to its precedence.

References