(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]
(a) -07 identified as valid(1)
(a) 42 identified as valid; deduct 1 mark for each invalid string (+-5 or 5-) also given as valid (minimum 0 for part (a))(1)
(b) <unsigned> is defined in terms of itself (recursion), so another digit can be added any number of times(1)
(c) <name> ::= <letter> | <letter><rest> (starts with a letter, optionally followed by more); allow an equivalent left-recursive form such as <name> ::= <letter> | <name><letter> | <name><digit> for both (c) marks(1)
(c) <rest> ::= <letter> | <digit> | <letter><rest> | <digit><rest> (recursive rule for any mix of letters and digits); accept any equivalent rules, e.g. <char> ::= <letter> | <digit> and <rest> ::= <char> | <char><rest>(1)
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]
(a) 1001 accepted (two 1s, finishes in A)(1)
(a) 0111 not accepted (three 1s, finishes in B)(1)
(b) Correct pattern for a pair of 1s with any number of 0s between them, e.g. 10*1(1)
(b) Fully correct expression that also allows 0s before/after and any number of pairs (including none), e.g. 0*(10*10*)* or (0|10*1)*; accept any equivalent expression that matches exactly the strings with an even number of 1s(1)
(c) An FSM has a fixed, finite number of states (no other memory), so it cannot remember/count an unbounded number of 0s to match against the 1s(1)
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]
(a) $\{0, 2, 4, 6\}$; order of members not important; must include 0(1)
(b) $\{2, 4, 6\}$; follow through from an incorrect $B$ in (a)(1)
(c) $\{1, 3, 5\}$; follow through from (a)(1)
(d) 24; follow through: $6 \times |B|$ from (a)(1)
(e) No, because $0 \in B$ but $0 \notin A$ (so not every member of $B$ is in $A$); 'No' alone scores 0; follow through from (a) only if the justification is consistent(1)
(f) Infinite and countable / countably infinite; accept 'its members can be counted off / put in one-to-one correspondence with the natural numbers'; both parts needed(1)
Answer: (a) {0, 2, 4, 6} (b) {2, 4, 6} (c) {1, 3, 5} (d) 24 (e) No: 0 is in B but not in A (f) Countably infinite
Examiner tip: AQA's $\mathbb{N}$ starts at 0, so $B$ contains 0 – forgetting this loses (a) and makes (e) look like a proper subset when it is not.
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]
(a) 0101110, all seven bits correct for 2 marks(1)
(a) 1 mark if exactly one bit is incorrect, or if the first four bits 0101 are correct(1)
(b) Outputs 1 when the input bit is different from the previous input bit and 0 when it is the same (with the previous bit taken as 0 at the start); accept 'detects a change/edge in the input'; do not accept 'outputs the input' or 'inverts the input'(1)
(c) A Mealy machine produces an output on each transition (the output depends on the current state and the input); an FSM without output only accepts or rejects the input (has accepting states); accept 'Mealy machine has outputs on its transitions / does not need accepting states'(1)
(d) Both rows needed: S1, 0, S0, 1 and S1, 1, S1, 0(1)
Answer: (a) 0101110 (b) Outputs 1 when a bit differs from the previous bit, else 0 (c) Mealy machine gives an output on each transition; an FSM without output only accepts/rejects (d) S1,0→S0, output 1; S1,1→S1, output 0
Examiner tip: Write the output digit for each transition as you go – it is the number after the slash, not the name of the state you land in.
(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]
(a) ac, abc, abccc identified as matching and no others: 2 marks(1)
(a) 1 mark if exactly one error (one omission or one extra string)(1)
(b) ab(ab)*; accept (ab)(ab)* or (ab)*ab(1)
(c) x*yx*; accept (x)*y(x)*(1)
(d) (p|q) followed by 0+ (accept 00*)(1)
(d) … followed by z? at the end (accept (z|) or alternatives that clearly allow 0 or 1 z only); whole expression must not allow any other strings, e.g. (p|q)0*z? scores max 1(1)
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]
(a) S2, accepted (both needed)(1)
(b) All strings (over {a, b}) containing at least two a's / two or more a's; do not accept 'containing two a's' or 'ending in a'(1)
(c) b*ab*a (or equivalent) correctly ensuring two a's are present before …(1)
(c) … (a|b)* at the end; accept any fully equivalent expression, e.g. (a|b)*a(a|b)*a(a|b)* for 2 marks(1)
(d) Add a new (non-accepting) state, e.g. S3, and change the transition from S2 on input a to go to S3 (S2 on input b still loops to S2)(1)
(d) S3 loops to itself on both a and b / is a dead (trap) state; S2 remains the only accepting state(1)
Answer: (a) S2, accepted (b) Strings containing at least two a's (c) b*ab*a(a|b)* (d) Add non-accepting trap state S3; S2 --a--> S3; S3 loops on a and b; S2 loops on b
Examiner tip: In (b), 'contains two a's' is ambiguous – say at least two, because S2 loops on a and b.
(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]
(a) (3,(4,5)), ((),8) and (((6))) identified as valid and no others: 2 marks(1)
(a) 1 mark if exactly one error (one omission or one extra)(1)
(b) An <item> can only be a single digit (or a list), so 12 is not an item; and there is no comma between 1 and 2 so it is not two items. Accept 'each item is one digit and items must be separated by commas'(1)
(c) Lists can be nested to any (unlimited) depth / the brackets must be matched (each ( needs a matching ))(1)
(c) A regular expression (or FSM) has no way to count / remember an unlimited number of open brackets (it has a finite number of states); BNF can because its rules are recursive (<list> is defined in terms of <item>, which can be a <list>). Accept 'regular expressions cannot express recursion/nesting'(1)
(d) e.g. <number> ::= <digit> | <digit><number>and<item> ::= <number> | <list>; accept <number><digit> (left recursion); both rules needed(1)
Answer: (a) (3,(4,5)), ((),8), (((6))) (b) An item is a single digit and items need commas between them (c) Lists nest to any depth so brackets must be matched; a regex/FSM cannot count unlimited brackets, BNF uses recursion (d) <number> ::= <digit> | <digit><number>; <item> ::= <number> | <list>
Examiner tip: For (c), 'BNF is more powerful' scores nothing on its own – you must name nesting/matched brackets and link it to recursion or the finite memory of an FSM.
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 state
Input
Next state
R0
0
R0
R0
1
R1
R1
0
R2
R1
1
R2
0
R2
1
(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]
(a) R1, 1 → R0; R2, 0 → R1; R2, 1 → R2: all three correct for 2 marks(1)
(a) 1 mark for any two correct(1)
(b) R0 only, because being in R0 means the remainder is 0 so the number is divisible by 3(1)
(c) R0 → R1 → R2 → R1 → R0 → R0 (accept without the initial R0), accepted; follow through from their table in (a)(1)
(d) It can be accepted/recognised by an FSM (this one), which has a finite number of states because only the remainder (3 possibilities) needs to be remembered, not the whole number; accept 'it can be represented by a regular expression'. Do not accept 'because it is countable'(1)
Answer: (a) R1,1→R0; R2,0→R1; R2,1→R2 (b) R0: remainder 0 (c) R0, R1, R2, R1, R0, R0 – accepted (10010 = 18) (d) It is accepted by a finite state machine (only the remainder needs storing)
Examiner tip: Use the rule given: new remainder = (2 × old remainder + bit) mod 3. Students who just guess a pattern usually get R2 wrong.