...

/

Regular Languages

Regular Languages

Learn about regular languages and explore some more examples of deterministic finite automata.

Formal definition

A regular language is a formal language for which there exists a deterministic finite automaton that accepts all and only those strings in the language.

Let’s look at a few examples of regular languages and their corresponding DFA.

DFA accepting strings starting with aa and ending with bb

Consider the language KK, of all strings that begin with an aa and end with a bb. We can write this language as K={all strings that begin with an a and end with a b}K = \{ \textrm{all strings that begin with an }a \textrm{ and end with a } b \}. If the first symbol is a bb, then there is no hope of the machine accepting it. The following figure shows a machine that accepts all the strings starting with an aa and ending with a bb.

Press + to interact
DFA for the language K
DFA for the language K

DFA accepting strings with a 1 in the next-to-last position

Consider the alphabet {0,1}\{0, 1\}. How would we construct a DFA that accepts the language LL, which contains strings of zeros and ones where the next-to-last symbol is a 11? Such a string must end with either 1010 or 1111. This language can be written as L={all strings of 0’s and 1’s ending with 10 or 11}L = \{ \textrm{all strings of } 0 \textrm{'s and } 1 \textrm{'s ending with } 10 \textrm{ or } 11 \}.

Since we never know when the next-to-last input occurs, we need to track those two substrings whenever they occur. The following figure shows a DFA for the language LL.

Press + to interact
DFA which accepts L
DFA which accepts L

The state labels reflect the substring just processed that led to each state. Observe how the out-edges from the accepting states behave. The last symbol read before reaching those states becomes the next-to-last symbol on the subsequent move.

DFA accepting strings with a 1 in the third-to-last position

Now let’s consider how to recognize strings over {0,1}\{0,1\} that end with a 11 in the third-to-last position. Reading a 11 starts us on a potential path to acceptance. From there, we’ll read a 00 or 11, at which point we’ll have read one of 1010 or 1111. An acceptable string must be of at least length three and end with 100100, 101101, 110110, or 111111. Thus, the language of our interest, is M={all strings of 0’s and 1’s ending with 100,101,110, or 111}M = \{ \textrm{all strings of } 0 \textrm{'s and } 1 \textrm{'s ending with } 100,\:101,\:110, \textrm{ or } 111 \}.

The possibilities that the input ends with 100,101,110,100, 101, 110, ...

Access this course and 1400+ top-rated courses and projects.