الانتقال إلى المحتوى

Labs · 9618 A2 16.2

Inside a Compiler

Do the compiler's work yourself: split code into tokens with a symbol table, write the object code, then optimise it.

8 levelsFree bench

In the exam

9618/03 Specimen Paper 2021 · Question 4 · 7 marks

(a)(i) The output of the lexical analysis stage forms the input to the next stage. Identify this stage. (ii) State two tasks that occur at this stage. (b) One reason for optimisation is to minimise the memory used. State another reason. (c) Suggest what a compiler could do to optimise A ← B + 2 * 6. (d) Suggest what a compiler could do to optimise the code for X ← A + B and Y ← A + B + C (seven lines: LDD 436, ADD 437, STO 612, LDD 436, ADD 437, ADD 438, STO 613).

Points that gain marks

  • (a)(i) Syntax analysis.
  • (ii) Any two: parsing / construction of a parse tree; checking that the rules of grammar (syntax) have been obeyed; production of an error report.
  • (b) To minimise the execution time.
  • (c) Replace 2 * 6 with the value 12.
  • (d) Remove the second LDD 436 (line 04) …
  • … and remove the second ADD 437 (line 05) …
  • … because the value needed is already in the accumulator.

In this lab: Levels 5 and 6 are parts (d) and (c).

9618/33 October/November 2022 · Question 4 · 4 marks

Draw one line to connect each stage of compilation (lexical analysis, syntax analysis, code generation, optimisation) to its most appropriate description.

Points that gain marks

  • Lexical analysis: converting a sequence of characters into a sequence of tokens.
  • Syntax analysis: using parsing algorithms to interpret the meaning of a sequence of tokens.
  • Code generation: converting an intermediate representation of source code into an executable form.
  • Optimisation: minimising a program's execution time and memory requirement.

In this lab: Level 8 uses these descriptions.

9618/32 October/November 2022 · Question 2 · 4 marks

Lexical analysis and syntax analysis are stages in the compilation of a program. (a) Identify two other stages that take place during the compilation of a program. (b) Outline the purpose of syntax analysis.

Points that gain marks

  • (a) Code generation; optimisation.
  • (b) It checks that the code (the tokens) matches the grammar of the language.
  • Syntax errors are reported.
  • A parse tree is produced.

In this lab: Level 8. Syntax analysis with grammar rules is in the BNF lab.

More real experiments

تسجيل الدخول أو إنشاء حساب

IGCSE, A-Level & AP