Glushkov's construction algorithm

In computer science theory – particularly formal language theory – Glushkov's construction algorithm, invented by Victor Mikhailovich Glushkov, transforms a given regular expression into an equivalent nondeterministic finite automaton (NFA). Thus, it forms a bridge between regular expressions and nondeterministic finite automata: two abstract representations of the same class of formal languages.

A regular expression may be used to conveniently describe an advanced search pattern in a "find and replace"–like operation of a text processing utility. Glushkov's algorithm can be used to transform it into an NFA, which furthermore is small by nature, as the number of its states equals the number of symbols of the regular expression, plus one. Subsequently, the NFA can be made deterministic by the powerset construction and then be minimized to get an optimal automaton corresponding to the given regular expression. The latter format is best suited for execution on a computer.

From another, more theoretical point of view, Glushkov's algorithm is a part of the proof that NFA and regular expressions both accept exactly the same languages; that is, the regular languages. The converse of Glushkov's algorithm is Kleene's algorithm, which transforms a finite automaton into a regular expression. The automaton obtained by Glushkov's construction is the same as the one obtained by Thompson's construction algorithm, once its ε-transitions are removed.

Construction
Given a regular expression $e$, the Glushkov Construction Algorithm creates a non-deterministic automaton that accepts the language $$L(e)$$ accepted by $e$. The construction uses four steps:

Step 1
Linearisation of the expression. Each letter of the alphabet appearing in the expression $e$ is renamed, so that each letter occurs at most once in the new expression $$e'$$. Glushkov's construction essentially relies on the fact that $$e'$$ represents a local language $$L(e')$$. Let $A$ be the old alphabet and let $B$ be the new one.

Step 2a
Computation of the sets $$P(e')$$, $$D(e')$$, and $$F(e')$$. The first, $$P(e')$$, is the set of letters which occurs as first letter of a word of $$L(e')$$. The second, $$D(e')$$, is the set of letters that can end a word of $$L(e')$$. The last one, $$F(e')$$, is the set of letter pairs that can occur in words of $$L(e')$$, i.e. it is the set of factors of length two of the words of $$L(e')$$. Those sets are mathematically defined by
 * $$P(e')=\{x\in B\mid xB^*\cap L(e')\ne\emptyset\}$$,
 * $$D(e')=\{y\in B\mid B^*y\cap L(e')\ne\emptyset\}$$,
 * $$F(e')=\{u\in B^2\mid B^*uB^*\cap L(e')\ne\emptyset\}$$.

They are computed by induction over the structure of the expression, as explained below.

Step 2b
Computation of the set $$\Lambda(e')$$ which contains the empty word if this word belongs to  $$L(e')$$, and is the empty set otherwise. Formally, this is $$\Lambda(e')=\{\varepsilon\}\cap L(e')$$, where $$\varepsilon$$ denotes the empty word.

Step 3
Computation of automaton recognizing the local language, as defined by $$P(e')$$,  $$D(e')$$, $$F(e')$$, and $$\Lambda(e')$$. By definition, the local language defined by the sets $P$, $D$, and $F$ is the set of words which begin with a letter of $P$, end by a letter of $D$, and whose factors of length 2 belong to $F$, optionally also including the empty word; that is, it is the language:
 * $$L'=(PB^*\cap B^*D) \setminus B^*(B^2\setminus F)B^* \cup \Lambda(e')$$.

Strictly speaking, it is the computation of the automaton for the local language denoted by this linearised expression that is Glushkov's construction.

Step 4
Remove the linearisation, replacing each indexed letter $B$ by the original letter of $A$.

Example


Consider the regular expression $$e = (a(ab)^*)^* + (ba)^*$$. 1. The linearized version is
 * $e'=(a_1(a_2b_3)^*)^*+(b_4a_5)^*$.

The letters have been linearized by appending an index to them.

2. The sets $P$, $D$, and $F$ of the first letters, last letters, and factors of length 2 for the linear expression are respectively


 * $\begin{align}

P(e')&=\{a_1,b_4\}\\ D(e')&=\{a_1,b_3,a_5\}\\ F(e')&=\{a_1a_2, a_1a_1, a_2b_3, b_3a_1,b_3a_2,b_4a_5,a_5b_4\} \end{align}$.

The empty word belongs to the language, hence $\Lambda(e')=\{ \varepsilon \}$.

3. The automaton of the local language
 * $L'=(PB^*\cap B^*D) \setminus B^*(B^2\setminus F)B^*$

contains an initial state, denoted 1, and a state for each of the five letters of the alphabet
 * $B=\{a_1, a_2, b_3, b_4, a_5\}$.

There is a transition from 1 to the two states of $P$, a transition from $x$ to $y$ for $xy \in F$, and the three states of $D$ are final, and such is the state 1. All transitions to a letter $y$ have as label the letter $y$.

4. Obtain the automaton for $L(e)$ by deleting the indices.

Computation of the set of letters
The computation of the sets $P$, $D$, $F$, and $&Lambda;$ is done inductively over the regular expression $$e'$$. One must give the values for $∅, ε$ (the symbols for the empty language and the singleton language containing the empty word), the letters, and the results of the operations $$+,\cdot,^*$$. 1. For $&Lambda;$, one has
 * $\Lambda(\emptyset)=\emptyset$,
 * $\Lambda(\varepsilon)=\{ \varepsilon \}$,
 * $\Lambda(a)=\emptyset$ for each letter $a$,
 * $\Lambda(e+f)=\Lambda(e)\cup\Lambda(f)$,
 * $\Lambda(e\cdot f)=\Lambda(e)\cdot \Lambda(f)$, and
 * $\Lambda(e^*)=\{ \varepsilon \}$.

2. For $P$, one has
 * $P(\emptyset)=P(\varepsilon)=\emptyset$,
 * $P(a)=\{a\}$ for each letter $a$,
 * $P(e+f)=P(e)\cup P(f)$,
 * $ P(e\cdot f)= P(e)\cup\Lambda(e)P(f)$, and
 * $P(e^*)=P(e)$.

The same formulas are also used for $D$, except for the product where
 * $D(e\cdot f)= D(f)\cup D(e) \Lambda(f)$.

3. For the set of factors of length 2, one has The most costly operations are the products of sets for the computation of $a$.
 * $F(\emptyset)=F(\varepsilon)=F(a)=\emptyset$ for each letter $F$,
 * $F(e+f)=F(e)\cup F(f)$,
 * $F(e\cdot f)=F(e)\cup F(f)\cup D(e)P(f)$, and
 * $F(e^*)=F(e)\cup D(e)P(e)$.

Properties
The obtained automaton is non-deterministic, and it has as many states as the number of letters of the regular expression, plus one. Furthermore, it has been shown that Glushkov's automaton is the same as Thompson's automaton when the ε-transitions are removed.

Applications and deterministic expressions
The computation of the automaton by the expression occurs often; it has been systematically used in search functions, in particular by the Unix grep command. Similarly, XML's specification also uses such constructions; for more efficiency, regular expressions of a certain kind, called deterministic expressions, have been studied.