Question bank › A-level › A-level Computer Science › Theory of computation

FSMs, regular expressions and BNF

10 questions52 marksabout 60 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Core4 marks

The finite state machine (FSM) below has start state A and accepting state C. The input alphabet is {0, 1}.

(a) State whether the input 10101 is accepted. Justify your answer. [1]

(b) State whether the input 0110 is accepted. Justify your answer. [1]

(c) Describe the set of strings that this FSM accepts. [1]

(d) Write a regular expression that defines the same language as this FSM. [1]

ABC010110
2Standard4 marks

A regular expression is a(b|c)*d.

(a) The strings below are tested against the whole regular expression. Identify which strings match.

ad   abcd   abbcb   acbbd   abdd   aabd [2]

(b) Write a regular expression for the set of all strings of 0s and 1s that start with 1 and end with 0. [2]

3Standard5 marks

A number format is defined using Backus-Naur Form (BNF):

<digit> ::= 0|1|2|3|4|5|6|7|8|9
<unsigned> ::= <digit> | <digit><unsigned>
<number> ::= <unsigned> | +<unsigned> | -<unsigned>

(a) State which of these are valid <number>s: -07   +-5   5-   42 [2]

(b) Explain how the definition of <unsigned> allows numbers of any length. [1]

(c) Write BNF rules for a <name> that must start with a letter followed by any number of letters or digits. You may assume <letter> and <digit> are already defined. [2]

Stuck on FSMs, regular expressions and BNF?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Hard5 marks

The FSM below has start state A, which is also the only accepting state. The input alphabet is {0, 1}.

(a) State whether each input is accepted: 1001 and 0111. [2]

(b) Write a regular expression for the strings accepted by the FSM. [2]

(c) Explain why an FSM cannot be used to recognise the language of strings with n 0s followed by exactly n 1s for any n. [1]

AB1100
5Core6 marks

Two sets are defined as follows, where $\mathbb{N}$ is the set of natural numbers $\{0, 1, 2, 3, \ldots\}$:

$$A = \{1, 2, 3, 4, 5, 6\}$$$$B = \{2x \mid x \in \mathbb{N} \wedge x < 4\}$$

(a) List the members of set $B$. [1]

(b) State the set $A \cap B$. [1]

(c) State the set $A \setminus B$ (the difference $A - B$). [1]

(d) State the cardinality of the Cartesian product $A \times B$. [1]

(e) State whether $B \subset A$. Justify your answer. [1]

(f) The set $E = \{2x \mid x \in \mathbb{N}\}$. State whether $E$ is finite or infinite and whether it is countable. [1]

6Standard5 marks

Figure 1 shows a Mealy machine with start state S0. Each transition is labelled input/output. The input alphabet is {0, 1}.

(a) The input string 0110100 is processed by the machine. State the output string produced. [2]

(b) Describe the purpose of this machine, in terms of the relationship between each input bit and the output bit it produces. [1]

(c) Explain how a Mealy machine differs from a finite state machine without output. [1]

(d) Write down the row(s) of a state transition table for this machine that show what happens when the machine is in state S1. Use the columns: current state, input, next state, output. [1]

S0S1start1/10/10/01/0
7Standard6 marks

(a) The regular expression ab?c+ is tested against each whole string below. State which strings match.

ac   abc   abbc   abccc   ab [2]

(b) Rewrite the regular expression (ab)+ so that it defines the same language without using the + metacharacter. [1]

(c) Write a regular expression for the set of all strings over the alphabet {x, y} that contain exactly one y. [1]

(d) A stock code consists of either the letter p or the letter q, followed by one or more 0s, optionally followed by a single letter z. For example p0, q000z.

Write a regular expression that defines all valid stock codes. [2]

8Hard6 marks

Figure 2 shows a finite state machine (FSM) with start state S0 and accepting state S2. The input alphabet is {a, b}.

(a) The input bbabab is processed. State the final state and whether the input is accepted. [1]

(b) Describe the set of strings accepted by this FSM. [1]

(c) Write a regular expression that defines the same language as this FSM. [2]

(d) The FSM is to be changed so that it accepts only strings that contain exactly two a's. Describe the changes needed. [2]

S0S1S2aabba, b
9Hard6 marks

A language for writing nested lists is defined using Backus-Naur Form (BNF):

<digit> ::= 0|1|2|3|4|5|6|7|8|9
<item>  ::= <digit> | <list>
<items> ::= <item> | <item>,<items>
<list>  ::= () | (<items>)

(a) State which of the following are valid <list>s.

(3,(4,5))   (7,)   ((),8)   (12)   (((6))) [2]

(b) Explain why (12) is not a valid <list>. [1]

(c) Explain why the language of valid <list>s could not be defined using a regular expression. [2]

(d) Rewrite the rule for <item>, adding any new rule(s) needed, so that an item can be a whole number with one or more digits. [1]

10Stretch5 marks

An FSM reads a binary number one bit at a time, most significant bit first, and must accept the number only if it is divisible by 3. It has three states: R0 (start state), R1 and R2. Being in state Rk means the value of the bits read so far leaves remainder k when divided by 3.

When a bit $b$ is read, the value read so far, $v$, becomes $2v + b$.

Table 1 is a partly completed state transition table for the FSM.

Current stateInputNext state
R00R0
R01R1
R10R2
R11 
R20 
R21 

(a) Complete Table 1 by giving the three missing next states. [2]

(b) State which state(s) should be accepting states. Explain your answer. [1]

(c) Show the sequence of states the FSM passes through when the input is 10010, and state whether it is accepted. [1]

(d) The set of binary numbers divisible by 3 is infinite. Explain why it is nevertheless a regular language. [1]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session