A1.4.1Translation processes of interpreters and compilers
0/12
A1.4 TranslationA1.4.1

Translation processes of interpreters and compilers

Evaluate the translation processes of interpreters and compilers

HL only≈ 60 minCommand term: EvaluateFree lesson

In this lesson

  • The mechanics and use cases of compilers, interpreters, bytecode interpreters and just-in-time (JIT) compilation
  • Their differences in error detection, translation time, portability and applicability
  • Choosing a translation method for rapid development and testing, performance-critical applications and cross-platform development

LearnWhy translation is needed

A processor can only execute machine code: binary instructions from its own instruction set. Programmers write source code in high-level languages. Something has to translate one into the other.

    0 of 4

    LearnInside a translator

    Compilers and interpreters start with the same analysis stages. They differ in what they do afterwards, and when.

      0 of 5

      CheckLexical analysis

      The statement if total > 100: print("Free delivery") is split into tokens. Classify these tokens.

      1. if
      2. total
      3. >
      4. 100
      5. "Free delivery"
      6. print

      LearnCompilers

        0 of 3

        LearnInterpreters

          0 of 4

          LearnBytecode interpreters and JIT compilation

            0 of 4

            LabWhen is the error found?

            The same short program contains one error. Run it with a compiler (imagine it written in a compiled, statically typed language such as C), a line-by-line interpreter and a bytecode interpreter such as CPython. Watch what runs before the error is reported. In the third case the user types 0 when asked for the number of boxes.

            1. 1price = 12
            2. 2qty = 3
            3. 3boxes = int(input("Boxes: "))
            4. 4print("Price:", price)
            5. 5total = price * * qty
            6. 6print("Total:", total)
            7. 7per_box = total / boxes
            8. 8print("Per box:", per_box)
            Choose how to run the program.

            Run every kind of error with all three translators to complete this activity.

            LabTranslation time versus run time

            The same program, with a loop, is run with each approach. Compare a developer, who changes the code and tests it again and again, with a user, who runs the finished program. Then move from a few loop runs to a million.

            The developer edits the code and tests it again and again: every change must be translated before it runs.

            Fastest here: Interpreter (from source), 10 ms

            • Compiler (ahead of time) 7.50 s
              whole program → machine code before it runs · translation 7.50 s, execution 100 µs
            • Interpreter (from source) fastest10 ms
              each statement analysed and run, every time · translation 7.0 ms, execution 3.0 ms
            • Bytecode interpreter 11 ms
              source → bytecode once, then the bytecode is interpreted · translation 7.5 ms, execution 3.0 ms
            • JIT compiler 11 ms
              bytecode, with frequently run code compiled to machine code at run time · translation 7.5 ms, execution 3.0 ms

            translation execution. Bars are drawn to the same scale for the current settings. Try 1–10 and at least 100,000 loop runs in both views to complete this activity.

            LearnComparing the approaches

            Compiler (ahead of time)Interpreter (source)Bytecode interpreterJIT compiler
            Error detectionSyntax, type and declaration errors at compile time, all reported before the program runs; data-dependent errors at run timeEach error only when its line is reached; code that never runs is never checkedDepends on the language: statically typed (Java, C#) – syntax, type and declaration errors when compiled to bytecode; dynamically typed (CPython) – mainly syntax errors, the rest at run timeAs for bytecode (decided by the language, not by the JIT); JIT compilation adds no new checks
            Translation timeAll of it before execution: a long build, repeated after every change; none at run timeNone in advance; repeated during execution, every time a statement runsCompiled to bytecode before the program runs (often before it is shipped), then interpreted at run timeBytecode produced in advance, plus run-time compilation of hot code (warm-up), which is then cached
            Execution speedFastestSlowestFaster than source interpretationClose to compiled once warmed up
            PortabilityExecutable is platform-specific; recompile for each targetSource runs anywhere with the interpreterSame bytecode runs on any platform with the virtual machineBytecode is portable; the machine code it generates is not
            ApplicabilityPerformance-critical software: operating systems, game engines, trading systems, embedded devicesRapid development and testing, scripts, teachingCross-platform applications and toolsLong-running cross-platform applications: servers, large desktop and web apps

            CheckWhich approach fits?

            Choose the most suitable translation approach for each scenario.

            1. A trading platform must react to market data within microseconds
            2. A scientist tries out ten versions of a data-cleaning script in one afternoon
            3. A company's customer system must run on Windows, macOS and Linux from one build
            4. The firmware of a pacemaker runs on a tiny processor with very little memory
            5. Students in a first programming course experiment with short programs
            6. A web server runs the same code for months and handles millions of requests

            CheckQuick check

            Why can the same Java program run on Windows, macOS and Linux without being recompiled for each?

            ExamWorked exam answer

            Outline how just-in-time compilation improves the performance of a program distributed as bytecode.

            [3]
              Try writing your own answer first.

              ReviewMistakes examiners see

              Misconception

              “A language is compiled or interpreted” is too simple: it is the implementation that compiles or interprets. Python can be run by CPython (bytecode interpreter) or PyPy (JIT).

              Misconception

              Bytecode is not machine code. It is an intermediate form that still needs a virtual machine.

              Misconception

              A compiler does not find every error. Data-dependent errors such as division by zero still happen at run time.

              Misconception

              JIT is not the same as ahead-of-time compilation: it compiles during execution, on the user's computer.

              Exam tip

              In an evaluate question, use the guide's criteria by name (error detection, translation time, portability, applicability), link each to the scenario, and finish with a justified recommendation.

              Exam practiceExam-style questions

              5 questions · 14 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.

              Scenario

              A start-up is building a fitness app. It needs a quick prototype to show investors, which will change after every investor meeting, a video-analysis component that measures a user's movements in real time, and a desktop version that runs on Windows, macOS and Linux.

              (a)Outline · AO2[2]

              Outline one difference between the way a compiler and an interpreter translate source code.

              (b)Describe · AO2[2]

              Describe what happens during syntax analysis.

              (c)Explain · AO3[3]

              Explain why using an interpreter is suitable for developing and testing the prototype.

              (d)Explain · AO3[3]

              The desktop version will be distributed as bytecode. Explain one disadvantage of this compared with distributing native executables.

              (e)Evaluate · AO3[4]

              Evaluate the use of ahead-of-time compilation for the real-time video-analysis component.

              0 / 14

              0 of 5 questions marked. Mark every question to see your total.

              Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.