Regular language

In theoretical computer science and formal language theory, a regular language (also called a rational language) is a formal language that can be defined by a regular expression, in the strict sense in theoretical computer science (as opposed to many modern regular expression engines, which are augmented with features that allow the recognition of non-regular languages).

Alternatively, a regular language can be defined as a language recognised by a finite automaton. The equivalence of regular expressions and finite automata is known as Kleene's theorem (after American mathematician Stephen Cole Kleene). In the Chomsky hierarchy, regular languages are the languages generated by Type-3 grammars.

Formal definition
The collection of regular languages over an alphabet Σ is defined recursively as follows:
 * The empty language Ø is a regular language.
 * For each a ∈ Σ (a belongs to Σ), the singleton language {a&hairsp;} is a regular language.
 * If A is a regular language, A* (Kleene star) is a regular language. Due to this, the empty string language {ε} is also regular.
 * If A and B are regular languages, then A ∪ B (union) and A • B (concatenation) are regular languages.
 * No other languages over Σ are regular.

See regular expression for syntax and semantics of regular expressions.

Examples
All finite languages are regular; in particular the empty string language {ε} = Ø* is regular. Other typical examples include the language consisting of all strings over the alphabet {a, b} which contain an even number of a ' s, or the language consisting of all strings of the form: several a ' s followed by several b ' s.

A simple example of a language that is not regular is the set of strings {anbn | n ≥ 0}. Intuitively, it cannot be recognized with a finite automaton, since a finite automaton has finite memory and it cannot remember the exact number of a's. Techniques to prove this fact rigorously are given below.

Equivalent formalisms
A regular language satisfies the following equivalent properties:
 * 1) it is the language of a regular expression (by the above definition)
 * 2) it is the language accepted by a nondeterministic finite automaton (NFA)
 * 3) it is the language accepted by a deterministic finite automaton (DFA)
 * 4) it can be generated by a regular grammar
 * 5) it is the language accepted by an alternating finite automaton
 * 6) it is the language accepted by a two-way finite automaton
 * 7) it can be generated by a prefix grammar
 * 8) it can be accepted by a read-only Turing machine
 * 9) it can be defined in monadic second-order logic (Büchi–Elgot–Trakhtenbrot theorem)
 * 10) it is recognized by some finite syntactic monoid M, meaning it is the preimage {w ∈ Σ* | f(w) ∈ S} of a subset S of a finite monoid M under a monoid homomorphism f: Σ* → M from the free monoid on its alphabet
 * 11) the number of equivalence classes of its syntactic congruence is finite.  (This number equals the number of states of the minimal deterministic finite automaton accepting L.)

Properties 10. and 11. are purely algebraic approaches to define regular languages; a similar set of statements can be formulated for a monoid M ⊆ Σ*. In this case, equivalence over M leads to the concept of a recognizable language.

Some authors use one of the above properties different from "1." as an alternative definition of regular languages.

Some of the equivalences above, particularly those among the first four formalisms, are called Kleene's theorem in textbooks. Precisely which one (or which subset) is called such varies between authors. One textbook calls the equivalence of regular expressions and NFAs ("1." and "2." above) "Kleene's theorem". Another textbook calls the equivalence of regular expressions and DFAs ("1." and "3." above) "Kleene's theorem". Two other textbooks first prove the expressive equivalence of NFAs and DFAs ("2." and "3.") and then state "Kleene's theorem" as the equivalence between regular expressions and finite automata (the latter said to describe "recognizable languages"). A linguistically oriented text first equates regular grammars ("4." above) with DFAs and NFAs, calls the languages generated by (any of) these "regular", after which it introduces regular expressions which it terms to describe "rational languages", and finally states "Kleene's theorem" as the coincidence of regular and rational languages. Other authors simply define "rational expression" and "regular expressions" as synonymous and do the same with "rational languages" and "regular languages".

Apparently, the term "regular" originates from a 1951 technical report where Kleene introduced "regular events" and explicitly welcomed "any suggestions as to a more descriptive term". Noam Chomsky, in his 1959 seminal article, used the term "regular" in a different meaning at first (referring to what is called "Chomsky normal form" today), but noticed that his "finite state languages" were equivalent to Kleene's "regular events".

Closure properties
The regular languages are closed under various operations, that is, if the languages K and L are regular, so is the result of the following operations:


 * the set-theoretic Boolean operations: union $K ∪ L$, intersection $K ∩ L$, and complement $\overline{L}$, hence also relative complement $K &minus; L$.
 * the regular operations: $K ∪ L$, concatenation $K \circ L$, and Kleene star $L^{*}$.
 * the trio operations: string homomorphism, inverse string homomorphism, and intersection with regular languages. As a consequence they are closed under arbitrary finite state transductions, like quotient K / L with a regular language. Even more, regular languages are closed under quotients with arbitrary languages: If L is regular then L / K is regular for any K.
 * the reverse (or mirror image) LR. Given a nondeterministic finite automaton to recognize L, an automaton for LR can be obtained by reversing all transitions and interchanging starting and finishing states. This may result in multiple starting states; ε-transitions can be used to join them.

Decidability properties
Given two deterministic finite automata A and B, it is decidable whether they accept the same language. As a consequence, using the above closure properties, the following problems are also decidable for arbitrarily given deterministic finite automata A and B, with accepted languages LA and LB, respectively:
 * Containment: is LA ⊆ LB ?
 * Disjointness: is LA ∩ LB = {} ?
 * Emptiness: is LA = {} ?
 * Universality: is LA = Σ* ?
 * Membership: given a ∈ Σ*, is a ∈ LB ?

