Sub topic 3
NFAs and DFAs as Token Recognizers
Assume you know regular expressions, basic finite automata, sets, and transitions. This chapter treats an automaton as a recognizer: it reads an input string and decides whether the string belongs to a language. In a compiler, that language is usually the set of lexemes for one token class.
Finite automata and recognition
A finite automaton has a finite set of states, an input alphabet, transitions, a start state, and accepting states. It accepts an input when the complete input can be consumed while ending in an accepting state.
A nondeterministic finite automaton (NFA) may have several possible next states for the same input symbol. It may also have an epsilon transition, written ε, which changes state without consuming input. The NFA accepts if at least one possible path consumes the whole input and ends in an accepting state.
A deterministic finite automaton (DFA) has exactly one next state for each state and input symbol. It has no epsilon transitions. Therefore, after reading any input prefix, the current state is uniquely known.
| Property | NFA | DFA |
|---|---|---|
| Next states | A set of possible states | One state |
| Epsilon transitions | Allowed | Not allowed |
| Input processing | Track possible paths | Track one path |
| Recognition power | Regular languages | Regular languages |
| Typical use | Compact construction | Fast implementation |
The two models recognize exactly the regular languages. An NFA can be easier to build from a regular expression. A DFA is usually easier to execute because each character causes one state transition.
Example: recognizing `ab` or `ac`
The following automaton branches after reading a. It accepts exactly the two strings ab and ac.
For input ac, the NFA takes the a transition and then the c transition, ending in an accepting state. For input ad, no complete accepting path exists, so it rejects. It also rejects a, because the input ends before an accepting state is reached.
Simulating an NFA
An NFA simulator stores a set of currently possible states rather than one state. For each input character, it computes all destinations reachable by that character. If epsilon transitions exist, the simulator also includes every state reachable through zero or more epsilon transitions. This set is called the epsilon-closure.
A DFA can simulate the same behavior by treating each set of NFA states as one DFA state. This construction is called subset construction. Some sets are unreachable, and a practical implementation omits them. The resulting DFA can have as many as \(2^n\) states for an NFA with \(n\) states, although real token patterns often produce much smaller machines.
Token recognition
A lexer can use one automaton per token class or combine several token patterns into one machine. The recognizer reports whether the characters form a valid lexeme. Decisions such as choosing the longest matching lexeme and resolving equal-length matches belong to the lexer’s token-selection policy.
Exercise
Why can a DFA execute an input more directly than an NFA?
Show answer
A DFA has exactly one current state and one next state for each input character, while an NFA must track multiple possible states.