📘 CodingMarble Learn

Finite State Machines and Formal Languages

A finite state machine (FSM) has a fixed set of states, an alphabet of input symbols, a start state, a transition function that says which state comes next for each symbol, and (for an acceptor) a set of accepting states. It reads an input string one symbol at a time; if it ends in an accepting state the string is accepted. A Mealy machine also gives an output on each transition. The strings an FSM accepts form a regular language, which can also be described by a regular expression. Languages with nesting (like brackets) need more power: they are context-free and are written with BNF rules or syntax diagrams.

🎬 Step-by-step story

  1. This is a turnstile at a metro gate, drawn as a machine. It has just two states: LOCKED (red) and UNLOCKED (green). The arrow marked "start" shows where it begins.
  2. Now the arrows appear. Each arrow is a transition: "in this state, if this input comes, go there". A coin moves LOCKED → UNLOCKED. A push moves UNLOCKED → LOCKED. Loops mean "stay here".
  3. Let us feed the input "coin, push". The yellow token starts at LOCKED, follows the coin arrow to UNLOCKED, then the push arrow back to LOCKED.
  4. Now "push, push, coin". Pushing a locked gate does nothing: the token loops and stays LOCKED. Only the coin moves it to UNLOCKED.
  5. Mark UNLOCKED as the accepting state (double ring). Feed "coin, push, coin": the machine ends in UNLOCKED, so the string is accepted. Ending in LOCKED would be rejected.
  6. Free play: press coin and push in any order. Before each press, predict the next state. Then check the token and the result.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

What is a "state" really?

It is all the machine remembers about the past. The turnstile only needs to remember "locked or unlocked", nothing else.

Why does every state need an arrow for every input?

So the machine always knows where to go. In the turnstile, even a push on LOCKED has an arrow: a loop back to LOCKED.

Does the order of inputs matter?

Yes. "coin, push" ends LOCKED but "push, coin" ends UNLOCKED. The token follows the arrows in order.

What does a loop arrow mean?

The input does not change the state. Pushing a locked gate leaves it locked.

How does the machine decide accept or reject?

Only the final state matters: if it is a double-ringed accepting state, the whole string is accepted.

Can a machine have more than one accepting state?

Yes, any number, including the start state. Try in free play: here UNLOCKED is the only one.

What is a finite state machine?

A finite state machine (FSM), also called a finite automaton, is a simple model of a computer with no memory except the state it is in. It has:

The machine reads the input string one symbol at a time from left to right. After the last symbol, if it is in an accepting state, the string is accepted; otherwise it is rejected. An FSM like this has no output except yes/no.

If every state has exactly one arrow for every symbol, it is a deterministic FSM (DFA). If a state may have several arrows for the same symbol (or none), it is non-deterministic (NFA). Every NFA can be turned into an equivalent DFA.

State transition diagrams and tables

The same machine can be drawn as a diagram or written as a table.

Current stateInputNext state
LOCKEDcoinUNLOCKED
LOCKEDpushLOCKED
UNLOCKEDcoinUNLOCKED
UNLOCKEDpushLOCKED

Example acceptor: states S0 (start, accepting) and S1. Input alphabet {0, 1}. Reading 1 swaps the state; reading 0 keeps it. This machine accepts exactly the binary strings with an even number of 1s: 1001 → S0 (accept), 111 → S1 (reject).

FSMs with output: Mealy machines

A Mealy machine gives an output on every transition. Each arrow is labelled input / output. It has no accepting states; its job is to translate input to output.

Example: the turnstile could output "open" or "beep": LOCKED —coin/open→ UNLOCKED, LOCKED —push/beep→ LOCKED. Another classic: a machine that outputs 1 whenever the input bit differs from the previous one (an edge detector). Mealy machines are used in traffic-light controllers, vending machines and simple ciphers.

(In a Moore machine the output depends only on the state, not on the transition.)

Sets, alphabets and words

An alphabet Σ is a finite set of symbols, e.g. Σ = {a, b}. A word (string) is a finite sequence of symbols; the empty word is ε. A language is a set of words, e.g. L = {ab, aab, aaab, …}.

Set notation: A = {1, 2, 3}; set-builder {x | x ∈ ℕ ∧ x < 4}; ∅ is the empty set. Operations: union A ∪ B, intersection A ∩ B, difference A \ B, Cartesian product A × B (all ordered pairs). Sets can be finite, countably infinite (like ℕ), or uncountable (like ℝ).

