Regular Language
Overview
Finite automata are classified as either deterministic or nondeterministic. These two representations are equivalent.
If
A language is called a regular language if a finite automaton recognizes it.
Automaton
Deterministic
A deterministic finite automaton (DFA) is a
is a finite set called the states; is a finite set called the alphabet; is the transition function; is the start state; and is the set of final states.
These automaton are typically denoted using a state diagram like below. The start state is indicated by an arrow pointing at it from nowhere. An accept state is denoted with a double circle.

Nondeterministic
A nondeterministic finite automaton (NFA) is a
is a finite set called the states; is a finite set called the alphabet; is the transition function; is the start state; and is the set of final states.
Like DFAs, these automaton are typically denoted using a state diagram. Unlike DFAs, not every state needs an exiting transition arrow for each symbol in the alphabet. Also, arrows can be labeled
Equivalence
Two finite automaton are equivalent if they recognize the same language. As it turns out, every NFA can be converted to an equivalent DFA.
Epsilon-Closure
Let
More formally, define
Then the
Using the bottom-up approach,
where
Power Set Construction
The power set construction describes the process by which an NFA can be converted into an equivalent DFA:
Let
Then
Regular Expressions
A regular expression is a textual representation of some language. They are equivalent to finite automaton in their descriptive power. More formally, we say
for some in the alphabet , , , where and are regular expressions, where and are regular expressions, where is a regular expression.
Note individual symbols like
GNFA
A generalized nondeterministic finite automaton (GNFA) is an NFA in which the transition arrows may have any regular expressions as labels, instead of only members of an alphabet (or
These are useful for constructing a regular expression from a DFA. In particular, every DFA can be converted into a GNFA and every GNFA can be converted into a 2-state GNFA whose sole transition is labeled with the desired regular expression.
Extended
The following ERE (Extended Regular Expression) operators were defined to achieve consistency between programs like grep, sed, and awk. In POSIX, regexps are greedy.
.matches any single character.- There exist application-specific exclusions. For instance, newlines and the
NULcharacter are often ignored.
- There exist application-specific exclusions. For instance, newlines and the
[...], the bracket expression, matches any enclosed character.- An optional
-can be included to denote a range. -is treated literally if its the first or last specified character.]is treated literally if its the first specified character.^complements the set if its the first specified character.
- An optional
^is the leading anchor. It matches the starting position of a string.$is the trailing anchor. It matches the ending position of a string.*matches the preceding element zero or more times.+matches the preceding element one or more times.?matches the preceding element zero or one times.{n}, an interval expression, matches the preceding elementntimes.{n,}matches the preceding element at leastntimes.{n,m}matches the preceding element betweennandmtimes.- Interval expressions cannot contain repetition counts
> 255. Results are otherwise undefined.
|is the alternation operator. It allows specifying match alternatives.
Character Classes
ERE also introduced notation for describing a class of characters specific to a given locale/character set:
| Class | Similar To | Meaning |
|---|---|---|
[:alnum:] | [A-Za-z0-9] | Alphanumeric characters |
[:alpha:] | [A-Za-z] | Alphabetic characters |
[:blank:] | [ \t] | ' ' and TAB characters |
[:cntrl:] | Control characters | |
[:digit:] | [0-9] | Numeric characters |
[:graph:] | [^ [:cntrl:]] | Printable and visible characters |
[:lower:] | [a-z] | Lowercase alphabetic characters |
[:print:] | [ [:graph:]] | Printable characters |
[:punct:] | All graphic characters except letters and digits | |
[:space:] | [ \t\n\r\f\v] | Whitespace characters |
[:upper:] | [A-Z] | Uppercase alphabetic characters |
[:xdigit:] | [0-9A-Fa-f] | Hexadecimal digits |
Backtracking
Many popular implementations of regular expression libraries use backtracking instead of finite automaton. This implementation strategy is notorious for being exponentially slow.
The only situation in which backtracking is the best known option is in the case of backreferences, i.e. a substring that matches a previously matched portion of the "regular expression" (quoted since backreferences are not actual regular expressions in the theoretical sense).
Thompson's Algorithm
Thompson's algorithm converts regular expressions into an NFA containing a number of states equal to at most the length of the expression. The total NFA is built up from partial NFAs for each subexpression, with a different construction for each operator. Each partial NFA has dangling arrows pointing to nothing. The result is concatenated together at the end.
Once built, the NFA is simulated in a multiple-state fashion. Every possible branch of navigation is executed simultaneously.
Closure Operations
Let
- Union:
- Intersection:
- Complement:
- Reversal:
- Concatenation:
- Kleene star:
Product Construction
Let
,
The definition of
Pumping Lemma
Let
for any integer , , and .