Lecture No. 07
Dated: 14-03-2025
Table Encoding of Finite Automaton
A finite automaton
1 can be encoded as a table
. This is called transition table
.
a | b | |
---|---|---|
0 | 1 | err |
1 | 2 | 1 |
2 | err | err |
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 expression
2 can be used to encode a token
3 and is converted to a non deterministic finite automaton
4 which are joined using \(\epsilon\) moves into a single deterministic finite automaton
1 which can later be encoded in a transition table
.
A non deterministic finite automaton
4 can have multiple transitions for one input in a given table.
Sometimes it might not even need an input character for transition.
The acceptance of non deterministic finite automaton
4 for a given string
4 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 NFAs
4 and DFAs
1 can recognize same set
4 of languages
5 though DFAs
are easier but can be exponentially larger than NFAs
. Therefore, NFAs
are the key for automating RE -> DFA
construction.
RE
2 → NFA
4 Construction
The algorithm for regular expression
2 to DFA
conversion is called Thompson's Construction
. The algorithm appeared in CACM 1968
. It works as follows
- Builds a
NFA
4 for eachregular expression
.2 - The
NFAs
4 are combined using \(\epsilon\) moves. - The
NFA
4 is converted intoDFA
.1 - The number of
states
is reduced in theDFA
1 using theHopcroft’s algorithm
.
Example
Consider a NFA
4 for the regular expression
2 \(ab\).
NFA
4 for \(a\)
NFA
4 for \(b\)
NFA
4 for \(ab\). The NFA
4 for \(a\) appears first due to its precedence.