AQA 7517, section 4.4.2.4
Languages and strings
In computer science, a language is simply a set of strings built from an alphabet of symbols. For example, with the alphabet :
- the set of all strings of 0s and 1s with an even number of 1s is a language;
- the set of all binary strings that end in 1 is a language;
- the set is a language.
What is a regular language?
A language is called regular if it can be represented by a regular expression.
Because regular expressions and finite state machines are equivalent, there is another way to say the same thing:
A regular language is any language that a finite state machine will accept.
So these three statements all mean the same:
| Statement |
|---|
| The language can be described by a regular expression. |
| The language can be accepted by a finite state machine. |
| The language is regular. |
Examples of regular languages
| Language | Regular expression |
|---|---|
Strings of a followed by any number of b |
ab* |
| Binary strings ending in 1 | (0|1)*1 |
Either cat or dog |
cat|dog |
| Binary strings with an even number of 1s | 0*(10*10*)* |
Each one can also be recognised by a small FSM, as shown in the earlier pages.
What a finite state machine cannot do
An FSM has only a finite number of states, and no other memory. So it can't count without limit.
Consider : any number of 0s followed by the same number of 1s. To decide whether a string belongs, a machine would have to remember how many 0s it saw, and that number can be as large as you like. A machine with a fixed number of states can't store an unlimited count.
So this language is not regular. It can't be described by a regular expression, and no FSM accepts it. It needs a more powerful method of describing languages, such as Backus-Naur Form (4.4.3.1), or a more powerful machine, such as a Turing machine (4.4.5.1).
Other languages that aren't regular include strings of correctly matched brackets such as ((())), where the machine would also have to count how many brackets are still open.
Worked example
Task: is the language "all strings of as and bs that contain no two bs in a row" regular? If so, give an FSM.
Yes. A machine only needs to remember whether the last symbol was b, which needs just two states, plus a dead state.
| Current state | Input a | Input b |
|---|---|---|
S0 (start, accepting): last symbol wasn't b |
S0 | S1 |
S1 (accepting): last symbol was b |
S0 | Dead |
| Dead (not accepting) | Dead | Dead |
Test abab: S0, a gives S0, b gives S1, a gives S0, b gives S1. It ends in S1, which is accepting. Test abba: S0, a gives S0, b gives S1, b gives Dead. It never leaves Dead, so it's rejected.
It can also be described by the regular expression (a|ba)*b?: any number of a or ba pieces, optionally ending with a single b. Since it has a regular expression and an FSM, it is a regular language.
Exam tip: a regular language is one that can be represented by a regular expression, or equivalently accepted by an FSM. To show a language is not regular, explain that it would need unbounded counting or memory, which a finite number of states can't provide.
Check yourself
What is a regular language?
A language that can be represented by a regular expression, which is the same as a language that a finite state machine will accept.
Why can't an FSM accept the language of strings with n zeros followed by n ones?
It would need to count the zeros, and the number is unlimited. An FSM has only a finite number of states, so it can't remember an arbitrarily large count.
Give an example of a regular language and a regular expression for it
Binary strings ending in 1, written as (0|1)*1. (Other correct examples are possible.)