Compilation and interpretation
導入済み
13:59
リソース、コンパイラ & RPN
ブラウザ、音楽プレイヤー、ゲームを開きます。プロセッサは1つ(コアが数個あるかもしれません)ですが、すべてが同時に動いているように見えます。そしてこれらはすべて…もっと必要なことを求めています。
英語のナレーション・英語と中国語の字幕を切り替え可能
このレッスンの内容
| English | 意味 |
|---|---|
| guidance software | Programs that control the direction and movement of a vehicle. |
| programmer | A person who writes and develops computer programs. |
| compiler/kəmˈpaɪlə/ | コンパイラ |
| interpreter/ɪnˈtɜːprɪtə/ | インタープリタ |
| executable file | A file containing a program that a computer can run. |
| source/sɔːs/ | The original program instructions written by a programmer in a programming language. |
| statement/ˈsteɪtmənt/ | A line of code that tells a program what to do. |
| types/taɪps/ | Categories that describe what kind of data values can be stored or used. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| Lexical analysis/ˈleksɪkl əˈnæləsɪs/ | The process of grouping source code characters into tokens and removing unnecessary whitespace and comments. |
| characters/ˈkærɪktəz/ | A text unit such as a letter, digit, punctuation mark or other symbol. |
| tokens/ˈtəʊkənz/ | トークン |
| keywords/ˈkiːwɜːdz/ | Words with special meanings that are part of a programming language. |
| identifiers | Names chosen by programmers for parts of a program. |
| operators/ˈɒpəreɪtəz/ | Symbols or keywords specifying operations to perform on values or expressions. |
| literals | Fixed values written directly in source code. |
| Whitespace/ˈwaɪtspeɪs/ | Spaces and other characters used to separate parts of source code. |
| comments/ˈkɒments/ | Explanatory text in source code or markup that does not execute as a program instruction. |
| symbol table/ˈsɪmbl ˈteɪbl/ | A compiler data structure recording identifiers and information such as their types, scopes or locations. |
| assignment/əˈsaɪnmənt/ | Storing a value in a variable or other named location. |
| lexer/ˈleksə/ | The part of a compiler that groups source code characters into tokens. |
| Syntax analysis/ˈsɪntæks əˈnæləsɪs/ | The process of checking tokens against language grammar and building a tree of their structure. |
| syntax/ˈsɪntæks/ | The rules that determine the correct structure of statements in a programming language. |
| grammar/ˈɡræmə/ | Rules defining valid sequences of symbols in a programming language. |
| abstract syntax tree/ˈæbstrækt ˈsɪntæks triː/ | 抽象構文木 |
| syntax error/ˈsɪntæks ˈerə/ | 構文エラー |
| parser/ˈpɑːsə/ | The part of a compiler that checks syntax and builds a tree showing program structure. |
| Semantic analysis/səˈmæntɪk əˈnæləsɪs/ | Checking that a program follows meaning rules, such as using compatible types. |
| variable/ˈveərɪəbl/ | A quantity or named value that can change or take different values. |
| function/ˈfʌŋkʃn/ | A mapping that assigns exactly one output to each allowed input. |
| arguments/ˈɑːɡjuːmənts/ | Values or references supplied to a function or procedure when it is called. |
| semantics/səˈmæntɪks/ | The meaning of program statements according to the rules of a programming language. |
| Code generation/kəʊd ˌdʒenəˈreɪʃn/ | The process of producing machine code from the program's analysed structure. |
| target machine code | Instructions produced for the computer or processor that will run the program. |
| registers/ˈredʒɪstəz/ | A small, fast storage location within a processor used to hold data, addresses or instructions temporarily. |
| Code optimisation/kəʊd ˌɒptɪmaɪˈzeɪʃn/ | Improving code so it runs faster or uses less space without changing what it does. |
| constant expressions | Calculations made entirely from fixed values that can be worked out before a program runs. |
| compile time | The period when source code is translated into a form a computer can run. |
| instructions/ɪnˈstrʌkʃnz/ | Commands that tell a computer what actions to perform. |
| pipeline/ˈpaɪplaɪn/ | An arrangement that overlaps stages of different instructions or tasks to improve overall throughput. |
| stand-alone executable | A program file that can run without the compiler being present. |
| operand/ˈɒpərænd/ | A value, register or memory location that an instruction operates on. |
| logic error/ˈlɒdʒɪk ˈerə/ | A mistake in an algorithm or program that produces an incorrect result despite valid syntax. |
| testing/ˈtestɪŋ/ | Running a program with chosen inputs to check that it gives the expected outputs. |
| semantic error | An error where code follows grammar rules but breaks meaning rules, such as using incompatible types. |
コマによる欠陥が宇宙船を失った
- NASAのマライナー1号は、1962年の打ち上げから293秒後に破壊されました。この失敗は、ガイドウェアの単一の誤った文字に起因するとされることが多いです。
- この話は頻繁に語られるのは、すべてのプログラマーが経験する恐怖であるためです:コンパイラが、あなたが意図していないことを述べるプログラムを快く翻訳してしまうことです。
- コンパイラは単一のプロセスではなく5つのプロセスであり、それぞれ異なるクラスのミスを検出します。どの段階で何が検知されるかを知ることは、特定のエラーの発生源を理解することにつながります。
- このLessonでは、インタープリタが何も生成せずどのようにプログラムを実行するか、そしてコンパイラが通す5つの段階について解説します。