Syntax is the form of a word (is it built by the rules?); semantics is its meaning.

Regular expressions and regular languages

A regular expression is a short pattern that describes a set of strings:

A language is regular if it can be described by a regular expression, and exactly then it can be accepted by an FSM. Example: (0|1)*1 = binary strings ending in 1; 1*01* = strings with exactly one 0.

Everyday use: a simple pattern for an Indian vehicle plate like KA05MN1234 is [A-Z]{2}[0-9]{2}[A-Z]{1,2}[0-9]{4}. A postcode, phone number or date can be checked the same way.

Limit: an FSM cannot count without bound. The language {aⁿbⁿ} (same number of a then b) is not regular, so no FSM accepts it.

Context-free languages: BNF and syntax diagrams

Nested structures, like matching brackets or expressions inside expressions, need a grammar. A grammar has terminals (actual symbols), non-terminals (names in angle brackets) and production rules. Written in Backus–Naur form (BNF):

<digit>   ::= 0|1|2|3|4|5|6|7|8|9
<integer> ::= <digit> | <digit><integer>

The second rule is recursive, so it can make integers of any length. Recursion is what makes BNF more powerful than regular expressions: e.g. <S> ::= ab | a<S>b produces {aⁿbⁿ}.

EBNF adds shortcuts: {x} for repetition, [x] for optional. A syntax diagram shows the same rules as railway tracks: follow any path from left to right; ovals are terminals, rectangles are non-terminals.

A derivation (parse) tree shows how a word is built from the start symbol: e.g. 42 → <integer> → <digit><integer> → 4 <digit> → 4 2. Compilers use such trees to check programs.

Try it: predict, then check

In the 3D free play, try to find a short input that ends in LOCKED even though it contains two coins. (Hint: what does the last symbol have to be?)

On paper: draw an FSM with alphabet {0, 1} that accepts strings ending in 1. You need only two states. Test it on 1011 (accept) and 110 (reject).

Key formulas and definitions

Worked examples

1. Trace the turnstile on "coin, coin, push, coin". Which state does it end in?

LOCKED → coin → UNLOCKED → coin → UNLOCKED → push → LOCKED → coin → UNLOCKED. It ends UNLOCKED (accepted).

2. Using the even-1s machine (S0 accepting, 1 swaps state), is 10110 accepted?

It has three 1s. S0 →1→ S1 →0→ S1 →1→ S0 →1→ S1 →0→ S1. Ends in S1: rejected.

3. Write a regular expression for binary strings that start with 1 and end with 0.

1(0|1)*0. The middle part can be anything.

4. Which of these match a(b|c)*d: ad, abcd, abd, acbx?

ad (zero repeats), abcd and abd match. acbx does not end in d.

5. Using <integer> ::= <digit> | <digit><integer>, show that 305 is valid.

<integer> → <digit><integer> → 3<integer> → 3<digit><integer> → 30<integer> → 30<digit> → 305. Valid.

6. Design a Mealy machine that outputs 1 when the current bit differs from the previous bit (start: assume previous = 0).

States P0 (last bit 0, start) and P1 (last bit 1). P0 —0/0→ P0, P0 —1/1→ P1, P1 —1/0→ P1, P1 —0/1→ P0. Input 0110 gives output 0101.

Common mistakes

Practice quiz

1. An FSM accepts a string when:
2. In a Mealy machine, outputs are written:
3. Which string matches the regex 10*1?
4. Which cannot be recognised by any FSM?
5. In BNF, the symbol ::= means:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is a finite state machine in simple words?

A model with a few states that moves from one state to another depending on each input it reads, like a turnstile switching between locked and unlocked.

What is the difference between a Mealy machine and an FSM acceptor?

An acceptor only says yes/no at the end using accepting states. A Mealy machine has no accepting states but gives an output on every transition.

How are regular expressions and FSMs related?

They describe the same family of languages (regular languages): every regular expression can be turned into an FSM and vice versa.

Where this is taught

NetherlandsHAVO 4 (bovenbouw, 2e fase)Foundations
NetherlandsVWO 4 (bovenbouw, 2e fase)Foundations
England (GCSE, A level)Year 124.4 Theory of computation (part 1)
England (GCSE, A level)Year 134.4 Theory of computation (A-level)
Germany (Bavaria)Jahrgangsstufe 13Formal languages and automata

Related lessons

All Computer Science lessons