For regular expressions, the universality problem is NP-complete already for a singleton alphabet. For larger alphabets, that problem is PSPACE-complete. If regular expressions are extended to allow also a squaring operator, with "A2" denoting the same as "AA", still just regular languages can be described, but the universality problem has an exponential space lower bound,  and is in fact complete for exponential space with respect to polynomial-time reduction.

For a fixed finite alphabet, the theory of the set of all languages — together with strings, membership of a string in a language, and for each character, a function to append the character to a string (and no other operations) — is decidable, and its minimal elementary substructure consists precisely of regular languages. For a binary alphabet, the theory is called S2S.

Complexity results
In computational complexity theory, the complexity class of all regular languages is sometimes referred to as REGULAR or REG and equals DSPACE(O(1)), the decision problems that can be solved in constant space (the space used is independent of the input size). REGULAR ≠ AC0, since it (trivially) contains the parity problem of determining whether the number of 1 bits in the input is even or odd and this problem is not in AC0. On the other hand, REGULAR does not contain AC0, because the nonregular language of palindromes, or the nonregular language $$\{0^n 1^n : n \in \mathbb N\}$$ can both be recognized in AC0.

If a language is not regular, it requires a machine with at least Ω(log&thinsp;log&thinsp;n) space to recognize (where n is the input size). In other words, DSPACE(o(log&thinsp;log&thinsp;n)) equals the class of regular languages. In practice, most nonregular problems are solved by machines taking at least logarithmic space.

Location in the Chomsky hierarchy
To locate the regular languages in the Chomsky hierarchy, one notices that every regular language is context-free. The converse is not true: for example, the language consisting of all strings having the same number of a ' s as b ' s is context-free but not regular. To prove that a language is not regular, one often uses the Myhill–Nerode theorem and the pumping lemma. Other approaches include using the closure properties of regular languages or quantifying Kolmogorov complexity.

Important subclasses of regular languages include
 * Finite languages, those containing only a finite number of words. These are regular languages, as one can create a regular expression that is the union of every word in the language.
 * Star-free languages, those that can be described by a regular expression constructed from the empty symbol, letters, concatenation and all boolean operators (see algebra of sets) including complementation but not the Kleene star: this class includes all finite languages.

The number of words in a regular language
Let $$s_L(n)$$ denote the number of words of length $$n$$ in $$L$$. The ordinary generating function for L is the formal power series


 * $$S_L(z) = \sum_{n \ge 0} s_L(n) z^n \ . $$

The generating function of a language L is a rational function if L is regular. Hence for every regular language $$L$$ the sequence $$s_L(n)_{n \geq 0}$$ is constant-recursive; that is, there exist an integer constant $$n_0$$, complex constants $$\lambda_1,\,\ldots,\,\lambda_k$$ and complex polynomials $$p_1(x),\,\ldots,\,p_k(x)$$ such that for every $$n \geq n_0$$ the number $$s_L(n)$$ of words of length $$n$$ in $$L$$ is $$s_L(n)=p_1(n)\lambda_1^n+\dotsb+p_k(n)\lambda_k^n$$.

Thus, non-regularity of certain languages $$L'$$ can be proved by counting the words of a given length in $$L'$$. Consider, for example, the Dyck language of strings of balanced parentheses. The number of words of length $$2n$$ in the Dyck language is equal to the Catalan number $$C_n\sim\frac{4^n}{n^{3/2}\sqrt{\pi}}$$, which is not of the form $$p(n)\lambda^n$$, witnessing the non-regularity of the Dyck language. Care must be taken since some of the eigenvalues $$\lambda_i$$ could have the same magnitude. For example, the number of words of length $$n$$ in the language of all even binary words is not of the form $$p(n)\lambda^n$$, but the number of words of even or odd length are of this form; the corresponding eigenvalues are $$2,-2$$. In general, for every regular language there exists a constant $$d$$ such that for all $$a$$, the number of words of length $$dm+a$$ is asymptotically $$C_a m^{p_a} \lambda_a^m$$.

The zeta function of a language L is


 * $$\zeta_L(z) = \exp \left({ \sum_{n \ge 0} s_L(n) \frac{z^n}{n} }\right) . $$

The zeta function of a regular language is not in general rational, but that of an arbitrary cyclic language is.

Generalizations
The notion of a regular language has been generalized to infinite words (see ω-automata) and to trees (see tree automaton).

Rational set generalizes the notion (of regular/rational language) to monoids that are not necessarily free. Likewise, the notion of a recognizable language (by a finite automaton) has namesake as recognizable set over a monoid that is not necessarily free. Howard Straubing notes in relation to these facts that “The term "regular language" is a bit unfortunate. Papers influenced by Eilenberg's monograph often use either the term "recognizable language", which refers to the behavior of automata, or "rational language", which refers to important analogies between regular expressions and rational power series. (In fact, Eilenberg defines rational and recognizable subsets of arbitrary monoids; the two notions do not, in general, coincide.) This terminology, while better motivated, never really caught on, and "regular language" is used almost universally.”

Rational series is another generalization, this time in the context of a formal power series over a semiring. This approach gives rise to weighted rational expressions and weighted automata. In this algebraic context, the regular languages (corresponding to Boolean-weighted rational expressions) are usually called rational languages. Also in this context, Kleene's theorem finds a generalization called the Kleene-Schützenberger theorem.