インタープリタがプログラムを実行する方法
- インタープリタはソースを同時に翻訳して実行し、文を1つずつ処理します。
- 各文に対して、読み込んだ後、解析し、型をチェックし、その後直ちにアクションを実行して次の文へ進みます。
- 実行可能なファイルを生成しません:翻訳はメモリ上にのみ存在し、破棄されます。これがシラバスの正確なポイントであり、翻訳版を生成せずにプログラムを実行できるということです。
- 問題のあるラインに到達した瞬間にエラーを報告し、通常は停止するため、開発中に迅速なフィードバックを得られます。ただし、すべての実行時に再翻訳を行うため速度が遅く、ソースとインタープリタの両方が常に必要です。

一度翻訳して保存するか、翻訳して実行して忘れるか

An interpreter:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
It works line by line, reporting errors as it reaches them; nothing is saved as an executable, and it is generally slower.
An interpreter can execute a program without producing a translated version of it.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The translation exists only in memory, statement by statement, and is discarded. That is why the interpreter must be present every time the program runs.
第1段階:レキシカル解析
- レキシカル解析は、ソースの個々の文字をトークン(キーワード、識別子、演算子、リテラル)にまとめます。
- Programs whitespaceとコメントは破棄されます。これらはコンパイラにとって意味を持たないためです。また、識別子はシンボルテーブルに登録されます。
- したがって、
total ← count * 2は識別子、代入、識別子、演算子、リテラルというトークン列になります。レキサーはそれが意味を持つかどうかを知りませんし、気にもしません。

Lexical analysis (the lexer) turns:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
The lexer groups characters into tokens and discards whitespace/comments; parsing then builds the tree.
Lexical analysis groups the source characters into ____ and discards whitespace and comments.
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Keywords, identifiers, operators and literals. The lexer makes no judgement about whether the sequence is a valid program.
第2段階:構文解析
- 構文解析(パース)は、トークンの列が言語の文法に適合しているかチェックし、構造を表す抽象構文木を構築します。
- 括弧の欠落や、
ENDIFの欠落、あるいは不適切な場所にあるキーワードはここで検知されます。これが構文エラーであり、コンパイラがプログラムを実行することなくそれを報告できる理由でもあります。

パースが確認する文法

第3段階:意味解析
- 意味解析は、構文上有効なプログラムが実際に意味を持つかをチェックします:すべての変数は使用前に宣言されていること、代入の両側の型が互換性があること、関数が正しい数の引数で呼び出されていることなどです。
total ← "seven" * 2は構文上完璧ですが、意味的には全く無意味です。これを検知するのはこの段階のみです。- これは試験で問われる区別です:構文は形式、意味論は意味です。

