Week 4 · Lesson 5 · 55 minutes

How programs are translated

I can evaluate a translation approach for a real task.

Planned lesson. Follow your teacher’s teaching order.

Source workbook pages: 28.

Resources

Vocabulary

Vocabulary
compiler: translates code into another form before that form runs; bytecode: an intermediate instruction form for a virtual machine
55-minute lesson sequence
  1. 0–5 · Retrieve machine versus high-level code.
  2. 5–15 · Model the three translation paths with CPython as a hybrid example.
  3. 15–25 · Students draw and label task 1 before discussion.
  4. 25–45 · Compare the three scenarios and justify choices with costs.
  5. 45–50 · Each student identifies one oversimplified compiler/interpreter claim.
  6. 50–55 · Collect the CPython exit explanation; assign homework.

Try before notes

Why can a high-level program not be treated as the CPU’s own machine instructions?

Build the idea

Source code expresses a program in a language for people. A processor executes its machine instruction set. Translation connects these representations, but different implementations can use different stages.

Ahead-of-time compilation translates before execution. An interpreter executes a representation at runtime. Bytecode is an intermediate form, and JIT compilation can translate selected code while a program runs. These mechanisms can coexist.

CPython compiles source to bytecode and runs that bytecode in its virtual machine. Therefore, calling a language simply compiled or interpreted can hide important implementation details. Successful translation also does not prove correct results.

source → compiler → bytecode → virtual machine
source → native compiler → machine code → CPU

See the method

A compiler can produce machine code before execution. An interpreter executes a program representation at runtime. Systems can combine both. CPython compiles Python source to bytecode and executes it in a virtual machine. A JIT compiler translates selected code during execution. Translation and runtime can detect different errors; none proves a program correct.

Predict then reveal

A program translates successfully but computes a rectangle area using width+height. Is the compiler necessarily faulty?

Reveal reasoning

No. The expression can be valid code with the wrong algorithm. Translation checks and runtime execution do not know that the requirement was width×height. A test against a known area can expose the logical error.

An application distributes bytecode. Does it run on every device without anything else?

Reveal reasoning

No. It needs a compatible runtime and any required libraries or platform services. Intermediate code can improve portability, but it does not eliminate all dependencies.

Work one step together

Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.

Practice

  1. Draw three paths: source → ahead-of-time native code → CPU; source → bytecode → interpreter; source or bytecode → JIT → native execution. Label when translation occurs. Explain one portability limit.

Your turn

Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.

Practice

  1. Draw three paths: source → ahead-of-time native code → CPU; source → bytecode → interpreter; source or bytecode → JIT → native execution. Label when translation occurs. Explain one portability limit.
  2. Recommend an approach for rapid prototyping, a performance-critical repeated calculation and a cross-platform tool. Compare startup cost, repeated execution, debugging and runtime needs.

Check and explain

A valid program can still produce the wrong answer. Translation is not a proof of correctness.

Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.

Show what you know

Why is “Python is never compiled” an unsafe claim?

HL challenge when ready

Can a compiler accept a program that later divides by zero?

Week 4 homework

Show working. Keep code and output. Stop at 25 minutes and mark where you reached.

  1. All jobs arrive at 0 in order A,B. A needs 5 units and B needs 2. Draw FCFS and round robin with quantum 2. Ignore switch cost. Give completion times. Python retrieval: start queue = ["A","B","C"]. Move unfinished A from front to back. Predict, then print the queue.
  2. A device generates rare events while its controller sleeps. Recommend polling or interrupts with one benefit and one limitation.
  3. Explain one deadlock using two processes and two resources. Give a change that breaks the problem.
  4. Explain why a bytecode interpreter and a compiler can both be part of one language implementation.

Source workbook pages: 29.

Weekly homework (Word)

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Compile a valid expression and evaluate it. Predict when Python checks its syntax.

No imports needed.

Hint and starter code

Compilation translates source into another form. Use only this fixed trusted expression, not user-supplied text.

# Compilation translates source into another form.
code = compile("2 + 3", "demo", "eval")
print(eval(code))
Open this starter in Python playground →
Prompt 2

Distinguish a syntax error from an error during execution. Change the expression to 1 / 0.

No imports needed.

Hint and starter code

Syntax is the language’s structure rules; a runtime error occurs while valid code executes.

# Syntax is the language’s structure rules; a runtime error occurs while valid code executes.
expression = "2 +"
try:
    code = compile(expression, "demo", "eval")
    print(eval(code))
except (SyntaxError, ZeroDivisionError) as error:
    print(type(error).__name__)
Open this starter in Python playground →