Theory of computation · A level · AQA 7517 4.4.3.1, Eduqas A500QS 1.8 · about 25 min
Production rules, syntax diagrams, a grammar for robot programs, recursive descent, and why BNF can describe what a regex cannot.
[1 mark]In BNF, what does ::= mean?
[1 mark]Using <integer> ::= <digit> | <digit><integer> and <move> ::= <direction><integer> with <direction> ::= F | B | L | R, which are valid moves?
Tick every answer that is true.
[1 mark]Why can BNF describe some languages that regular expressions cannot?
[1 mark]In a syntax diagram, what does a path that loops back round a box show?
[1 mark]Given <s> ::= ab | a<s>b, how many times must the rule be used to produce aaabbb?
[1 mark]What does this program print?
def s(text, i):
if text[i:i + 2] == "ab":
return i + 2
if text[i:i + 1] == "a":
j = s(text, i + 1)
if j != -1 and text[j:j + 1] == "b":
return j + 1
return -1
for t in ["aabb", "aab", "abb"]:
print(t, s(t, 0))Write a recursive descent checker for the robot command language above and use it on the strings in tests.
- Write one function per rule you need, each taking (text, i) and returning the position after the match, or -1 if it does not match. At least program, command and integer.
- A string is valid only if program matches all of it.
- Check the nesting with your functions. Do not use the re module: no regex can check matched brackets.
- For each string in tests, in order, print the string, a space, then valid or invalid. For example F20R90 valid. Eight lines in all.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
tests = ["F20R90", "[4F20R90]", "[2F10[3L5]]", "F20R", "[4F20", "4F20", "[3]", "F1]"]
def integer(text, i):
return -1Plan your program here, then type it in and press Run.
S, meaning stop, as a command on its own. Change both the BNF and the syntax diagram.+ or -.<integer> rule regular? Write a regex for it. Is <program> regular?