Operating systems, software and translators · A level · OCR H446 1.2.2, AQA 7517 4.6.1.3, Eduqas A500QS 1.8 · about 45 min
Lexical analysis, syntax analysis, code generation and optimisation, then static and dynamic linking and the loader.
[1 mark]Put the stages of compilation in order.
Number the lines 1 to 4 to put them in the right order.
OptimisationLexical analysisSyntax analysisCode generation[1 mark]Which of these happen during lexical analysis?
Tick every answer that is true.
[1 mark]At which stage is a missing closing bracket detected?
[1 mark]What is an advantage of dynamic linking over static linking?
[1 mark]Which part of the operating system copies an executable from secondary storage into memory and adjusts its addresses so it can run?
[1 mark]What does this program print?
source = "total = total + 5 # add five"
code = source.split("#")[0]
tokens = code.split()
print(len(tokens), tokens)Write tokenise(text), the lexical analysis stage for a tiny language. text is a string that may contain several lines. It returns a list of (type, text) tuples in order, where type is one of:
- "KEYWORD": a word in KEYWORDS;
- "IDENTIFIER": any other word, made of letters, digits and underscores and not starting with a digit;
- "NUMBER": one or more digits;
- "OPERATOR": one character from OPERATORS;
- "PUNCTUATION": one character from PUNCTUATION.
Whitespace is skipped, and a # starts a comment that runs to the end of its line and produces no tokens. Do not use Python's own tokenize module or regular expressions: read the characters yourself.
Then print each token of source on its own line as <type> <text>, for example KEYWORD while. Finally print the symbol table: symbols: followed by each different identifier, in the order it first appears, separated by , . The robot stays still.
# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()
KEYWORDS = ["while", "if", "else", "def", "return"]
OPERATORS = "=+-*/<>"
PUNCTUATION = "():,"
source = "while gap > 20: # keep going\n gap = gap - step"
def tokenise(text):
tokens = []
return tokensPlan your program here, then type it in and press Run.
$.==, <= and >=. How does the lexer know whether = is finished?generate and fold, as a DIV instruction, and check that in speed + 60 / 4 the division is folded to PUSH 15.0.