| English | 意味 |
|---|---|
| compiler/kəmˈpaɪlə/ | コンパイラ |
| interpreter/ɪnˈtɜːprɪtə/ | インタープリタ |
| lexical analysis/ˈleksɪkl əˈnæləsɪs/ | レキシカル解析 |
| tokens/ˈtəʊkənz/ | トークン |
| syntax analysis/ˈsɪntæks əˈnæləsɪs/ | 構文解析 |
| abstract syntax tree/ˈæbstrækt ˈsɪntæks triː/ | 抽象構文木 |
| syntax error/ˈsɪntæks ˈerə/ | 構文エラー |
| semantic analysis/səˈmæntɪk əˈnæləsɪs/ | 意味解析 |
| code generation/kəʊd ˌdʒenəˈreɪʃn/ | コード生成 |
| code optimisation/kəʊd ˌɒptɪmaɪˈzeɪʃn/ | コード最適化 |
The bug that cost a spacecraft, found by a comma
- NASA's Mariner 1 was destroyed 293 seconds after launch in 1962. The failure is often blamed on a single wrong character in the guidance software.
- The story is retold so often because it is the fear every programmer knows: a compiler happily translating a program that says something you did not mean.
- A compiler is not one process but five, and each one catches a different class of mistake. Knowing which stage catches what tells you where any given error comes from.
- This lesson is how an interpreter 解释器 runs a program without producing anything, and the five stages a compiler 编译器 goes through.
How an interpreter runs a program
- An interpreter translates and runs the source at the same time, one statement at a time.
- For each statement it reads the line, analyses it, checks the types, then executes the action immediately, and moves to the next.
- It produces no executable file: the translation exists only in memory and is thrown away. That is the syllabus's exact point, that it can execute a program without producing a translated version.
- It reports an error the moment it reaches the offending line and usually stops, which gives quick feedback while developing. Every run re-translates, so it is slower, and both the source and the interpreter must be present.

Translate once and keep it, or translate and run and forget
インタプリタとは:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
行単位で動作し、到達したエラーを報告する;実行可能ファイルとして保存されるものはなく、一般的に遅い。
インタプリタは、翻訳版を生成せずにプログラムを実行できる。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
翻訳はメモリ内でのみ文単位で行われ、破棄される。そのため、インタプリタはプログラムが実行されるたびに存在する必要がある。
Stage 1: lexical analysis
- Lexical analysis 词法分析 groups the individual characters of the source into tokens 词法单元: keywords, identifiers, operators and literals.
- Whitespace and comments are discarded, since they have no meaning to the compiler, and identifiers are entered into a symbol table.
- So
total ← count * 2becomes the token sequence identifier, assignment, identifier, operator, literal. The lexer does not know or care whether that makes sense.
Lexical分析(レキサー)が変換するもの:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
レキサーは文字列をトークンにまとめ、空白/コメントを除去します;パースはその後木を構築します。
Lexical分析はソースの文字列を ____ にまとめ、空白文字やコメントを除去する。
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
キーワード、識別子、演算子、リテラル。レキサーはシーケンスが有効なプログラムかどうかを判断しない。
Stage 2: syntax analysis
- Syntax analysis 语法分析, or parsing, checks that the sequence of tokens fits the language's grammar, and builds an abstract syntax tree 抽象语法树 representing the structure.
- A missing bracket, a missing
ENDIFor a keyword in the wrong place is caught here: that is what a syntax error 语法错误 is, and it is why the compiler can report one without ever running the program.

The grammar the parser checks against
Stage 3: semantic analysis
- Semantic analysis 语义分析 checks that a syntactically valid program actually makes sense: every variable is declared before use, the types on each side of an assignment are compatible, a function is called with the right number of arguments.
total ← "seven" * 2is perfectly good syntax and complete nonsense. Only this stage catches it.- This is the distinction the exam tests: syntax is form, semantics is meaning.
以下のうち、構文解析ではなく意味解析によってチェックされるものはどれか?
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
括弧は文法なので構文;空白文字はレキサー;レジスターはコード生成です。宣言と型は意味です。
Stages 4 and 5: code generation and optimisation
- Code generation 代码生成 walks the tree and emits the target machine code, choosing registers and laying out the data.
- Code optimisation 代码优化 improves that code without changing what it does: removing redundant work, computing constant expressions at compile time, and reordering instructions to suit the pipeline.
- The output is a stand-alone executable that runs without the compiler present.

Five stages, and each one catches something the last could not
コンパイルのフェーズ
コンパイラーがソースに対して行う処理を追跡してください。各フェーズは出力を次のフェーズに渡します——文字列がトークンになり、トークンが木になり、木が最適化された機械語になります。
各コンパイラフェーズとそれが行う処理を一致させてください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
各フェーズがプログラムを一段階進め、トークン、次に木、確認、そして最適化されたコードへと変換する。
コンパイラの工程を正しい順に並べ替えてください。
難易度 3/5 · 200 コイン + 今日初回完了時に小ボーナス付与
Lexical → 構文解析 → 意味解析 → コード生成 → 最適化。
コード最適化の目的は:
難易度 1/5 · 100 コイン + 今日初回完了時に小ボーナス付与
最適化は生成されたコードを改善する(定数折りたたみ、重複の削除、パイプライン向けのreorder)。
Worked example: which stage catches which error
x ← 5 +: the tokens do not fit the grammar, an operator with no right operand, so syntax analysis.x ← y + 1whereywas never declared: the form is fine but the meaning is not, so semantic analysis.IF a > b THEN OUTPUT awith noENDIF: syntax analysis again.- A program that runs and prints the wrong average: no stage catches it. That is a logic error, and only testing finds it. Name the stage and why the earlier ones let it through.
各誤った行とそれを検出するコンパイラフェーズを一致させてください。
難易度 2/5 · 140 コイン + 今日初回完了時に小ボーナス付与
構文は形式、意味論は意味であり、論理エラーは両方において有効だが意図が誤っている。
Worked example: compare the two translators
- Give two differences between how a compiler and an interpreter handle a program. [4]
- A compiler translates the whole program before it runs and produces an executable, which then runs without the compiler; an interpreter translates and executes one statement at a time and produces no executable, so the interpreter must be present every run.
- A compiler reports all the errors together after translation; an interpreter reports the first error when it reaches that line and then stops.
- Each mark is a pairing: say what one does and what the other does instead.
Marks that slip away
- An interpreter produces no translated version. That is the phrase the syllabus uses and the mark it awards.
- The lexer produces tokens and discards whitespace and comments; it does not check whether the program is valid.
- Syntax is form, semantics is meaning. An undeclared variable is a semantic error, not a syntax error.
- Optimisation must not change what the program does, only how quickly or compactly it does it.
You've got it
- an interpreter translates and executes one statement at a time, producing no executable, reporting the first error at its line, and re-translating on every run
- lexical analysis makes tokens and discards whitespace and comments; syntax analysis checks the grammar and builds an abstract syntax tree, catching syntax errors
- semantic analysis checks meaning: declarations, types, argument counts
- code generation emits machine code and optimisation improves it without changing behaviour, giving a stand-alone executable