Overview

A Finite State Automaton (FSA) is a simple model of a machine. It has a finite number of states, exactly one of which is active at any moment; a set of transition rules that move the machine from one state to another when it reads an input symbol; one initial state; and one or more final (accepting) states.

In ACSL, FSAs are used to parse strings: the machine reads the string one character at a time, following the transitions, and the string is accepted if the machine finishes in a final state. If at any point there is no transition for the character being read, or the machine ends somewhere other than a final state, the string is rejected.

A Regular Expression (RE) is an algebraic way of writing the same thing: a compact pattern that describes exactly the set of strings an FSA accepts. Every FSA has an equivalent RE and every RE has an equivalent FSA.

Key Concepts:

  • Drawing conventions: states are circles, the final state is a double circle, the start state has an incoming arrow from nowhere, and transitions are labelled arrows.
  • The three RE operators are concatenation, union (U or |) and the Kleene star (*).
  • Typical questions: convert an FSA to an RE, simplify an RE, pick equivalent REs, or decide which strings a pattern matches.

Key Concepts

Reading an FSA

ABC xy xy

This FSA has three states. A is the initial state and C is the final state. Reading an x in state A moves to B. In B, an x stays in B and a y moves to C. In C, a y stays in C. So the machine accepts one or more x's followed by one or more y's: xy, xxy, xxxyy, xyyy and so on, but not yx, x, or xyx. The equivalent regular expression is xx*yy*.

Rules for Building Regular Expressions

  1. The null string, written λ, is an RE.
  2. Any single symbol of the alphabet is an RE.
  3. If a and b are REs, so are:
    • Concatenation ab: a followed by b.
    • Union a U b (also written a|b): a or b.
    • Kleene star a*: a repeated zero or more times.

Precedence

Kleene star binds tightest, then concatenation, then union. Parentheses group sub-expressions. So dca*b generates dcb, dcab, dcaab, ..., while d(ca)*b generates db, dcab, dcacab, ...

Regular Expression Identities

These identities let you simplify an RE or recognise that two REs are equivalent.

1. (a*)* = a*

2. aa* = a*a

3. aa* U λ = a*

4. a(b U c) = ab U ac

5. a(ba)* = (ab)*a

6. (a U b)* = (a* U b*)*

7. (a U b)* = (a*b*)*

8. (a U b)* = a*(ba*)*

Regex in Practice

Programmers use regular expressions ("regex") constantly to describe search patterns. Every modern language has a regex library. The exact syntax varies slightly between tools, but ACSL uses the following additions, which are nearly universal:

PatternMeaningExample
|Alternatives (union)gray|grey matches gray or grey
*Zero or more of the preceding elementab*c matches ac, abc, abbc, ...
?Zero or one of the preceding elementcolou?r matches color and colour
+One or more of the preceding elementab+c matches abc, abbc, ... but not ac
.Any single charactera.b matches a7b, a&b, arb; a.*b matches ab, acb, a123b
[ ]One character from the set; ranges allowed[abc], [a-z], [a-cx-z]
[^ ]One character not in the set[^abc] matches any character except a, b or c
( )GroupingH(ä|ae?)ndel matches Handel, Händel, Haendel

Examples

Example 1: FSA to regular expression

An FSA has states A (start), B and C (final). Transitions: A goes to B on 0; B loops to itself on 1; B goes to C on 0; C goes to a further final state D on 1.

Follow the only route from start to finish and write down what is read: a 0, then any number of 1's (the loop), then a 0, then a 1.

Regular expression: 01*01

Example 2: Which strings does "00*1*1 U 11*0*0" accept?

Candidates: (A) 0000001111111, (B) 1010101010, (C) 1111111, (D) 0110, (E) 10.

  1. The union has two halves. 00*1*1 means one or more 0's followed by one or more 1's: 01, 001, 0001111, ...
  2. 11*0*0 means one or more 1's followed by one or more 0's: 10, 1110, 1111100, ...
  3. A is all 0's then all 1's, so it matches the first half. E is 10, which matches the second half.
  4. B alternates, C has no 0, and D switches direction twice, so none of them match.

Accepted: A and E

Example 3: Which strings match "[A-D]*[a-d]*[0-9]"?

Candidates: 1. ABCD8, 2. abcd5, 3. ABcd9, 4. AbCd7, 5. X, 6. abCD7, 7. DCCBBBaaaa5.

The pattern is: zero or more uppercase A-D, then zero or more lowercase a-d, then exactly one digit. Once you have started the lowercase part you cannot go back to uppercase (that rules out 4 and 6), and the string must end with a digit (rules out 5).

Matches: 1, 2, 3 and 7

Example 4: Which strings match "Hi?g+h+[^a-ceiou]"?

Candidates: 1. Highb, 2. HiiighS, 3. HigghhhC, 4. Hih, 5. Hghe, 6. Highd, 7. HgggggghX.

H, then an optional single i, then one or more g's, then one or more h's, then one character that is not a, b, c, e, i, o or u. String 1 ends in b (excluded); 2 has three i's; 4 has no g; 5 ends in e (excluded).

Matches: 3, 6 and 7

Practice Problems

For "which strings" questions, enter the matching numbers separated by commas, for example 1,3,5.

Problem 1 Junior

Which of the following strings are accepted by the regular expression ab*a?

  1. aa
  2. aba
  3. abba
  4. ab
  5. baa

Problem 2 Junior

Which of the following strings match the pattern [A-C]+[0-9]?x?

  1. ABx
  2. A9x
  3. x
  4. AB99x
  5. C0x
  6. abx

Problem 3 Intermediate

Write the simplified regular expression for the FSA described by this transition table. A is the start state and C is the only final state.

FromReadsTo
A0B
B1B
B0C
C1C

Problem 4 Intermediate

Which of the following strings are accepted by the regular expression 0(10)*1 U 11*0?

  1. 01
  2. 0101
  3. 01011
  4. 110
  5. 10
  6. 1110
  7. 00

Problem 5 Senior

Which of the following strings match the pattern (ab|ba)+c?[^xyz]. exactly (the whole string must be consumed)?

  1. abcq1
  2. babab
  3. abbac5!
  4. ababcx9
  5. bad_
  6. abc