Which of these is checked by semantic analysis rather than syntax analysis?
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Brackets are grammar, so syntax; whitespace is the lexer; registers are code generation. Declarations and types are meaning.
第4および第5段階:コード生成と最適化
- コード生成は木をトレースしてターゲット機械命令を出力し、レジスタを選択してデータを配置します。
- コード最適化は動作を変えずにそのコードを改善します:冗長な作業の削除、定数式のカンパイルタイムでの計算、パイプラインに適した命令の並べ替えなどです。
- 出力は、コンパイラが存在せずとも動作する単独実行可能ファイルです。

5つの段階があり、それぞれが前段階では見つけられなかったものを検知します

The phases of compilation
Step through what a compiler does to your source. Each phase hands its output to the next — characters become tokens, tokens become a tree, the tree becomes optimised machine code.
Match each compiler phase to what it does.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Each phase transforms the program a step further: tokens, then a tree, then checked, then optimised code.
Put the compiler stages in order.
難易度 3/5 · 200 コイン + 今日初回達成時の小ボーナス
Lexical → syntax → semantic → code generation → optimisation.
Code optimisation aims to:
難易度 1/5 · 100 コイン + 今日初回達成時の小ボーナス
Optimisation improves the generated code (constant folding, removing redundancy, reordering for the pipeline).
解き方例:どの工程がどのエラーを検出するか
x ← 5 +:トークンが文法に適合しない、右 operand のない演算子なので、構文解析。x ← y + 1でyが未宣言の場合:形式は適切ですが意味ではないので、意味解析。IF a > b THEN OUTPUT aにENDIFがない場合:再度構文解析。- 動作して間違った平均値を印刷するプログラム:どの段階も検知しません。これはロジックエラーであり、テストによってのみ発見されます。段階名を指定し、かつなぜそれ以前の段階が通り越したかを説明してください。

Match each faulty line to the compiler stage that catches it.
難易度 2/5 · 140 コイン + 今日初回達成時の小ボーナス
Syntax is form, semantics is meaning, and a logic error is valid in both but wrong in intent.
Worked example: 2つの翻訳者の比較
- コンパイラとインタープリタがプログラムを扱う方法の違いを2つ挙げよ。 [4]
- コンパイラは実行前に全体のプログラムを翻訳して実行可能ファイルを生成し、その後コンパイラなしで動作します。一方、インタープリタは文を1つずつ翻訳・実行し、実行可能ファイルを生成しないため、インタープリタが常時存在する必要があります。
- コンパイラは翻訳後にすべてのエラーをまとめて報告します。インタープリタは該当ラインに到達した際に最初のエラーを報告して停止します。
- 各マークはペアリングです:片方が何をするか、もう片方が代わりに何をするかを述べてください。
見落としやすいポイント
- インタープリタは翻訳版を生成しません。これがシラバスで使われている表現であり、評価されるポイントです。
- レキサーはトークンを生成して Programs whitespace とコメントを破棄しますが、プログラムが有効かどうかをチェックしません。
- 構文は形式、意味論は意味です。未宣言の変数は構文エラーではなく意味論エラーです。
- 最適化はプログラムが何をするかを変えてはならず、その速さやコンパクトさだけを変えるべきです。
理解できましたか
- インタープリタは文を1つずつ翻訳・実行し、実行可能ファイルを生成せず、最初のエラーを該当ラインで報告し、すべての実行時に再翻訳します
- 語彙解析はトークンを作成し、空白文字やコメントを除去する;構文解析は文法をチェックして抽象構文木を構築し、構文エラーを検出する。
- 意味解析は意味をチェックします:宣言、型、引数の数など
- コード生成は機械語を出力し、最適化は動作を変えずに改善して、単独で実行可能なファイルを与えます
何が問題でしたか?この内容はサイトの運営チームだけが確認します。