第7章高水準プログラミング言語
7.4 Programming Language Implementation7.4 プログラミング言語の実装
Learning Objectives学習目標
By the end of this section, you will be able to:
この節を終えると、次のことができるようになる。
• Discuss how to build and run programs written in various HLLs
• さまざまな高水準言語で書かれたプログラムを、構築して実行する方法を論じる。
• Describe the work of an HLL runtime management implementation
• 高水準言語の実行時管理の実装が担う仕事を説明する。
• List and explain various HLL optimization methods applicable to programs
• プログラムに適用できる高水準言語のさまざまな最適化方法を挙げ、説明する。
To implement programs that you create, you must use a process to generate machine code from source code. As previously discussed, the major methods of implementing programming languages are compilation, pure interpretation, and hybrid implementation. These are complex processes best learned in stages. There are differences between a compiler and an interpreter, as shown in Table 7.9.
英語のヒント
作成したプログラムを実装するには、ソースコードから機械語コードを生成する処理を使わなければならない。前に述べたように、プログラミング言語を実装する主な方法は、コンパイル、純粋な解釈実行、混成実装である。これらは複雑な処理なので、段階的に学ぶのがよい。コンパイラーとインタープリターには、表7.9に示す違いがある。
Compiler コンパイラー | Interpreter インタープリター |
|---|---|
Scans and translates the entire source code at once ソースコード全体を一度に走査して翻訳する。 | Scans and translates the source code one line at a time ソースコードを一行ずつ走査して翻訳する。 |
Takes a relatively large amount of time to scan and translate 走査と翻訳に比較的長い時間がかかる。 | Takes a relatively small amount of time to scan and translate but has an overall longer execution time 走査と翻訳の時間は比較的短いが、全体の実行時間は長い。 |
Always generates an intermediary object code and will need further linking, thus will need more memory 必ず中間のオブジェクトコードを生成し、その後のリンクも必要なので、より多くのメモリを要する。 | Does not generate an intermediary code so highly efficient in terms of its memory 中間コードを生成しないため、メモリの面で非常に効率がよい。 |
Generates any error message only after it scans the complete program プログラム全体を走査してから、初めてエラーメッセージを出す。 | Translates and executes the program until an error is encountered, then it stops working エラーが見つかるまでプログラムを翻訳して実行し、エラーが起きると動作を止める。 |
Table 7.9 Differences Between Compilation and Interpretation
表7.9 コンパイルと解釈実行の違い
Preprocessing前処理
The single process that takes the source code the programmer creates and transforms it into another language is called compilation. In short, it is the rendering from one language into another and includes a deconstruction of the input, and its product may be machine language. Modern compilation usually starts with preprocessing, a step in which the source code is manipulated to ready it for the compilation stage. Some of the actions the preprocessor can take are as follows:
英語のヒント
プログラマーが作成したソースコードを受け取り、別の言語へ変換する一連の処理を、コンパイルという。端的にいえば、ある言語から別の言語への翻訳である。入力を分解する処理を含み、その生成物は機械語の場合がある。現代のコンパイルは、通常、前処理から始まる。この段階では、コンパイルに備えてソースコードを加工する。プリプロセッサーが行う処理には、次のようなものがある。
• Removing any comments and white space
英語のヒント
• コメントと空白文字を取り除く。
• Executing a preprocessor directive, which describes resources the program needs to compile, usually files to insert into the source code. The C/C++ preprocessor provides a fine example.
英語のヒント
• プリプロセッサー指令を実行する。これは、コンパイルに必要な資源、通常はソースコードへ挿入するファイルを指定する。C/C++のプリプロセッサーがよい例である。
1. The #include directive of C/C++ deals with a variety of files such as header or standard files containing the definitions of objects and functions from the API that are going to be used in the program. It can also take care of user-defined files or modules.
英語のヒント
1. C/C++の#include指令は、ヘッダーファイルや標準ファイルなど、さまざまなファイルを扱う。これらには、プログラムで使うAPIのオブジェクトや関数の定義が含まれている。また、利用者が定義したファイルやモジュールも扱える。
2. The #define directive of C/C++ names a piece of code for the preprocessor to replace every time it sees the name.
英語のヒント
2. C/C++の#define指令は、コードの一部分に名前を付け、プリプロセッサーがその名前を見るたびにコードで置き換えるようにする。
3. The #ifdef directive of C/C++ tells the compiler to include a specified piece of code in compilation depending upon some condition.
英語のヒント
3. C/C++の#ifdef指令は、ある条件に応じて、指定したコードの一部分をコンパイルに含めるようにコンパイラーへ指示する。
• Other tasks include pre-identifying high-level constructs such as loops and functions.
英語のヒント
• ほかの作業には、ループや関数などの高水準の構成要素を、事前に識別することがある。
Compilationコンパイル
The phases of compilation are grouped into three stages that are usually executed in three passes through the preprocessed source code:
英語のヒント
コンパイルの各工程は三つの段階にまとめられ、通常は、前処理済みのソースコードを三回走査して実行される。
• front end that is responsible for the analysis of source code
英語のヒント
• フロントエンドは、ソースコードの解析を担当する。
• middle end that is responsible for performing optimizations of the code
• ミドルエンドは、コードの最適化を担当する。
• back end (or code generator) that is responsible for the production of the target code
• バックエンド(コード生成器)は、目的のコードの生成を担当する。
Front-End Compiler Structureコンパイラーのフロントエンドの構造
The front end is responsible for analyzing the source code for syntax, semantics, and other tasks. The first part of this process is lexical analysis, which converts the source code strings of characters into tokens. These are sequences of compiler recognizable atomic symbols which make it much easier for the next steps to recognize them.
英語のヒント
フロントエンドは、ソースコードの構文と意味を解析することなどを担当する。最初の処理は字句解析で、ソースコードの文字列をトークンへ変換する。トークンは、コンパイラーが認識できる、それ以上分けない記号の列であり、以降の段階での識別を大幅に容易にする。
The process proceeds to syntax analysis, usually referred to as parsing or reading the code to make sure it conforms with the syntax rules of the language. This is done to test conformability logically by breaking code into separate parts.
英語のヒント
処理は次に、構文解析へ進む。これは、一般にパーシング、または言語の構文規則に従っているかを確かめるためにコードを読むことと呼ばれる。コードを別々の部分に分解し、規則への適合性を論理的に検査するために行う。
The next step is semantic analysis, which is the discovery of meaning in the code. It takes care of type checking in strongly typed languages. It also performs binding, which associates variable and function references with their declarations and definitions. The compiler runs a static semantic analysis which concerns itself only with the source code without testing inputs.
英語のヒント
次は意味解析で、コードの意味を見いだす段階である。強い型付けを持つ言語では、型検査を行う。また、変数や関数への参照を、その宣言や定義と結び付ける束縛も行う。コンパイラーは、入力を試さず、ソースコードだけを対象とする静的意味解析を実施する。
Semantic analysis cannot figure out all meaning in the code. Many languages also use dynamic binding (late binding) that defers the binding of certain elements such as objects and methods until runtime. This is particularly true of OOP languages which support inheritance and polymorphism. Some processes, such as when an array goes out of bounds, will not be known until run time, and are part of the program’s dynamic semantics.
英語のヒント
意味解析だけでは、コードのすべての意味を把握できない。多くの言語では、オブジェクトやメソッドなど、特定の要素の束縛を実行時まで遅らせる、動的束縛(遅延束縛)も使う。これは、継承とポリモーフィズムを支えるOOP言語に特によく当てはまる。配列の範囲を超える場合など、一部の処理は実行時まで分からず、プログラムの動的な意味の一部となる。
Semantic analysis concludes with the creation and management of the symbol table, a data structure that is used to connect every symbol with necessary information such as data type, scope, and memory location.
英語のヒント
意味解析は、記号表の作成と管理で締めくくられる。記号表は、各記号を、データ型、スコープ、メモリ位置などの必要な情報と結び付けるためのデータ構造である。